def make_item_content_encoded(self, text1, text2, url, comment_name): """ Called from item_content_encoded() in children. text1 and text2 are chunks of HTML text (or empty strings). url is the URL of the item (no domain needed, eg '/diary/1666/10/31/'). comment_name is one of 'comment' or 'annotation'. """ return '%s %s <p><strong><a href="%s#%ss">Read the %ss</a></strong></p>' % ( force_unicode(smartypants.smartyPants(text1)), force_unicode(smartypants.smartyPants(text2)), add_domain(Site.objects.get_current().domain, url), comment_name, comment_name)
def make_item_content_encoded(self, text1, text2, url, comment_name): """ Called from item_content_encoded() in children. text1 and text2 are chunks of HTML text (or empty strings). url is the URL of the item (no domain needed, eg '/diary/1666/10/31/'). comment_name is one of 'comment' or 'annotation'. """ return '%s %s <p><strong><a href="%s#%ss">Read the %ss</a></strong></p>' % ( force_unicode(smartypants.smartyPants(text1)), force_unicode(smartypants.smartyPants(text2)), add_domain(Site.objects.get_current().domain, url), comment_name, comment_name )
def sidenoteLinkToMarkdown(match): group = match.groupdict() return ("[%s](javascript:Sidenote.openColumnLoud\('#%s','#%s','%s'\))" % (group["linktext"], escapeQuotes(source), escapeQuotes(group["identifier"]), escapeQuotes(smartypants.smartyPants(group["linktext"]))))
def keywordToSidenoteLink(match): keyword = match.group() begin = end = "" if re.match("\W", keyword[0]): begin = keyword[0] keyword = keyword[1:] if re.match("\W", keyword[-1]): end = keyword[-1] keyword = keyword[:-1] pageId = keywordIndex[keyword.lower()] if pageId == source: return "%s%s%s" % (begin, keyword, end) if keyword in observedKeywords: loudness = "Quiet" else: loudness = "Loud" observedKeywords.add(keyword) return ("%s[%s](javascript:Sidenote.openColumn%s\('#%s','#%s','%s'\))%s" % (begin, keyword, loudness, escapeQuotes(source), escapeQuotes(pageId), escapeQuotes(smartypants.smartyPants(keyword)), end))
def formatQA(html, type, cid, mid, fact, tags, cm): logger.debug(u"before smartypants, html is:\n" + html) if NO_SMARTYPANTS_TAG not in tags: html = smartypants.smartyPants(html, attr="2") logger.debug(u"after smartypants, html is:\n" + html) return html
def apply_smartypants(self, text, smarty, node): # Try to be clever about when to use smartypants if node.__class__ in (docutils.nodes.paragraph, docutils.nodes.block_quote, docutils.nodes.title): return smartyPants(text, smarty) return text
def prettify_text(self, text): """ Make text more nicerer. Run it through SmartyPants and Widont. """ text = self.widont(text) text = smartypants.smartyPants(text) return text
def read_and_parse_entries(): files = sorted( [join(DIRS['source'], f) for f in os.listdir(DIRS['source'])], reverse=True) entries = [] for file in files: match = META_REGEX.findall(file) if len(match): meta = match[0] with open(file, 'r') as open_file: msg = email.message_from_file(open_file) date = datetime(*[int(d) for d in meta[0:3]]) entries.append({ 'slug': slugify(meta[3]), 'title': meta[3].replace('.', ' '), 'tags': msg['Tags'].split(), 'date': { 'iso8601': date.isoformat(), 'rfc3339': rfc3339(date), 'display': date.strftime('%Y-%m-%d'), }, 'content_html': smartyPants(_markdown(msg.get_payload())), }) return entries
def typogrify(content): """The super typography filter Applies the following filters: widont, smartypants, caps, amp, initial_quotes""" return number_suffix( initial_quotes(caps(smartypants.smartyPants(widont(amp(content)), "2"))))
def typographie(text): text = html_parser.unescape(text) text = force_unicode(text) text = smartyPants(text) text = ellipsis(text) text = spaces(text) text = widont(text) return mark_safe(text)
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ return _smartypants.smartyPants(text)
def unsmartypants(value): """ Normalizes a string which has been processed by smartypants.py. """ try: import smartypants return smartypants.smartyPants(value, '-1') except ImportError: return value
def smartquotes(text): """Applies smarty pants to curl quotes. >>> smartquotes('The "Green" man') u'The “Green” man' """ text = unicode(text) output = smartypants.smartyPants(text) return output
def main(): with open(sys.argv[1]) as f: source = f.read() doc_parts = publish_parts( source, settings_overrides={'output_encoding': 'utf8', 'initial_header_level': 2, }, writer_name="html") RE = smartypants.tags_to_skip_regex pattern = RE.pattern.replace('|code', '|code|tt') pattern = pattern.replace('|script', '|script|style') RE = re.compile(pattern, RE.flags) smartypants.tags_to_skip_regex = RE print smartyPants(doc_parts['fragment']).encode('utf-8')
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ text = force_unicode(text) output = _smartypants.smartyPants(re.sub(ur'“|”', '"', re.sub(ur'‘|’', "'", text))) return output
def smartypants_filter(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ text = force_unicode(text) output = smartypants.smartyPants(text) return mark_safe(output)
def _smartypants(text): """Applies SmartyPants transformations to a block of text.""" text = force_unicode(text) try: import smartypants except ImportError: return text else: return smartypants.smartyPants(text)
def save(self, force_insert=False, force_update=False): """ Use Markdown to convert the ``copy`` field from plain-text to HTMl. Smartypants is also used to bring in curly quotes. """ from markdown import markdown from smartypants import smartyPants self.copy_html = smartyPants( markdown(self.copy, ['abbr', 'headerid(level=2)'])) super(Entry, self).save(force_insert=False, force_update=False)
def save(self, force_insert=False, force_update=False): """ Use Markdown to convert the ``copy`` field from plain-text to HTMl. Smartypants is also used to bring in curly quotes. """ from markdown import markdown from smartypants import smartyPants self.copy_html = smartyPants(markdown(self.copy, ['abbr', 'headerid(level=2)'])) super(Entry, self).save(force_insert=False, force_update=False)
def curlify(text): """Replace quotes in `text` with curly equivalents.""" if config.options.smartypants == 'off': return text # Replace any ampersands with an entity so we don't harm the text. text = text.replace('&', '&') # Use smartypants to curl the quotes, creating HTML entities text = smartypants.smartyPants(text, config.options.smartypants) # Replace the entities with real Unicode characters. text = re.sub('&#(\d+);', lambda m: unichr(int(m.group(1))), text) return text
def typogrify(content): """The super typography filter Applies the following filters: widont, smartypants, caps, amp, initial_quotes""" return number_suffix( initial_quotes( caps( smartypants.smartyPants( widont( amp(content)), mode))))
def smartypants(value): """ Filters the value through SmartyPants for converting plain ASCII punctuation characters into typographically correct versions """ try: from smartypants import smartyPants return smartyPants(value) except ImportError: return value
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ t = _smartypants.smartyPants(text) # replace "curly" quotes with russian-style quotes (« and ») t = t.replace('“', '«') t = t.replace('”', '»') return t
def markdown_filter(data, img_path=None): if data is None: return '' if img_path: # replace relative paths to images with absolute data = RELATIVE_PATH_RE.sub('[\g<1>](' + img_path + '/\g<2>)', data) data = convert_legacy_people_to_at_names(data) result = markdown(data, extensions=['codehilite', 'fenced_code']) result = smartyPants(result) return result
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') 'The “Green” man' """ try: import smartypants except ImportError: return text else: return smartypants.smartyPants(text)
def smartypants(text): """Applies smarty pants to curl quotes. >>> Typogrify.smartypants('The "Green" man') 'The “Green” man' """ try: import smartypants except ImportError: raise TypogrifyError, "Error in {% smartypants %} filter: The Python smartypants library isn't installed." else: output = smartypants.smartyPants(text) return output
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ try: import smartypants except ImportError: logger.error("Error in {% smartypants %} filter: The Python smartypants library isn't installed.") return text output = smartypants.smartyPants(text) return jinja2.Markup(output)
def to_html(rest, smart='1'): """ Converts reStructuredText to HTML fragment. By default, educates quotes, dashes and ellipses with the smartypants engine. You can feed in any of the options it accepts: http://web.chad.org/projects/smartypants.py/#options. """ parts = core.publish_parts(source=rest, writer_name='html', settings_overrides={'initial_header_level': 3}) html = '%s%s' % (parts['body_pre_docinfo'], parts['fragment']) html = smartyPants(html, smart) return html
def _CreateSeqDiv(self, seq_obj, kind): soup = bs4.BeautifulSoup("") seq = soup.new_tag("div") seq["class"] = kind seq_h = soup.new_tag("h2") seq_h.string = smartyPants(seq_obj["title"]) seq.append(seq_h) if "description" in seq_obj: # XXX "html.parser" is used here because lxml adds <html></html> around # the element. Figure out how to achieve what we need here in a proper # manner. desc_soup = bs4.BeautifulSoup(smartyPants(seq_obj["description"]), "html.parser") seq_desc = soup.new_tag("div") seq_desc["class"] = CssClass.DESCRIPTION for elem in desc_soup.children: seq_desc.append(elem) seq.append(seq_desc) return seq
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') 'The “Green” man' """ try: import smartypants except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError, "Error in {% smartypants %} filter: The Python smartypants library isn't installed." return text else: return smartypants.smartyPants(text)
def markdown_filter(data, img_path=None): if data is None: return '' if img_path: # replace relative paths to images with absolute data = RELATIVE_PATH_RE.sub('[\g<1>](' + img_path + '/\g<2>)', data) data = convert_legacy_people_to_at_names(data) if data.startswith('#'): data = '\\' + data result = markdown(data, extensions=['codehilite', 'fenced_code']) result = smartyPants(result) return result
def markdown_filter(data, img_path=None): if data is None: return "" if img_path: # replace relative paths to images with absolute data = RELATIVE_PATH_RE.sub("[\g<1>](" + img_path + "/\g<2>)", data) data = convert_legacy_people_to_at_names(data) if data.startswith("#"): data = "\\" + data result = markdown(data, extensions=["codehilite", "fenced_code"]) result = smartyPants(result) return result
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ text = force_unicode(text) try: import smartypants except ImportError: raise Exception, "Error in {% smartypants %} filter: The Python smartypants library isn't installed." else: output = smartypants.smartyPants(text) return mark_safe(output)
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') 'The “Green” man' """ try: import smartypants except ImportError: raise TypogrifyError( "Error in {% smartypants %} filter: The Python smartypants library isn't installed." ) else: output = smartypants.smartyPants(text) return output
def markdown_filter(data, img_path=None, url_processor=url_to_link, person_processor=person_to_microcard): if data is None: return '' if img_path: # replace relative paths to images with absolute data = RELATIVE_PATH_RE.sub('[\g<1>](' + img_path + '/\g<2>)', data) data = convert_legacy_people_to_at_names(data) result = markdown(data, extensions=['codehilite', 'fenced_code']) if url_processor or person_processor: result = autolink(result, url_processor, person_processor) result = smartyPants(result) return result
def unsmartypants(value): """ Normalizes a string which has been processed by smartypants.py. {% entry.body|smartypants|unsmartypants %} """ try: import smartypants except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError, "Error in {% smartypants %} filter: The Python SmartyPants library isn't installed." return value else: return smartypants.smartyPants(value, '-1')
def smartypants(text): """Applies smarty pants to curl quotes. >>> smartypants('The "Green" man') u'The “Green” man' """ text = force_unicode(text) try: import smartypants except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError, "Error in {% smartypants %} filter: The Python smartypants library isn't installed." return text else: output = smartypants.smartyPants(text) return mark_safe(output)
def smartypants(value): """ Applies SmartyPants to a piece of text, applying typographic niceties. Requires the Python SmartyPants library to be installed; see http://web.chad.org/projects/smartypants.py/ """ try: from smartypants import smartyPants except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError("Error in smartypants filter: the Python smartypants module is not installed or could not be imported") return value else: return mark_safe(smartyPants(value))
def generate_chapter(self, chapter): """Return a rendered 'chapter' given a chapter element from the manifest. This uses the extension of the input file to determine the processing (if any) to be done. First external processors defined in the configuration file are checked; otherwise, the internal Markdown processor is used for .md/.txt files. XHTML files are used as-is. """ html = False filename, file_ext = os.path.splitext(chapter['file']) file_ext = file_ext.lower() stylesheet = self.styles + '/' + chapter.get( 'stylesheet', self.manifest.get('stylesheet', 'default.css')) processors = self.config.get('processors', {}) if processors.has_key(file_ext): command = processors[file_ext].format(chapter['file']) try: html = subprocess.check_output(command.split()) except: print "Error running '{}'".format(chapter['file']) sys.exit(1) # TODO handle partial or full HTML output here elif file_ext == '.md' or file_ext == '.txt' or file_ext == '.markdown': try: file_obj = open(self.source_dir + '/' + chapter['file']) text = file_obj.read() except IOError as err: print err sys.exit(1) try: html = smartyPants(markdown(text)) except MarkdownException as err: print "Error processing {} - {}".format(chapter['file'], err) sys.exit(1) elif file_ext == '.xhtml': file_obj = open(self.source_dir + '/' + chapter['file']) return file_obj.read() if html is False: raise BinderError(BinderError.WEIRD_FILE, 'Unknown file type: ' + chapter['file']) return self.templatize( 'chapter.xhtml', { 'content': html, 'stylesheet': stylesheet, 'title': chapter.get('title') })
def smartypants(value): """ Applies SmartyPants to a piece of text, applying typographic niceties. Requires the Python SmartyPants library to be installed; see http://web.chad.org/projects/smartypants.py/ """ try: from smartypants import smartyPants except ImportError: if settings.DEBUG: raise TemplateSyntaxError("Error in smartypants filter: the Python smartypants module is not installed or could not be imported") return value else: return smartyPants(value)
def render(self, text, **kwargs): from smartypants import smartyPants return smartyPants(text, **kwargs)
def encode(self, text): text = html4css1.HTMLTranslator.encode(self, text) if self._in_literal <= 0: text = smartypants.smartyPants(text, "qde") return text
def smartypants(value): try: import smartypants return smartypants.smartyPants(value) except ImportError: return value
def run(content): return smartypants.smartyPants(content)