aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2008-09-03 16:08:20 -0400
committerWaylan Limberg <waylan@gmail.com>2008-09-03 16:08:20 -0400
commit290ad46003ca04a07f6bd8c39ef4301c48074fe6 (patch)
treef61d59f94269f1d1bfaa2af9f56af94e5a2f78b0
parent6408b4b814612bc8d1d0f9b23b68c32876e7c797 (diff)
downloadmarkdown-290ad46003ca04a07f6bd8c39ef4301c48074fe6.tar.gz
markdown-290ad46003ca04a07f6bd8c39ef4301c48074fe6.tar.bz2
markdown-290ad46003ca04a07f6bd8c39ef4301c48074fe6.zip
[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.
-rw-r--r--docs/AUTHORS1
-rw-r--r--markdown_extensions/codehilite.py33
2 files changed, 23 insertions, 11 deletions
diff --git a/docs/AUTHORS b/docs/AUTHORS
index 00abb15..b1633c7 100644
--- a/docs/AUTHORS
+++ b/docs/AUTHORS
@@ -29,6 +29,7 @@ Jeff Balogh
Sergej Chodarev
Chris Clark
Tiago Cogumbreiro
+Kjell Magne Fauske
G. Clark Haynes
Daniel Krech
Steward Midwinter
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 = '<div class="codehilite"><pre>%s</pre></div>\n'% txt
+ txt = '<div class="%s"><pre>%s</pre></div>\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>[#]!)) #shebang or 2 or more colons
- (?P<path>(?:/\w+)*[/ ])? # zero or 1 path ending in either a / or a single space
- (?P<lang>\w*) # the language (a single / or space before lang is a path)
+ (?:(?:::+)|(?P<shebang>[#]!)) # Shebang or 2 or more colons.
+ (?P<path>(?:/\w+)*[/ ])? # Zero or 1 path ending in either
+ # a / or a single space.
+ (?P<lang>\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 <div> - 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')