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

Commit

Permalink
Merge pull request #28 from morganwillcock/no-maths
Browse files Browse the repository at this point in the history
Add option to disable inline math
  • Loading branch information
miyakogi authored Oct 11, 2018
2 parents e88a711 + fdf0c64 commit 69d92e1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
no_underscore_emphasis = True
m2r_parse_relative_links = True
m2r_anonymous_references = False
m2r_disable_inline_math = False

# The encoding of source files.
#source_encoding = 'utf-8-sig'
Expand Down
30 changes: 22 additions & 8 deletions m2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
parser.add_argument('--anonymous-references', action='store_true',
default=False,
help='use anonymous references in generated rst')
parser.add_argument('--disable-inline-math', action='store_true',
default=False,
help='disable parsing inline math')


def parse_options():
Expand Down Expand Up @@ -131,19 +134,27 @@ class RestInlineLexer(mistune.InlineLexer):
'image_link',
'rest_role',
'rest_link',
'inline_math',
'eol_literal_marker',
] + mistune.InlineLexer.default_rules

def __init__(self, *args, **kwargs):
no_underscore_emphasis = kwargs.pop('no_underscore_emphasis', False)
disable_inline_math = kwargs.pop('disable_inline_math', False)
super(RestInlineLexer, self).__init__(*args, **kwargs)
if no_underscore_emphasis:
self.rules.no_underscore_emphasis()
elif not _is_sphinx:
if not _is_sphinx:
parse_options()
if options.no_underscore_emphasis:
self.rules.no_underscore_emphasis()
if no_underscore_emphasis or getattr(options,
'no_underscore_emphasis',
False):
self.rules.no_underscore_emphasis()
inline_maths = 'inline_math' in self.default_rules
if disable_inline_math or getattr(options,
'disable_inline_math',
False):
if inline_maths:
self.default_rules.remove('inline_math')
elif not inline_maths:
self.default_rules.insert(0, 'inline_math')

def output_double_emphasis(self, m):
# may include code span
Expand Down Expand Up @@ -198,9 +209,9 @@ def __init__(self, *args, **kwargs):
super(RestRenderer, self).__init__(*args, **kwargs)
if not _is_sphinx:
parse_options()
if options.parse_relative_links:
if getattr(options, 'parse_relative_links', False):
self.parse_relative_links = options.parse_relative_links
if options.anonymous_references:
if getattr(options, 'anonymous_references', False):
self.anonymous_references = options.anonymous_references

def _indent_block(self, block):
Expand Down Expand Up @@ -548,6 +559,7 @@ def parse(self, inputstrings, document):
no_underscore_emphasis=config.no_underscore_emphasis,
parse_relative_links=config.m2r_parse_relative_links,
anonymous_references=config.m2r_anonymous_references,
disable_inline_math=config.m2r_disable_inline_math
)
super(M2RParser, self).parse(converter(inputstring), document)

Expand Down Expand Up @@ -620,6 +632,7 @@ def run(self):
no_underscore_emphasis=config.no_underscore_emphasis,
parse_relative_links=config.m2r_parse_relative_links,
anonymous_references=config.m2r_anonymous_references,
disable_inline_math=config.m2r_disable_inline_math
)
include_lines = statemachine.string2lines(converter(rawtext),
tab_width,
Expand All @@ -635,6 +648,7 @@ def setup(app):
app.add_config_value('no_underscore_emphasis', False, 'env')
app.add_config_value('m2r_parse_relative_links', False, 'env')
app.add_config_value('m2r_anonymous_references', False, 'env')
app.add_config_value('m2r_disable_inline_math', False, 'env')
app.add_source_parser('.md', M2RParser)
app.add_directive('mdinclude', MdInclude)
metadata = dict(
Expand Down
2 changes: 2 additions & 0 deletions tests/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ __content__
## サブタイトル

[A link to GitHub](http://github.com/)

This is `$E = mc^2$` inline math.
2 changes: 2 additions & 0 deletions tests/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ SubTitle
------------

`A link to GitHub <http://github.com/>`_

This is :math:`E = mc^2` inline math.
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUp(self):
options.dry_run = False
options.no_underscore_emphasis = False
options.anonymous_references = False
options.disable_inline_math = False
self._orig_argv = copy(sys.argv)
if path.exists(test_rst):
with open(test_rst) as f:
Expand All @@ -56,6 +57,7 @@ def test_no_file(self):
self.assertIn('usage', message)
self.assertIn('underscore-emphasis', message)
self.assertIn('anonymous-references', message)
self.assertIn('inline-math', message)
self.assertIn('optional arguments:', message)

def test_parse_file(self):
Expand Down Expand Up @@ -132,3 +134,11 @@ def test_anonymous_reference_option(self):
main()
self.assertIn("`A link to GitHub <http://github.com/>`__",
m.call_args[0][0])

def test_disable_inline_math(self):
sys.argv = [
sys.argv[0], '--disable-inline-math', '--dry-run', test_md]
with patch(_builtin + '.print') as m:
main()
self.assertIn('``$E = mc^2$``', m.call_args[0][0])
self.assertNotIn(':math:', m.call_args[0][0])
5 changes: 5 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ def test_inline_math(self):
out = self.conv(src)
self.assertEqual(out, '\nthis is :math:`E = mc^2` inline math.\n')

def test_disable_inline_math(self):
src = 'this is `$E = mc^2$` inline math.'
out = self.conv(src, disable_inline_math=True)
self.assertEqual(out, '\nthis is ``$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 69d92e1

Please sign in to comment.