aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@10gen.com>2014-01-06 22:10:51 -0500
committerA. Jesse Jiryu Davis <jesse@10gen.com>2014-01-06 22:10:51 -0500
commit436b420c1c6bb9db569d6ce4be2d06cff829aea9 (patch)
treebb15cbf1a860b9e161a290d782e358725eb3a25c
parentc18ce238e39c2ccac92da25a6429bdade0db4dff (diff)
downloadmarkdown-436b420c1c6bb9db569d6ce4be2d06cff829aea9.tar.gz
markdown-436b420c1c6bb9db569d6ce4be2d06cff829aea9.tar.bz2
markdown-436b420c1c6bb9db569d6ce4be2d06cff829aea9.zip
Support syntax for highlighted lines like: ```python hl_lines=“1 3”
-rw-r--r--markdown/extensions/codehilite.py11
-rw-r--r--markdown/extensions/fenced_code.py7
-rw-r--r--tests/test_extensions.py37
3 files changed, 47 insertions, 8 deletions
diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py
index d05d495..7f20efe 100644
--- a/markdown/extensions/codehilite.py
+++ b/markdown/extensions/codehilite.py
@@ -35,14 +35,14 @@ except ImportError:
def parse_hl_lines(expr):
"""Support our syntax for emphasizing certain lines of code.
- expr should be like '{1,2}' to emphasize lines 1 and 2 of a code block.
+ expr should be like '1 2' to emphasize lines 1 and 2 of a code block.
Returns a list of ints, the line numbers to emphasize.
"""
if not expr:
return []
try:
- return map(int, expr.strip('{}').split(','))
+ return map(int, expr.split())
except ValueError:
return []
@@ -151,7 +151,9 @@ class CodeHilite(object):
(e.i.: :::python), line numbering is left in the current state - off
by default.
- Also parses optional list of highlight lines, like :::python{1,3}
+ Also parses optional list of highlight lines, like:
+
+ :::python hl_lines="1 3"
"""
import re
@@ -165,7 +167,8 @@ class CodeHilite(object):
(?:(?:^::+)|(?P<shebang>^[#]!)) # Shebang or 2 or more colons
(?P<path>(?:/\w+)*[/ ])? # Zero or 1 path
(?P<lang>[\w+-]*) # The language
- (?P<hl_lines>\{.*?})? # Maybe hl_lines
+ \s* # Arbitrary whitespace
+ (hl_lines="(?P<hl_lines>.*?)")? # Maybe highlight lines
''', re.VERBOSE)
# search first line for shebang
m = c.search(fl)
diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py
index 64ff769..6ee6759 100644
--- a/markdown/extensions/fenced_code.py
+++ b/markdown/extensions/fenced_code.py
@@ -62,7 +62,7 @@ Optionally backticks instead of tildes as per how github's code block markdown i
If the codehighlite extension and Pygments are installed, lines can be highlighted:
>>> text = '''
- ... ```{1,3}
+ ... ```hl_lines="1 3"
... line 1
... line 2
... line 3
@@ -108,7 +108,10 @@ class FencedCodeExtension(Extension):
class FencedBlockPreprocessor(Preprocessor):
FENCED_BLOCK_RE = re.compile(r'''
-(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*(?P<hl_lines>\{.*?})?[ ]*\n
+(?P<fence>^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~
+(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*))?[ ]* # Optional {, and lang
+(hl_lines="(?P<hl_lines>.*?)")?[ ]* # Optional highlight lines
+}?[ ]*\n # Optional closing }
(?P<code>.*?)(?<=\n)
(?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE)
CODE_WRAP = '<pre><code%s>%s</code></pre>'
diff --git a/tests/test_extensions.py b/tests/test_extensions.py
index e918695..ea4adad 100644
--- a/tests/test_extensions.py
+++ b/tests/test_extensions.py
@@ -127,7 +127,7 @@ class TestCodeHilite(unittest.TestCase):
'</code></pre>')
def testHighlightLinesWithColon(self):
- text = '\t:::Python{2}\n\t#line 1\n\t#line 2\n\t#line 3'
+ text = '\t:::Python hl_lines="2"\n\t#line 1\n\t#line 2\n\t#line 3'
md = markdown.Markdown(extensions=['codehilite'])
if self.has_pygments:
@@ -216,7 +216,7 @@ Fenced code block
""" Test Fenced Code with Highlighted Lines. """
text = '''
-```{1,3}
+```hl_lines="1 3"
line 1
line 2
line 3
@@ -238,6 +238,39 @@ line 3
'line 2\n'
'line 3</code></pre>')
+ def testFencedLanguageAndHighlightLines(self):
+ """ Test Fenced Code with Highlighted Lines. """
+
+ text0 = '''
+```.python hl_lines="1 3"
+#line 1
+#line 2
+#line 3
+```'''
+ text1 = '''
+~~~{.python hl_lines="1 3"}
+#line 1
+#line 2
+#line 3
+~~~'''
+ for text in (text0, text1):
+ md = markdown.Markdown(extensions=[
+ 'codehilite(linenums=None,guess_lang=False)',
+ 'fenced_code'])
+
+ if self.has_pygments:
+ self.assertEqual(md.convert(text),
+ '<div class="codehilite"><pre>'
+ '<span class="hll"><span class="c">#line 1</span>\n</span>'
+ '<span class="c">#line 2</span>\n'
+ '<span class="hll"><span class="c">#line 3</span>\n</span>'
+ '</pre></div>')
+ else:
+ self.assertEqual(md.convert(text),
+ '<pre class="codehilite"><code class="language-python">#line 1\n'
+ '#line 2\n'
+ '#line 3</code></pre>')
+
class TestHeaderId(unittest.TestCase):
""" Test HeaderId Extension. """