Skip to content

Commit 9f6b45f

Browse files
committed
Added a 'use_pygments' config option to CodeHilite.
Fixes #386. I'm doing this against my better judgement. The only reason is that I'm using the HTML format suggested by the HTML5 Spec and will simply not consider any alternate output. If a JavaScript library requires something else, to bad. I don't care. That library should support the format suggested by the spec or I'm not interested in it. If you want something else then you can create your own extension which does whatever you want.
1 parent 75855cd commit 9f6b45f

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

docs/extensions/code_hilite.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ language. When that fails, the code block will display as un-highlighted code.
3131
[documentation]: http://pygments.org/docs
3232

3333
!!! Note
34-
The css and/or javascript is not included as part of this extension
34+
The css and/or JavaScript is not included as part of this extension
3535
but must be provided by the end user. The Pygments project provides
3636
default css styles which you may find to be a useful starting point.
3737

@@ -165,3 +165,12 @@ The following options are provided to configure the output:
165165

166166
* **`noclasses`**:
167167
Use inline styles instead of CSS classes. Defaults to `False`.
168+
169+
* **`use_pygments`**:
170+
Defaults to `True`. Set to `False` to disable the use of Pygments.
171+
If a language is defined for a code block, it will be assigned to the
172+
`<code>` tag as a class in the manner suggested by the [HTML5 spec][spec]
173+
(alternate output will not be entertained) and might be used by a JavaScript
174+
library in the browser to highlight the code block.
175+
176+
[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element

docs/release-2.6.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ worked on PyPy for some time, it is now officially supported and tested on PyPy.
202202

203203
[TOC]: extensions/toc.html
204204

205+
* The [CodeHilite][ch] Extension has gained a new config option: `use_pygments`.
206+
The option is `True` by default, however, it allows one to turn off Pygments code
207+
highlighting (set to `False`) while preserving the language detection features of
208+
the extension. Note that Pygments language detection is not used as that would 'use
209+
Pygments`. If a language is defined for a code block, it will be assigned to the
210+
`<code>` tag as a class in the manner suggested by the [HTML5 spec][spec]
211+
(alternate output will not be entertained) and might be used by a JavaScript
212+
library in the browser to highlight the code block.
213+
214+
[ch]: extensions/code_hilite.html
215+
[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
216+
205217
* Test coverage has been improved including running [flake8]. While those changes
206218
will not directly effect end users, the code is being better tested which will
207219
benefit everyone.

markdown/extensions/codehilite.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CodeHilite(object):
7575

7676
def __init__(self, src=None, linenums=None, guess_lang=True,
7777
css_class="codehilite", lang=None, style='default',
78-
noclasses=False, tab_length=4, hl_lines=None):
78+
noclasses=False, tab_length=4, hl_lines=None, use_pygments=True):
7979
self.src = src
8080
self.lang = lang
8181
self.linenums = linenums
@@ -85,6 +85,7 @@ def __init__(self, src=None, linenums=None, guess_lang=True,
8585
self.noclasses = noclasses
8686
self.tab_length = tab_length
8787
self.hl_lines = hl_lines or []
88+
self.use_pygments = use_pygments
8889

8990
def hilite(self):
9091
"""
@@ -102,7 +103,7 @@ def hilite(self):
102103
if self.lang is None:
103104
self._parseHeader()
104105

105-
if pygments:
106+
if pygments and self.use_pygments:
106107
try:
107108
lexer = get_lexer_by_name(self.lang)
108109
except ValueError:
@@ -211,7 +212,8 @@ def run(self, root):
211212
css_class=self.config['css_class'],
212213
style=self.config['pygments_style'],
213214
noclasses=self.config['noclasses'],
214-
tab_length=self.markdown.tab_length
215+
tab_length=self.markdown.tab_length,
216+
use_pygments=self.config['use_pygments']
215217
)
216218
placeholder = self.markdown.htmlStash.store(code.hilite(),
217219
safe=True)
@@ -231,9 +233,6 @@ def __init__(self, *args, **kwargs):
231233
self.config = {
232234
'linenums': [None,
233235
"Use lines numbers. True=yes, False=no, None=auto"],
234-
'force_linenos': [False,
235-
"Depreciated! Use 'linenums' instead. Force "
236-
"line numbers - Default: False"],
237236
'guess_lang': [True,
238237
"Automatic language detection - Default: True"],
239238
'css_class': ["codehilite",
@@ -244,7 +243,11 @@ def __init__(self, *args, **kwargs):
244243
'(Colorscheme) - Default: default'],
245244
'noclasses': [False,
246245
'Use inline styles instead of CSS classes - '
247-
'Default false']
246+
'Default false'],
247+
'use_pygments': [True,
248+
'Use Pygments to Highlight code blocks. '
249+
'Disable if using a JavaScript library. '
250+
'Default: True']
248251
}
249252

250253
super(CodeHiliteExtension, self).__init__(*args, **kwargs)

tests/test_extensions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ def testHighlightLinesWithColon(self):
226226
'#line 3</code></pre>'
227227
)
228228

229+
def testUsePygmentsFalse(self):
230+
text = '\t:::Python\n\t# A Code Comment'
231+
md = markdown.Markdown(
232+
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(use_pygments=False)]
233+
)
234+
self.assertEqual(
235+
md.convert(text),
236+
'<pre class="codehilite"><code class="language-python"># A Code Comment'
237+
'</code></pre>'
238+
)
239+
229240

230241
class TestFencedCode(unittest.TestCase):
231242
""" Test fenced_code extension. """

0 commit comments

Comments
 (0)