Skip to content

Commit a76480a

Browse files
author
Waylan Limberg
committed
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.
1 parent ef7b35a commit a76480a

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

‎markdown/extensions/codehilite.py‎

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ def hilite(self):
7777
TextLexer
7878
from pygments.formatters import HtmlFormatter
7979
except ImportError:
80-
# just escape and pass through
80+
# just escape and build markup usable by JS highlighting libs
8181
txt = self._escape(self.src)
82+
classes = []
83+
if self.lang:
84+
classes.append('language-%s' % self.lang)
8285
if self.linenos:
83-
txt = self._number(txt)
84-
else :
85-
txt = '<div class="%s"><pre>%s</pre></div>\n'% \
86-
(self.css_class, txt)
87-
return txt
86+
classes.append('linenums')
87+
class_str = ''
88+
if classes:
89+
class_str = ' class="%s"' % ' '.join(classes)
90+
return '<pre class="%s"><code%s>%s</code></pre>\n'% \
91+
(self.css_class, class_str, txt)
8892
else:
8993
try:
9094
lexer = get_lexer_by_name(self.lang)
@@ -107,28 +111,11 @@ def _escape(self, txt):
107111
txt = txt.replace('"', '&quot;')
108112
return txt
109113

110-
def _number(self, txt):
111-
""" Use <ol> for line numbering """
112-
# Fix Whitespace
113-
txt = txt.replace('\t', ' '*self.tab_length)
114-
txt = txt.replace(" "*4, "&nbsp; &nbsp; ")
115-
txt = txt.replace(" "*3, "&nbsp; &nbsp;")
116-
txt = txt.replace(" "*2, "&nbsp; ")
117-
118-
# Add line numbers
119-
lines = txt.splitlines()
120-
txt = '<div class="codehilite"><pre><ol>\n'
121-
for line in lines:
122-
txt += '\t<li>%s</li>\n'% line
123-
txt += '</ol></pre></div>\n'
124-
return txt
125-
126-
127114
def _getLang(self):
128115
"""
129-
Determines language of a code block from shebang lines and whether said
116+
Determines language of a code block from shebang line and whether said
130117
line should be removed or left in place. If the sheband line contains a
131-
path (even a single /) then it is assumed to be a real shebang lines and
118+
path (even a single /) then it is assumed to be a real shebang line and
132119
left alone. However, if no path is given (e.i.: #!python or :::python)
133120
then it is assumed to be a mock shebang for language identifitation of a
134121
code fragment and removed from the code block prior to processing for

0 commit comments

Comments
 (0)