Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions markdown/extensions/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class CodeHilite(object):

def __init__(self, src=None, linenums=None, guess_lang=True,
css_class="codehilite", lang=None, style='default',
noclasses=False, tab_length=4, hl_lines=None, use_pygments=True):
noclasses=False, tab_length=4, hl_lines=None, linenostart=1,
use_pygments=True):
self.linenostart = linenostart
self.src = src
self.lang = lang
self.linenums = linenums
Expand Down Expand Up @@ -119,7 +121,8 @@ def hilite(self):
cssclass=self.css_class,
style=self.style,
noclasses=self.noclasses,
hl_lines=self.hl_lines)
hl_lines=self.hl_lines,
linenostart=self.linenostart)
return highlight(self.src, lexer, formatter)
else:
# just escape and build markup usable by JS highlighting libs
Expand Down Expand Up @@ -170,6 +173,7 @@ def _parseHeader(self):
\s* # Arbitrary whitespace
# Optional highlight lines, single- or double-quote-delimited
(hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?
(linenostart=(?P<linenostart>[0-9]*))?
''', re.VERBOSE)
# search first line for shebang
m = c.search(fl)
Expand All @@ -187,6 +191,10 @@ def _parseHeader(self):
self.linenums = True

self.hl_lines = parse_hl_lines(m.group('hl_lines'))
linenostart = m.group('linenostart')
if linenostart:
# this will allow starting line 0
self.linenostart = int(linenostart)
else:
# No match
lines.insert(0, fl)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ def testUsePygmentsFalse(self):
'</code></pre>'
)

def testUseLineNoStart(self):
if not self.has_pygments:
self.skipTest("linenos only supported with Pygments")
text = '\t:::Python linenostart=42\n\t# A Code Comment'
md = markdown.Markdown(extensions=[
markdown.extensions.codehilite.CodeHiliteExtension(linenums=True)
])
self.assertIn("42", md.convert(text))


class TestFencedCode(unittest.TestCase):
""" Test fenced_code extension. """
Expand Down