Allow using different Pygments formatters.

This commit is contained in:
Georg Brandl 2008-11-30 20:29:34 +01:00
parent 50339493c6
commit c450bab147
2 changed files with 32 additions and 10 deletions

View File

@ -82,7 +82,12 @@ if sys.version_info < (2, 5):
class PygmentsBridge(object): class PygmentsBridge(object):
def __init__(self, dest='html', stylename='sphinx'): # Set these attributes if you want to have different Pygments formatters
# than the default ones.
html_formatter = HtmlFormatter
latex_formatter = LatexFormatter
def __init__(self, dest='html', stylename='sphinx', ):
self.dest = dest self.dest = dest
if not pygments: if not pygments:
return return
@ -93,11 +98,14 @@ class PygmentsBridge(object):
style = getattr(__import__(module, None, None, ['__name__']), stylename) style = getattr(__import__(module, None, None, ['__name__']), stylename)
else: else:
style = get_style_by_name(stylename) style = get_style_by_name(stylename)
self.hfmter = {False: HtmlFormatter(style=style), if dest == 'html':
True: HtmlFormatter(style=style, linenos=True)} self.fmter = {False: self.html_formatter(style=style),
self.lfmter = {False: LatexFormatter(style=style, commandprefix='PYG'), True: self.html_formatter(style=style, linenos=True)}
True: LatexFormatter(style=style, linenos=True, else:
commandprefix='PYG')} self.fmter = {False: self.latex_formatter(style=style,
commandprefix='PYG'),
True: self.latex_formatter(style=style, linenos=True,
commandprefix='PYG')}
def unhighlighted(self, source): def unhighlighted(self, source):
if self.dest == 'html': if self.dest == 'html':
@ -171,9 +179,9 @@ class PygmentsBridge(object):
lexer.add_filter('raiseonerror') lexer.add_filter('raiseonerror')
try: try:
if self.dest == 'html': if self.dest == 'html':
return highlight(source, lexer, self.hfmter[bool(linenos)]) return highlight(source, lexer, self.fmter[bool(linenos)])
else: else:
hlsource = highlight(source, lexer, self.lfmter[bool(linenos)]) hlsource = highlight(source, lexer, self.fmter[bool(linenos)])
return hlsource.translate(tex_hl_escape_map) return hlsource.translate(tex_hl_escape_map)
except ErrorToken: except ErrorToken:
# this is most probably not the selected language, # this is most probably not the selected language,
@ -187,9 +195,9 @@ class PygmentsBridge(object):
# no HTML styles needed # no HTML styles needed
return '' return ''
if self.dest == 'html': if self.dest == 'html':
return self.hfmter[0].get_style_defs() return self.fmter[0].get_style_defs()
else: else:
styledefs = self.lfmter[0].get_style_defs() styledefs = self.fmter[0].get_style_defs()
# workaround for Pygments < 0.12 # workaround for Pygments < 0.12
if styledefs.startswith('\\newcommand\\at{@}'): if styledefs.startswith('\\newcommand\\at{@}'):
styledefs += _LATEX_STYLES styledefs += _LATEX_STYLES

View File

@ -13,6 +13,7 @@ from util import *
from pygments.lexer import RegexLexer from pygments.lexer import RegexLexer
from pygments.token import Text, Name from pygments.token import Text, Name
from pygments.formatters.html import HtmlFormatter
from sphinx.highlighting import PygmentsBridge from sphinx.highlighting import PygmentsBridge
@ -27,6 +28,10 @@ class MyLexer(RegexLexer):
], ],
} }
class MyFormatter(HtmlFormatter):
def format(self, tokensource, outfile):
outfile.write('test')
@with_app() @with_app()
def test_add_lexer(app): def test_add_lexer(app):
@ -35,3 +40,12 @@ def test_add_lexer(app):
bridge = PygmentsBridge('html') bridge = PygmentsBridge('html')
ret = bridge.highlight_block('ab', 'test') ret = bridge.highlight_block('ab', 'test')
assert '<span class="n">a</span>b' in ret assert '<span class="n">a</span>b' in ret
def test_set_formatter():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('foo', 'python')
assert ret == 'test'
finally:
PygmentsBridge.html_formatter = HtmlFormatter