aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRob McBroom <github@skurfer.com>2011-06-19 15:02:08 -0400
committerRob McBroom <github@skurfer.com>2011-06-19 15:02:08 -0400
commit1dfe89c5c4a34ca23dd7a5b3207bb406f76c1833 (patch)
tree222cac3ea503fd71aabe4fcdd9173cef228decfe
parentc2cca0cefe6fb9977a23008af17641531e3b9e43 (diff)
downloadmarkdown-1dfe89c5c4a34ca23dd7a5b3207bb406f76c1833.tar.gz
markdown-1dfe89c5c4a34ca23dd7a5b3207bb406f76c1833.tar.bz2
markdown-1dfe89c5c4a34ca23dd7a5b3207bb406f76c1833.zip
allow language guessing to be disabled by passing a setting to CodeHilite - closes #24
-rw-r--r--docs/extensions/CodeHilite.txt7
-rw-r--r--markdown/extensions/codehilite.py20
-rw-r--r--markdown/extensions/fenced_code.py1
3 files changed, 24 insertions, 4 deletions
diff --git a/docs/extensions/CodeHilite.txt b/docs/extensions/CodeHilite.txt
index 482ad60..68288c8 100644
--- a/docs/extensions/CodeHilite.txt
+++ b/docs/extensions/CodeHilite.txt
@@ -111,3 +111,10 @@ to do so.
>>> html = markdown.markdown(text,
... ['codehilite(force_linenos=True)']
... )
+
+If you want to prevent Pygments from guessing the language, only highlighting
+blocks when you explicitly request it, set the `guess_lang` setting to 'False'.
+
+ >>> html = markdown.markdown(text,
+ ... ['codehilite(guess_lang=True)']
+ ... )
diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py
index cc64d6a..6f0da44 100644
--- a/markdown/extensions/codehilite.py
+++ b/markdown/extensions/codehilite.py
@@ -42,6 +42,8 @@ class CodeHilite:
* linenos: (Boolen) Turn line numbering 'on' or 'off' (off by default).
+ * guess_lang: (Boolen) Turn language auto-detection 'on' or 'off' (on by default).
+
* css_class: Set class name of wrapper div ('codehilite' by default).
Low Level Usage:
@@ -52,11 +54,13 @@ class CodeHilite:
"""
- def __init__(self, src=None, linenos=False, css_class="codehilite",
- lang=None, style='default', noclasses=False, tab_length=4):
+ def __init__(self, src=None, linenos=False, guess_lang=True,
+ css_class="codehilite", lang=None, style='default',
+ noclasses=False, tab_length=4):
self.src = src
self.lang = lang
self.linenos = linenos
+ self.guess_lang = guess_lang
self.css_class = css_class
self.style = style
self.noclasses = noclasses
@@ -75,7 +79,7 @@ class CodeHilite:
self.src = self.src.strip('\n')
- if self.lang == None:
+ if self.lang is None:
self._getLang()
if pygments:
@@ -83,7 +87,10 @@ class CodeHilite:
lexer = get_lexer_by_name(self.lang)
except ValueError:
try:
- lexer = guess_lexer(self.src)
+ if self.guess_lang:
+ lexer = guess_lexer(self.src)
+ else:
+ lexer = TextLexer()
except ValueError:
lexer = TextLexer()
formatter = HtmlFormatter(linenos=self.linenos,
@@ -169,6 +176,7 @@ class HiliteTreeprocessor(markdown.treeprocessors.Treeprocessor):
if len(children) == 1 and children[0].tag == 'code':
code = CodeHilite(children[0].text,
linenos=self.config['force_linenos'],
+ guess_lang=self.config['guess_lang'],
css_class=self.config['css_class'],
style=self.config['pygments_style'],
noclasses=self.config['noclasses'],
@@ -190,6 +198,7 @@ class CodeHiliteExtension(markdown.Extension):
# define default configs
self.config = {
'force_linenos' : [False, "Force line numbers - Default: False"],
+ 'guess_lang' : [True, "Automatic language detection - Default: True"],
'css_class' : ["codehilite",
"Set class name for wrapper <div> - Default: codehilite"],
'pygments_style' : ['default', 'Pygments HTML Formatter Style (Colorscheme) - Default: default'],
@@ -198,6 +207,9 @@ class CodeHiliteExtension(markdown.Extension):
# Override defaults with user settings
for key, value in configs:
+ # convert strings to booleans
+ if value == 'True': value = True
+ if value == 'False': value = False
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py
index 55e1773..3592d78 100644
--- a/markdown/extensions/fenced_code.py
+++ b/markdown/extensions/fenced_code.py
@@ -117,6 +117,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
if self.codehilite_conf:
highliter = CodeHilite(m.group('code'),
linenos=self.codehilite_conf['force_linenos'],
+ guess_lang=self.codehilite_conf['guess_lang'],
css_class=self.codehilite_conf['css_class'],
style=self.codehilite_conf['pygments_style'],
lang=(m.group('lang') or None),