core, template (filters): added pygments as a dependency + new highlight filter to use it.
This commit is contained in:
parent
5aec6ed680
commit
87690d88ce
2 changed files with 26 additions and 1 deletions
2
setup.py
2
setup.py
|
|
@ -303,6 +303,6 @@ setup(name=NAME,
|
|||
],
|
||||
scripts=['frontends/src/jp/jp', 'frontends/src/primitivus/primitivus', ],
|
||||
zip_safe=False,
|
||||
install_requires=['twisted >= 15.2.0', 'wokkel >= 0.7.1', 'sat_tmp', 'progressbar', 'urwid >= 1.2.0', 'urwid-satext >= 0.6.1', 'mutagen', 'pillow', 'lxml >= 3.1.0', 'pyxdg', 'markdown', 'html2text', 'pycrypto >= 2.6.1', 'python-potr', 'PyOpenSSL', 'service_identity', 'shortuuid', 'babel'],
|
||||
install_requires=['twisted >= 15.2.0', 'wokkel >= 0.7.1', 'sat_tmp', 'progressbar', 'urwid >= 1.2.0', 'urwid-satext >= 0.6.1', 'mutagen', 'pillow', 'lxml >= 3.1.0', 'pyxdg', 'markdown', 'html2text', 'pycrypto >= 2.6.1', 'python-potr', 'PyOpenSSL', 'service_identity', 'shortuuid', 'babel', 'pygments'],
|
||||
cmdclass={'install': CustomInstall},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -32,6 +32,9 @@ from babel import support
|
|||
from babel import Locale
|
||||
from babel.core import UnknownLocaleError
|
||||
from babel import dates
|
||||
import pygments
|
||||
from pygments import lexers
|
||||
from pygments import formatters
|
||||
try:
|
||||
import sat_templates
|
||||
except ImportError:
|
||||
|
|
@ -208,6 +211,7 @@ class Renderer(object):
|
|||
self.env.filters['item_filter'] = self._item_filter
|
||||
self.env.filters['adv_format'] = self._adv_format
|
||||
self.env.filters['dict_ext'] = self._dict_ext
|
||||
self.env.filters['highlight'] = self.highlight
|
||||
# custom tests
|
||||
self.env.tests['in_the_past'] = self._in_the_past
|
||||
|
||||
|
|
@ -511,6 +515,27 @@ class Renderer(object):
|
|||
ret.update(extra_dict)
|
||||
return ret
|
||||
|
||||
def highlight(self, code, lexer_name=None, lexer_opts=None, html_fmt_opts=None):
|
||||
"""Do syntax highlighting on code
|
||||
|
||||
under the hood, pygments is used, check its documentation for options possible values
|
||||
@param code(unicode): code or markup to highlight
|
||||
@param lexer_name(unicode, None): name of the lexer to use
|
||||
None to autodetect it
|
||||
@param html_fmt_opts(dict, None): kword arguments to use for HtmlFormatter
|
||||
@return (unicode): HTML markup with highlight classes
|
||||
"""
|
||||
if lexer_opts is None:
|
||||
lexer_opts = {}
|
||||
if html_fmt_opts is None:
|
||||
html_fmt_opts = {}
|
||||
if lexer_name is None:
|
||||
lexer = lexers.guess_lexer(code, **lexer_opts)
|
||||
else:
|
||||
lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts)
|
||||
formatter = formatters.HtmlFormatter(**html_fmt_opts)
|
||||
return safe(pygments.highlight(code, lexer, formatter))
|
||||
|
||||
## custom tests ##
|
||||
|
||||
def _in_the_past(self, timestamp):
|
||||
|
|
|
|||
Loading…
Reference in a new issue