diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index 0657c3768..6a2c0489a 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -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 @@ -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 @@ -170,6 +173,7 @@ def _parseHeader(self): \s* # Arbitrary whitespace # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P"|')(?P.*?)(?P=quot))? + (linenostart=(?P[0-9]*))? ''', re.VERBOSE) # search first line for shebang m = c.search(fl) @@ -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) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 38f0be3c9..9e8ceee39 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -236,6 +236,15 @@ def testUsePygmentsFalse(self): '' ) + 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. """