From 290ad46003ca04a07f6bd8c39ef4301c48074fe6 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Wed, 3 Sep 2008 16:08:20 -0400 Subject: [CodeHilite] Fixed Ticket 16. Added a 'css_class' option to codehilite extension and cleaned up whitespace in code. Thanks Kjell Magne Fauske for the initial patch. --- markdown_extensions/codehilite.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'markdown_extensions') diff --git a/markdown_extensions/codehilite.py b/markdown_extensions/codehilite.py index edfcedb..32f7ca0 100644 --- a/markdown_extensions/codehilite.py +++ b/markdown_extensions/codehilite.py @@ -41,9 +41,11 @@ class CodeHilite: >>> code = CodeHilite(src = text) >>> html = code.hilite() - * src: Can be a string or any object with a .readline attribute. + * src: Source string or any object with a .readline attribute. - * linenos: (Boolen) Turns line numbering 'on' or 'off' (off by default). + * 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() @@ -53,10 +55,11 @@ class CodeHilite: """ - def __init__(self, src=None, linenos = False): + def __init__(self, src=None, linenos=False, css_class="codehilite"): self.src = src self.lang = None self.linenos = linenos + self.css_class = css_class def hilite(self): """ @@ -75,7 +78,8 @@ class CodeHilite: try: from pygments import highlight - from pygments.lexers import get_lexer_by_name, guess_lexer, TextLexer + from pygments.lexers import get_lexer_by_name, guess_lexer, \ + TextLexer from pygments.formatters import HtmlFormatter except ImportError: # just escape and pass through @@ -83,7 +87,8 @@ class CodeHilite: if self.linenos: txt = self._number(txt) else : - txt = '
%s
\n'% txt + txt = '
%s
\n'% \ + (txt, self.css_class) return txt else: try: @@ -93,7 +98,8 @@ class CodeHilite: lexer = guess_lexer(self.src) except ValueError: lexer = TextLexer() - formatter = HtmlFormatter(linenos=self.linenos, cssclass="codehilite") + formatter = HtmlFormatter(linenos=self.linenos, + cssclass=self.css_class) return highlight(self.src, lexer, formatter) def _escape(self, txt): @@ -144,9 +150,11 @@ class CodeHilite: fl = lines.pop(0) c = re.compile(r''' - (?:(?:::+)|(?P[#]!)) #shebang or 2 or more colons - (?P(?:/\w+)*[/ ])? # zero or 1 path ending in either a / or a single space - (?P\w*) # the language (a single / or space before lang is a path) + (?:(?:::+)|(?P[#]!)) # Shebang or 2 or more colons. + (?P(?:/\w+)*[/ ])? # Zero or 1 path ending in either + # a / or a single space. + (?P\w*) # The language (a single / or + # space before lang is a path). ''', re.VERBOSE) # search first line for shebang m = c.search(fl) @@ -175,7 +183,9 @@ class CodeHiliteExtention(markdown.Extension): def __init__(self, configs): # define default configs self.config = { - 'force_linenos' : [False, "Force line numbers - Default: False"] + 'force_linenos' : [False, "Force line numbers - Default: False"], + 'css_class' : ["codehilite", + "Set class name for wrapper
- Default: codehilite"], } # Override defaults with user settings @@ -202,7 +212,8 @@ class CodeHiliteExtention(markdown.Extension): detabbed, theRest = md.blockGuru.detectTabbed(lines) text = "\n".join(detabbed).rstrip()+"\n" - code = CodeHilite(text, linenos=self.config['force_linenos'][0]) + code = CodeHilite(text, linenos=self.config['force_linenos'][0], + css_class=self.config['css_class'][0]) placeholder = md.htmlStash.store(code.hilite(), safe=True) # This wrapping p element will be removed when inserting raw html p = markdown.etree.SubElement(parent_elem, 'p') -- cgit v1.2.3