PKWˆm5EΉŽΕ‰‰tracpygments/__init__.py# -*- coding: utf-8 -*- # # Copyright (C) 2006 Matthew Good # All rights reserved. # # This software is licensed as described in the file COPYING, which # you should have received as part of this distribution. The terms # are also available at http://trac.edgewall.org/wiki/TracLicense. # # Author: Matthew Good """Syntax highlighting module, based on the Pygments module. """ import re from StringIO import StringIO from trac.core import * from trac.config import ListOption from trac.mimeview.api import IHTMLPreviewRenderer, Mimeview from trac.wiki.api import IWikiMacroProvider __all__ = ['PygmentsRenderer', 'PygmentsProcessors'] try: import pygments from pygments.lexers._mapping import LEXERS from pygments.plugin import find_plugin_lexers from pygments.formatters.html import HtmlFormatter from pygments.token import * except ImportError: pygments = None class PygmentsRenderer(Component): """Syntax highlighting based on Pygments.""" implements(IHTMLPreviewRenderer) enscript_modes = ListOption('mimeviewer', 'pygments_modes', '', doc= """List of additional MIME types known by Pygments. For each, a tuple `mimetype:mode:quality` has to be specified, where `mimetype` is the MIME type, `mode` is the corresponding Pygments mode to be used for the conversion and `quality` is the quality ratio associated to this conversion. That can also be used to override the default quality ratio used by the Pygments render.""") expand_tabs = True QUALITY_RATIO = 9 def __init__(self): self._types = None def get_quality_ratio(self, mimetype): if pygments is None: return 0 # Extend default MIME type to mode mappings with configured ones if self._types is None: self._types = {} for modname, _, aliases, _, mimetypes in _iter_lexerinfo(): for mimetype in mimetypes: self._types[mimetype] = (aliases[0], self.QUALITY_RATIO) self._types.update( Mimeview(self.env).configured_modes_mapping('pygments')) try: return self._types[mimetype][1] except KeyError: return 0 def render(self, req, mimetype, content, filename=None, rev=None): try: mimetype = mimetype.split(';', 1)[0] lang = self._types[mimetype][0] return _format(lang, content, True) except (KeyError, ValueError): raise Exception("No Pygments lexer found for mime-type '%s'." % mimetype) class PygmentsProcessors(Component): implements(IWikiMacroProvider) def __init__(self): self.languages = {} try: for modname, name, aliases, _, _ in _iter_lexerinfo(): for alias in aliases: self.languages[alias] = name except ImportError: pass def get_macros(self): return self.languages.keys() def get_macro_description(self, name): return 'Syntax highlighting for %s using Pygments' % self.languages[name] def render_macro(self, req, name, content): return _format(name, content) def _iter_lexerinfo(): for info in LEXERS.itervalues(): yield info for cls in find_plugin_lexers(): yield cls.__module__, cls.name, cls.aliases, cls.filenames, cls.mimetypes def _format(lang, content, annotate=False): lexer = pygments.get_lexer_by_name(lang) formatter = TracHtmlFormatter(cssclass = not annotate and 'code' or '') html = pygments.highlight(content, lexer, formatter).rstrip('\n') if annotate: return html[len('
'):-len('
')].splitlines() else: return html def _issubtoken(token, base): while token is not None: if token == base: return True token = token.parent return False if pygments is not None: class TracHtmlFormatter(HtmlFormatter): # more specific should come before their parents in order to # resolve them in the right order token_classes = [ (Comment.Preproc, 'code-prep'), (Comment, 'code-comment'), (Name.Attribute, 'h_attribute'), (Name.Builtin, 'code-lang'), (Name.Class, 'code-type'), #(Name.Constant, 'code-type'), #(Name.Decorator, 'code-type'), (Name.Entity, 'h_entity'), #(Name.Exception, 'code-type'), (Name.Function, 'code-func'), #(Name.Label, 'code-type'), #(Name.Namespace, 'code-type'), (Name.Tag, 'h_tag'), (Name.Variable, 'code-var'), (Operator, 'code-lang'), (String, 'code-string'), # TODO String subtokens (Keyword.Type, 'code-type'), (Keyword, 'code-keyword'), ] def _get_css_class(self, ttype): try: return self._class_cache[ttype] except KeyError: pass for token, css_class in self.token_classes: if _issubtoken(ttype, token): break else: css_class = None if css_class is not None: css_class = self.classprefix + css_class self._class_cache[ttype] = css_class return css_class PKšˆm5ŽŽΣ€--tracpygments/__init__.pyc;ς ·YEc@s(dZdkZdklZdkTdklZdklZlZdk l Z ddgZ y;dk Z d k lZd klZd klZdkTWnej o eZ nXdefd „ƒYZdefd „ƒYZd„Zed„Zd„Ze ej odefd„ƒYZndS(s:Syntax highlighting module, based on the Pygments module. N(sStringIO(s*(s ListOption(sIHTMLPreviewRenderersMimeview(sIWikiMacroProvidersPygmentsRenderersPygmentsProcessors(sLEXERS(sfind_plugin_lexers(s HtmlFormattercBs]tZdZeeƒedddddƒZeZdZ d„Z d„Z e e d „Z RS( s&Syntax highlighting based on Pygments.s mimeviewerspygments_modesssdocs­List of additional MIME types known by Pygments. For each, a tuple `mimetype:mode:quality` has to be specified, where `mimetype` is the MIME type, `mode` is the corresponding Pygments mode to be used for the conversion and `quality` is the quality ratio associated to this conversion. That can also be used to override the default quality ratio used by the Pygments render.i cCs t|_dS(N(sNonesselfs_types(sself((s3build/bdist.linux-i686/egg/tracpygments/__init__.pys__init__6scCsΣttjodSn|itjo}h|_xKtƒD]@\}}}}}x(|D] }|d|i f|i|tƒD]3\}}}}}x|D]}||i|
s
( spygmentssget_lexer_by_nameslangslexersTracHtmlFormattersannotates formatters highlightscontentsrstripshtmlslens splitlines(slangscontentsannotateslexershtmls formatter((s3build/bdist.linux-i686/egg/tracpygments/__init__.pys_formatos "cCs;x0|tj o"||jotSn|i}qWtSdS(N(stokensNonesbasesTruesparentsFalse(stokensbase((s3build/bdist.linux-i686/egg/tracpygments/__init__.pys _issubtokenxs   sTracHtmlFormattercBs§tZeidfedfeidfeidfeidfeidfei dfei dfei d fe dfe d feidfed fg Zd „ZRS( Ns code-preps code-comments h_attributes code-langs code-typesh_entitys code-funcsh_tagscode-vars code-strings code-keywordcCsy|i|SWntj onXx2|iD]!\}}t||ƒoPq/q/Wt}|tj o|i |}n||i|<|SdS(N( sselfs _class_cachesttypesKeyErrors token_classesstokens css_classs _issubtokensNones classprefix(sselfsttypes css_classstoken((s3build/bdist.linux-i686/egg/tracpygments/__init__.pys_get_css_classšs    (s__name__s __module__sCommentsPreprocsNames AttributesBuiltinsClasssEntitysFunctionsTagsVariablesOperatorsStringsKeywordsTypes token_classess_get_css_class(((s3build/bdist.linux-i686/egg/tracpygments/__init__.pysTracHtmlFormatters–(s__doc__sresStringIOs trac.cores trac.configs ListOptionstrac.mimeview.apisIHTMLPreviewRenderersMimeviews trac.wiki.apisIWikiMacroProviders__all__spygmentsspygments.lexers._mappingsLEXERSspygments.pluginsfind_plugin_lexersspygments.formatters.htmls HtmlFormatterspygments.tokens ImportErrorsNones ComponentsPygmentsRenderersPygmentsProcessorss_iter_lexerinfosFalses_formats _issubtokensTracHtmlFormatter(s ListOptionsMimeviews__all__sIWikiMacroProvidersStringIOs_iter_lexerinfosfind_plugin_lexerssPygmentsProcessorssrespygmentss _issubtokens_formatsPygmentsRenderersIHTMLPreviewRenderers HtmlFormattersTracHtmlFormattersLEXERS((s3build/bdist.linux-i686/egg/tracpygments/__init__.pys? s,           1   PKšˆm5΅©°ddEGG-INFO/PKG-INFOMetadata-Version: 1.0 Name: TracPygments Version: 0.1 Summary: Trac syntax colorer using Pygments Home-page: http://trac-hacks.org/wiki/TracPygmentsPlugin Author: Matt Good Author-email: matt@matt-good.net License: BSD Description: Keywords: trac.mimeview Platform: UNKNOWN Classifier: Framework :: Trac Classifier: License :: OSI Approved :: BSD License PKšˆm5#ΒX%%EGG-INFO/SOURCES.txtsetup.cfg setup.py TracPygments.egg-info/PKG-INFO TracPygments.egg-info/SOURCES.txt TracPygments.egg-info/dependency_links.txt TracPygments.egg-info/entry_points.txt TracPygments.egg-info/requires.txt TracPygments.egg-info/top_level.txt TracPygments.egg-info/zip-safe tracpygments/__init__.py PKšˆm5“Χ2EGG-INFO/dependency_links.txt PKšˆm5‰q‘I,,EGG-INFO/entry_points.txt[trac.plugins] tracpygments = tracpygments PKšˆm5τ΄’ΘEGG-INFO/requires.txtPygments >= 0.5PKšˆm5[ε’ EGG-INFO/top_level.txttracpygments PKmˆm5“Χ2EGG-INFO/zip-safe PKWˆm5EΉŽΕ‰‰€tracpygments/__init__.pyPKšˆm5ŽŽΣ€--€Ώtracpygments/__init__.pycPKšˆm5΅©°dd€#4EGG-INFO/PKG-INFOPKšˆm5#ΒX%%€Ά5EGG-INFO/SOURCES.txtPKšˆm5“Χ2€ 7EGG-INFO/dependency_links.txtPKšˆm5‰q‘I,,€I7EGG-INFO/entry_points.txtPKšˆm5τ΄’Θ€¬7EGG-INFO/requires.txtPKšˆm5[ε’ €ξ7EGG-INFO/top_level.txtPKmˆm5“Χ2€/8EGG-INFO/zip-safePK f_8