From 9f6b45f8944a2d91041e941da609e5ac09373e22 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 5 Feb 2015 22:23:52 -0500 Subject: 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. --- docs/extensions/code_hilite.txt | 11 ++++++++++- docs/release-2.6.txt | 12 ++++++++++++ markdown/extensions/codehilite.py | 17 ++++++++++------- tests/test_extensions.py | 11 +++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/docs/extensions/code_hilite.txt b/docs/extensions/code_hilite.txt index c775df7..92f8c12 100644 --- a/docs/extensions/code_hilite.txt +++ b/docs/extensions/code_hilite.txt @@ -31,7 +31,7 @@ language. When that fails, the code block will display as un-highlighted code. [documentation]: http://pygments.org/docs !!! Note - The css and/or javascript is not included as part of this extension + The css and/or JavaScript is not included as part of this extension but must be provided by the end user. The Pygments project provides default css styles which you may find to be a useful starting point. @@ -165,3 +165,12 @@ The following options are provided to configure the output: * **`noclasses`**: Use inline styles instead of CSS classes. Defaults to `False`. + +* **`use_pygments`**: + Defaults to `True`. Set to `False` to disable the use of Pygments. + If a language is defined for a code block, it will be assigned to the + `` tag as a class in the manner suggested by the [HTML5 spec][spec] + (alternate output will not be entertained) and might be used by a JavaScript + library in the browser to highlight the code block. + +[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element diff --git a/docs/release-2.6.txt b/docs/release-2.6.txt index 783837e..030d890 100644 --- a/docs/release-2.6.txt +++ b/docs/release-2.6.txt @@ -202,6 +202,18 @@ worked on PyPy for some time, it is now officially supported and tested on PyPy. [TOC]: extensions/toc.html +* The [CodeHilite][ch] Extension has gained a new config option: `use_pygments`. + The option is `True` by default, however, it allows one to turn off Pygments code + highlighting (set to `False`) while preserving the language detection features of + the extension. Note that Pygments language detection is not used as that would 'use + Pygments`. If a language is defined for a code block, it will be assigned to the + `` tag as a class in the manner suggested by the [HTML5 spec][spec] + (alternate output will not be entertained) and might be used by a JavaScript + library in the browser to highlight the code block. + +[ch]: extensions/code_hilite.html +[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element + * Test coverage has been improved including running [flake8]. While those changes will not directly effect end users, the code is being better tested which will benefit everyone. diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index f1e51f9..0657c37 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -75,7 +75,7 @@ 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): + noclasses=False, tab_length=4, hl_lines=None, use_pygments=True): self.src = src self.lang = lang self.linenums = linenums @@ -85,6 +85,7 @@ class CodeHilite(object): self.noclasses = noclasses self.tab_length = tab_length self.hl_lines = hl_lines or [] + self.use_pygments = use_pygments def hilite(self): """ @@ -102,7 +103,7 @@ class CodeHilite(object): if self.lang is None: self._parseHeader() - if pygments: + if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: @@ -211,7 +212,8 @@ class HiliteTreeprocessor(Treeprocessor): css_class=self.config['css_class'], style=self.config['pygments_style'], noclasses=self.config['noclasses'], - tab_length=self.markdown.tab_length + tab_length=self.markdown.tab_length, + use_pygments=self.config['use_pygments'] ) placeholder = self.markdown.htmlStash.store(code.hilite(), safe=True) @@ -231,9 +233,6 @@ class CodeHiliteExtension(Extension): self.config = { 'linenums': [None, "Use lines numbers. True=yes, False=no, None=auto"], - 'force_linenos': [False, - "Depreciated! Use 'linenums' instead. Force " - "line numbers - Default: False"], 'guess_lang': [True, "Automatic language detection - Default: True"], 'css_class': ["codehilite", @@ -244,7 +243,11 @@ class CodeHiliteExtension(Extension): '(Colorscheme) - Default: default'], 'noclasses': [False, 'Use inline styles instead of CSS classes - ' - 'Default false'] + 'Default false'], + 'use_pygments': [True, + 'Use Pygments to Highlight code blocks. ' + 'Disable if using a JavaScript library. ' + 'Default: True'] } super(CodeHiliteExtension, self).__init__(*args, **kwargs) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 6642921..6a57878 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -226,6 +226,17 @@ class TestCodeHilite(unittest.TestCase): '#line 3' ) + def testUsePygmentsFalse(self): + text = '\t:::Python\n\t# A Code Comment' + md = markdown.Markdown( + extensions=[markdown.extensions.codehilite.CodeHiliteExtension(use_pygments=False)] + ) + self.assertEqual( + md.convert(text), + '
# A Code Comment'
+            '
' + ) + class TestFencedCode(unittest.TestCase): """ Test fenced_code extension. """ -- cgit v1.2.3