def pygmentify_html(text, **kwargs): text = smart_text(text) lang = default_lang = 'text' # a tuple of known lexer names try: lexers_iter = LEXERS.itervalues() except AttributeError: lexers_iter = LEXERS.values() lexer_names = reduce(lambda a, b: a + b[2], lexers_iter, ()) # custom formatter formatter = ListHtmlFormatter(encoding='utf-8', **kwargs) subs = [] pre_re = re.compile(r'(<pre[^>]*>)(.*?)(</pre>)', re.DOTALL | re.UNICODE) br_re = re.compile(r'<br[^>]*?>', re.UNICODE) p_re = re.compile(r'<\/?p[^>]*>', re.UNICODE) lang_re = re.compile(r'lang=["\'](.+?)["\']', re.DOTALL | re.UNICODE) for pre_match in pre_re.findall(text): work_area = pre_match[1] work_area = br_re.sub('\n', work_area) match = lang_re.search(pre_match[0]) if match: lang = match.group(1).strip() if lang not in lexer_names: lang = default_lang lexer = get_lexer_by_name(lang, stripall=True) work_area = work_area.replace(u' ', u' ').replace( u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace( u'"', u'"').replace(u''', u"'") work_area = p_re.sub('', work_area) work_area = highlight(work_area, lexer, formatter) subs.append([u''.join(pre_match), smart_text(work_area)]) for sub in subs: text = text.replace(sub[0], sub[1], 1) return text
def pygmentify_html(text, **kwargs): text = smart_text(text) lang = default_lang = 'text' # a tuple of known lexer names try: lexers_iter = LEXERS.itervalues() except AttributeError: lexers_iter = LEXERS.values() lexer_names = reduce(lambda a,b: a + b[2], lexers_iter, ()) # custom formatter formatter = ListHtmlFormatter(encoding='utf-8', **kwargs) subs = [] pre_re = re.compile(r'(<pre[^>]*>)(.*?)(</pre>)', re.DOTALL | re.UNICODE) br_re = re.compile(r'<br[^>]*?>', re.UNICODE) p_re = re.compile(r'<\/?p[^>]*>', re.UNICODE) lang_re = re.compile(r'lang=["\'](.+?)["\']', re.DOTALL | re.UNICODE) for pre_match in pre_re.findall(text): work_area = pre_match[1] work_area = br_re.sub('\n', work_area) match = lang_re.search (pre_match[0]) if match: lang = match.group(1).strip() if lang not in lexer_names: lang = default_lang lexer = get_lexer_by_name(lang, stripall=True) work_area = work_area.replace(u' ', u' ').replace(u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace(u'"', u'"').replace(u''', u"'") work_area = p_re.sub('', work_area) work_area = highlight(work_area, lexer, formatter) subs.append([u''.join(pre_match), smart_text(work_area)]) for sub in subs: text = text.replace(sub[0], sub[1], 1) return text
def get_pygments_lexer(name): name = name.lower() if name == 'ipython2': from IPython.lib.lexers import IPythonLexer return IPythonLexer elif name == 'ipython3': from IPython.lib.lexers import IPython3Lexer return IPython3Lexer else: for module_name, cls_name, aliases, _, _ in LEXERS.values(): if name in aliases: return find_lexer_class(cls_name) warn("No lexer found for language %r. Treating as plain text." % name) from pygments.lexers.special import TextLexer return TextLexer
def get_pygments_lexer(name): name = name.lower() if name == 'ipython2': from IPython.lib.lexers import IPythonLexer return IPythonLexer elif name == 'ipython3': from IPython.lib.lexers import IPython3Lexer return IPython3Lexer else: for module_name, cls_name, aliases, _, _ in LEXERS.values(): if name in aliases: return find_lexer_class(cls_name) warn("No lexer found for language %r. Treating as plain text." % name) from pygments.lexers.special import TextLexer return TextLexer
import posixpath import pygments.lexers.web pygments_lexer_cache = {} file_ext_to_lexer_alias_cache = { '.pycon': 'pycon', '.rbcon': 'rbcon', '.Rd': 'latex', '.svg': 'xml', '.jinja': 'jinja' } # Add all pygments standard mappings. for module_name, name, alias_or_aliases, file_extensions, mime_types in list( PYGMENTS_LEXERS.values()): try: alias = alias_or_aliases[0] except IndexError: alias = alias_or_aliases for ext in file_extensions: ext = ext.lstrip("*") file_ext_to_lexer_alias_cache[ext] = alias class SyntaxHighlightMarkdownFilter(DexyFilter): """ Surrounds code with highlighting instructions for Markdown """ aliases = ['pyg4md']
@register_for('*.png', '*.jpg', '*.jpeg', '*.gif') def render_images(ctx, blob_obj): w, h = utils.calc_thumb_size(blob_obj.data, (640, 480)) url = ctx.url_for('view_obj', rev=blob_obj.commit.name, path=blob_obj.root_path) raw_url = url + '?raw=1' body = '<a href="%s"><img src="%s" width="%d" height="%s"></a>' % \ (raw_url, raw_url, w, h) return Document(title=blob_obj.name, body=body) formatter = HtmlFormatter(noclasses=True, linenos=True) @register_for(*[p for l in LEXERS.values() for p in l[3]]) def render_sourcecode(ctx, blob_obj): try: data = blob_obj.data.decode('utf-8') except UnicodeDecodeError: data = blob_obj.data try: lexer = guess_lexer_for_filename(blob_obj.name, data) except ValueError: # no lexer found - use the text one instead of an exception lexer = TextLexer() return Document(title=blob_obj.name, description=lexer.name,