Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions markdown/extensions/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import absolute_import
from __future__ import unicode_literals
import inspect
from . import Extension
from ..treeprocessors import Treeprocessor

Expand Down Expand Up @@ -75,7 +76,8 @@ class CodeHilite(object):

def __init__(self, src=None, linenums=None, guess_lang=True,
css_class="codehilite", lang=None, style='default',
noclasses=False, tab_length=4, hl_lines=None, use_pygments=True):
noclasses=False, tab_length=4, hl_lines=None, use_pygments=True,
pygments_formatter='html'):
self.src = src
self.lang = lang
self.linenums = linenums
Expand All @@ -86,6 +88,7 @@ def __init__(self, src=None, linenums=None, guess_lang=True,
self.tab_length = tab_length
self.hl_lines = hl_lines or []
self.use_pygments = use_pygments
self.pygments_formatter = pygments_formatter

def hilite(self):
"""
Expand Down Expand Up @@ -114,12 +117,19 @@ def hilite(self):
lexer = get_lexer_by_name('text')
except ValueError:
lexer = get_lexer_by_name('text')
formatter = get_formatter_by_name('html',
linenos=self.linenums,
cssclass=self.css_class,
style=self.style,
noclasses=self.noclasses,
hl_lines=self.hl_lines)
if inspect.isclass(self.pygments_formatter):
formatter = self.pygments_formatter(linenos=self.linenums,
cssclass=self.css_class,
style=self.style,
noclasses=self.noclasses,
hl_lines=self.hl_lines)
else:
formatter = get_formatter_by_name(self.pygments_formatter,
linenos=self.linenums,
cssclass=self.css_class,
style=self.style,
noclasses=self.noclasses,
hl_lines=self.hl_lines)
return highlight(self.src, lexer, formatter)
else:
# just escape and build markup usable by JS highlighting libs
Expand Down Expand Up @@ -213,7 +223,8 @@ def run(self, root):
style=self.config['pygments_style'],
noclasses=self.config['noclasses'],
tab_length=self.markdown.tab_length,
use_pygments=self.config['use_pygments']
use_pygments=self.config['use_pygments'],
pygments_formatter=self.config['pygments_formatter']
)
placeholder = self.markdown.htmlStash.store(code.hilite(),
safe=True)
Expand Down Expand Up @@ -247,7 +258,11 @@ def __init__(self, *args, **kwargs):
'use_pygments': [True,
'Use Pygments to Highlight code blocks. '
'Disable if using a JavaScript library. '
'Default: True']
'Default: True'],
'pygments_formatter': ['html',
'Use a specific Formatter for '
'Pygments Highlighting.'
'Default: html']
}

super(CodeHiliteExtension, self).__init__(*args, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def run(self, lines):
use_pygments=self.codehilite_conf['use_pygments'][0],
lang=(m.group('lang') or None),
noclasses=self.codehilite_conf['noclasses'][0],
hl_lines=parse_hl_lines(m.group('hl_lines'))
hl_lines=parse_hl_lines(m.group('hl_lines')),
pygments_formatter=self.codehilite_conf['pygments_formatter'][0]
)

code = highliter.hilite()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ def testBasicCodeHilite(self):
'</code></pre>'
)

def testCustomFormatter(self):
text = '\t# A Code Comment'
# Use 'null' formatter (meaning no highlighting)
md = markdown.Markdown(
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(
pygments_formatter='null')])
if self.has_pygments:
# Pygments can use random lexer here as we did not specify the language
self.assertStartsWith('<p># A Code Comment', md.convert(text))
else:
self.assertEqual(
md.convert(text),
'<pre class="codehilite"><code># A Code Comment'
'</code></pre>'
)

def testLinenumsTrue(self):
text = '\t# A Code Comment'
md = markdown.Markdown(
Expand Down