diff options
author | Lucas van Dijk <luckyluke56@gmail.com> | 2010-01-25 21:11:26 +0100 |
---|---|---|
committer | Lucas van Dijk <luckyluke56@gmail.com> | 2010-01-25 21:11:26 +0100 |
commit | ee6fb73b160721e666e72a0921135d6abae19fa4 (patch) | |
tree | 29a40435c5c2df41ce7d935b32217c5c0996025f /markdown/extensions/codehilite.py | |
parent | 4e7d92f8d8d58039f84c4677a9d69be3c20a16ef (diff) | |
download | markdown-ee6fb73b160721e666e72a0921135d6abae19fa4.tar.gz markdown-ee6fb73b160721e666e72a0921135d6abae19fa4.tar.bz2 markdown-ee6fb73b160721e666e72a0921135d6abae19fa4.zip |
Refactored codehilite and fenced code, so now fenced code will also be highlited when codehilite extension is enabled
Diffstat (limited to 'markdown/extensions/codehilite.py')
-rw-r--r-- | markdown/extensions/codehilite.py | 90 |
1 files changed, 51 insertions, 39 deletions
diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index c5d496b..b9e1760 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -10,9 +10,9 @@ Copyright 2006-2008 [Waylan Limberg](http://achinghead.com/). Project website: <http://www.freewisdom.org/project/python-markdown/CodeHilite> Contact: markdown@freewisdom.org - + License: BSD (see ../docs/LICENSE for details) - + Dependencies: * [Python 2.3+](http://python.org/) * [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/) @@ -38,41 +38,45 @@ class CodeHilite: Basic Usage: >>> code = CodeHilite(src = 'some text') >>> html = code.hilite() - + * src: Source string or any object with a .readline attribute. - + * linenos: (Boolen) Turn line numbering 'on' or 'off' (off by default). * css_class: Set class name of wrapper div ('codehilite' by default). - + Low Level Usage: >>> code = CodeHilite() >>> code.src = 'some text' # String or anything with a .readline attr. >>> code.linenos = True # True or False; Turns line numbering on or of. >>> html = code.hilite() - + """ - def __init__(self, src=None, linenos=False, css_class="codehilite"): + def __init__(self, src=None, linenos=False, css_class="codehilite", + lang=None, style='default', noclasses=False): self.src = src - self.lang = None + self.lang = lang self.linenos = linenos self.css_class = css_class + self.style = style + self.noclasses = noclasses def hilite(self): """ - Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with - optional line numbers. The output should then be styled with css to - your liking. No styles are applied by default - only styling hooks - (i.e.: <span class="k">). + Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with + optional line numbers. The output should then be styled with css to + your liking. No styles are applied by default - only styling hooks + (i.e.: <span class="k">). returns : A string of html. - + """ self.src = self.src.strip('\n') - - self._getLang() + + if self.lang == None: + self._getLang() try: from pygments import highlight @@ -96,8 +100,10 @@ class CodeHilite: lexer = guess_lexer(self.src) except ValueError: lexer = TextLexer() - formatter = HtmlFormatter(linenos=self.linenos, - cssclass=self.css_class) + formatter = HtmlFormatter(linenos=self.linenos, + cssclass=self.css_class, + style=self.style, + noclasses=self.noclasses) return highlight(self.src, lexer, formatter) def _escape(self, txt): @@ -114,8 +120,8 @@ class CodeHilite: txt = txt.replace('\t', ' '*TAB_LENGTH) txt = txt.replace(" "*4, " ") txt = txt.replace(" "*3, " ") - txt = txt.replace(" "*2, " ") - + txt = txt.replace(" "*2, " ") + # Add line numbers lines = txt.splitlines() txt = '<div class="codehilite"><pre><ol>\n' @@ -126,31 +132,31 @@ class CodeHilite: def _getLang(self): - """ + """ Determines language of a code block from shebang lines and whether said line should be removed or left in place. If the sheband line contains a path (even a single /) then it is assumed to be a real shebang lines and - left alone. However, if no path is given (e.i.: #!python or :::python) + left alone. However, if no path is given (e.i.: #!python or :::python) then it is assumed to be a mock shebang for language identifitation of a - code fragment and removed from the code block prior to processing for - code highlighting. When a mock shebang (e.i: #!python) is found, line - numbering is turned on. When colons are found in place of a shebang - (e.i.: :::python), line numbering is left in the current state - off + code fragment and removed from the code block prior to processing for + code highlighting. When a mock shebang (e.i: #!python) is found, line + numbering is turned on. When colons are found in place of a shebang + (e.i.: :::python), line numbering is left in the current state - off by default. - + """ import re - + #split text into lines lines = self.src.split("\n") #pull first line to examine fl = lines.pop(0) - + c = re.compile(r''' (?:(?:::+)|(?P<shebang>[#]!)) # Shebang or 2 or more colons. - (?P<path>(?:/\w+)*[/ ])? # Zero or 1 path - (?P<lang>[\w+-]*) # The language + (?P<path>(?:/\w+)*[/ ])? # Zero or 1 path + (?P<lang>[\w+-]*) # The language ''', re.VERBOSE) # search first line for shebang m = c.search(fl) @@ -169,7 +175,7 @@ class CodeHilite: else: # No match lines.insert(0, fl) - + self.src = "\n".join(lines).strip("\n") @@ -184,14 +190,16 @@ class HiliteTreeprocessor(markdown.treeprocessors.Treeprocessor): for block in blocks: children = block.getchildren() if len(children) == 1 and children[0].tag == 'code': - code = CodeHilite(children[0].text, + code = CodeHilite(children[0].text, linenos=self.config['force_linenos'][0], - css_class=self.config['css_class'][0]) - placeholder = self.markdown.htmlStash.store(code.hilite(), + css_class=self.config['css_class'][0], + style=self.config['pygments_style'][0], + noclasses=self.config['noclasses'][0]) + placeholder = self.markdown.htmlStash.store(code.hilite(), safe=True) # Clear codeblock in etree instance block.clear() - # Change to p element which will later + # Change to p element which will later # be removed when inserting raw html block.tag = 'p' block.text = placeholder @@ -204,19 +212,23 @@ class CodeHiliteExtension(markdown.Extension): # define default configs self.config = { 'force_linenos' : [False, "Force line numbers - Default: False"], - 'css_class' : ["codehilite", + 'css_class' : ["codehilite", "Set class name for wrapper <div> - Default: codehilite"], + 'pygments_style' : ['tango', 'Pygments HTML Formatter Style (Colorscheme) - Default: tango'], + 'noclasses': [False, 'Use inline styles instead of CSS classes - Default false'] } - + # Override defaults with user settings for key, value in configs: - self.setConfig(key, value) + self.setConfig(key, value) def extendMarkdown(self, md, md_globals): """ Add HilitePostprocessor to Markdown instance. """ hiliter = HiliteTreeprocessor(md) hiliter.config = self.config - md.treeprocessors.add("hilite", hiliter, "_begin") + md.treeprocessors.add("hilite", hiliter, "_begin") + + md.registerExtension(self) def makeExtension(configs={}): |