diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-04-07 02:52:09 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-04-07 02:52:09 -0400 |
commit | a76480afbea34043b75363f190253ad1fd51c047 (patch) | |
tree | b127a5723ba46cf1db1227946c3f95c81f0abd8b | |
parent | ef7b35ab7ddc8ba174e27f063208dbe38ed02e27 (diff) | |
download | markdown-a76480afbea34043b75363f190253ad1fd51c047.tar.gz markdown-a76480afbea34043b75363f190253ad1fd51c047.tar.bz2 markdown-a76480afbea34043b75363f190253ad1fd51c047.zip |
Cleaned up CodeHilite extension. When pygments is not available provide simpler markup which should allow for the use of JavaScript Highlighting libraries. In other words, no pygments like <div> and no <ol> for line numbering. Just a <pre><code> block with classes assinged to them. If people want fancier stuff, they can use JavaScript or create their own extension.
-rw-r--r-- | markdown/extensions/codehilite.py | 37 |
1 files changed, 12 insertions, 25 deletions
diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index e74493c..f7a9b8c 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -77,14 +77,18 @@ class CodeHilite: TextLexer from pygments.formatters import HtmlFormatter except ImportError: - # just escape and pass through + # just escape and build markup usable by JS highlighting libs txt = self._escape(self.src) + classes = [] + if self.lang: + classes.append('language-%s' % self.lang) if self.linenos: - txt = self._number(txt) - else : - txt = '<div class="%s"><pre>%s</pre></div>\n'% \ - (self.css_class, txt) - return txt + classes.append('linenums') + class_str = '' + if classes: + class_str = ' class="%s"' % ' '.join(classes) + return '<pre class="%s"><code%s>%s</code></pre>\n'% \ + (self.css_class, class_str, txt) else: try: lexer = get_lexer_by_name(self.lang) @@ -107,28 +111,11 @@ class CodeHilite: txt = txt.replace('"', '"') return txt - def _number(self, txt): - """ Use <ol> for line numbering """ - # Fix Whitespace - txt = txt.replace('\t', ' '*self.tab_length) - txt = txt.replace(" "*4, " ") - txt = txt.replace(" "*3, " ") - txt = txt.replace(" "*2, " ") - - # Add line numbers - lines = txt.splitlines() - txt = '<div class="codehilite"><pre><ol>\n' - for line in lines: - txt += '\t<li>%s</li>\n'% line - txt += '</ol></pre></div>\n' - return txt - - def _getLang(self): """ - Determines language of a code block from shebang lines and whether said + Determines language of a code block from shebang line 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 + path (even a single /) then it is assumed to be a real shebang line and 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 |