Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Add inline math
Browse files Browse the repository at this point in the history
  • Loading branch information
miyakogi committed May 27, 2016
1 parent 19e8808 commit eeb618b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions m2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ def parse_directive(self, m):
class RestInlineGrammar(mistune.InlineGrammar):
rest_role = re.compile(r':.*?:`.*?`')
rest_link = re.compile(r'`[^`]*?`_')
inline_math = re.compile(r'`\$(.*)?\$`')
# add colon as special text
text = re.compile(r'^[\s\S]+?(?=[\\<!\[:_*`~]|https?://| {2,}\n|$)')


class RestInlineLexer(mistune.InlineLexer):
grammar_class = RestInlineGrammar
default_rules = ['rest_role', 'rest_link'] + mistune.InlineLexer.default_rules
default_rules = ['rest_role', 'rest_link', 'inline_math'] + mistune.InlineLexer.default_rules

def output_rest_role(self, m):
"""Pass through rest role."""
Expand All @@ -54,6 +55,10 @@ def output_rest_link(self, m):
"""Pass through rest link."""
return self.renderer.rest_link(m.group(0))

def output_inline_math(self, m):
"""Pass through rest link."""
return self.renderer.inline_math(m.group(1))


class RestRenderer(mistune.Renderer):
_include_raw_html = False
Expand Down Expand Up @@ -115,7 +120,6 @@ def list(self, body, ordered=True):
:param body: body contents of the list.
:param ordered: whether this list is ordered or not.
"""
from pprint import pprint as pp
mark = '#. ' if ordered else '* '
lines = body.splitlines()
for i, line in enumerate(lines):
Expand Down Expand Up @@ -283,9 +287,11 @@ def rest_role(self, text):
def rest_link(self, text):
return text

def inline_math(self, math):
"""Extension of recommonmark"""
return '\ :math:`{}`\ '.format(math)

def directive(self, text):
if 'Foot' in text:
print(text)
return '\n' + text + '\n'


Expand Down
5 changes: 5 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def test_code_with_rest_link(self):
out = self.conv(src)
self.assertEqual(out, '\na \ ``code``\ and `RefLink <a>`_ here.\n')

def test_inline_math(self):
src = 'this is `$E = mc^2$` inline math.'
out = self.conv(src)
self.assertEqual(out, '\nthis is \ :math:`E = mc^2`\ inline math.\n')

def test_inline_html(self):
src = 'this is <s>html</s>.'
out = self.conv(src)
Expand Down

0 comments on commit eeb618b

Please sign in to comment.