From 3cb32fdaea4830ac90a21de7214d890edd3281f3 Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Tue, 31 Jul 2018 15:21:09 -0700 Subject: [PATCH 1/3] ENH: add anonymous_references option to enable use of rst anonymous references --- m2r.py | 22 +++++++++++++++++++--- tests/test_renderer.py | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/m2r.py b/m2r.py index 1e0f7df..4e64471 100644 --- a/m2r.py +++ b/m2r.py @@ -44,6 +44,9 @@ parser.add_argument('--parse-relative-links', action='store_true', default=False, help='parse relative links into ref or doc directives') +parser.add_argument('--anonymous-references', action='store_true', + default=False, + help='use anonymous references in generated rst') def parse_options(): @@ -191,11 +194,14 @@ class RestRenderer(mistune.Renderer): def __init__(self, *args, **kwargs): self.parse_relative_links = kwargs.pop('parse_relative_links', False) + self.anonymous_references = kwargs.pop('anonymous_references', False) super(RestRenderer, self).__init__(*args, **kwargs) if not _is_sphinx: parse_options() if options.parse_relative_links: self.parse_relative_links = options.parse_relative_links + if options.anonymous_references: + self.anonymous_references = options.anonymous_references def _indent_block(self, block): return '\n'.join(self.indent + line if line else '' @@ -364,6 +370,10 @@ def link(self, link, title, text): :param title: title content for `title` attribute. :param text: text content for description. """ + if self.anonymous_references: + underscore = '__' + else: + underscore = '_' if title: return self._raw_html( '{text}'.format( @@ -371,13 +381,18 @@ def link(self, link, title, text): ) ) if not self.parse_relative_links: - return '\ `{text} <{target}>`_\ '.format(target=link, text=text) + return '\ `{text} <{target}>`{underscore}\ '.format( + target=link, + text=text, + underscore=underscore + ) else: url_info = urlparse(link) if url_info.scheme: - return '\ `{text} <{target}>`_\ '.format( + return '\ `{text} <{target}>`{underscore}\ '.format( target=link, - text=text + text=text, + underscore=underscore ) else: link_type = 'doc' @@ -604,6 +619,7 @@ def setup(app): _is_sphinx = True 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_source_parser('.md', M2RParser) app.add_directive('mdinclude', MdInclude) metadata = dict( diff --git a/tests/test_renderer.py b/tests/test_renderer.py index 780531d..b33da77 100644 --- a/tests/test_renderer.py +++ b/tests/test_renderer.py @@ -142,6 +142,12 @@ def test_link(self): self.assertEqual( out, '\nthis is a `link `_.\n') + def test_anonymous_link(self): + src = 'this is a [link](http://example.com/).' + out = self.conv(src, anonymous_references=True) + self.assertEqual( + out, '\nthis is a `link `__.\n') + def test_link_with_rel_link_enabled(self): src = 'this is a [link](http://example.com/).' out = self.conv_no_check( @@ -151,6 +157,16 @@ def test_link_with_rel_link_enabled(self): self.assertEqual( out, '\nthis is a `link `_.\n') + def test_anonymous_link_with_rel_link_enabled(self): + src = 'this is a [link](http://example.com/).' + out = self.conv_no_check( + src, + parse_relative_links=True, + anonymous_references=True + ) + self.assertEqual( + out, '\nthis is a `link `__.\n') + def test_anchor(self): src = 'this is an [anchor](#anchor).' out = self.conv_no_check( From 40aec4a9ad461ea73554ac737ce3364b1eddecb4 Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Mon, 6 Aug 2018 14:40:28 -0700 Subject: [PATCH 2/3] TST: add cli test for --anonymous-references option --- docs/conf.py | 1 + tests/test.md | 2 ++ tests/test.rst | 2 ++ tests/test_cli.py | 10 ++++++++++ 4 files changed, 15 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index 3e3eaa0..5672618 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -50,6 +50,7 @@ source_suffix = '.md' no_underscore_emphasis = True m2r_parse_relative_links = True +m2r_anonymous_references = False # The encoding of source files. #source_encoding = 'utf-8-sig' diff --git a/tests/test.md b/tests/test.md index 8ce9ff3..c51b353 100644 --- a/tests/test.md +++ b/tests/test.md @@ -5,3 +5,5 @@ __content__ ## サブタイトル + +[A link to GitHub](http://github.com/) diff --git a/tests/test.rst b/tests/test.rst index 3fd9321..42a40d2 100644 --- a/tests/test.rst +++ b/tests/test.rst @@ -9,3 +9,5 @@ SubTitle サブタイトル ------------ + +`A link to GitHub `_ diff --git a/tests/test_cli.py b/tests/test_cli.py index 7b42724..ec55300 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -33,6 +33,7 @@ def setUp(self): options.overwrite = False options.dry_run = False options.no_underscore_emphasis = False + options.anonymous_references = False self._orig_argv = copy(sys.argv) if path.exists(test_rst): with open(test_rst) as f: @@ -54,6 +55,7 @@ def test_no_file(self): message = buffer.read().decode() self.assertIn('usage', message) self.assertIn('underscore-emphasis', message) + self.assertIn('anonymous-references', message) self.assertIn('optional arguments:', message) def test_parse_file(self): @@ -122,3 +124,11 @@ def test_underscore_option(self): main() self.assertIn('__content__', m.call_args[0][0]) self.assertNotIn('**content**', m.call_args[0][0]) + + def test_anonymous_reference_option(self): + sys.argv = [ + sys.argv[0], '--anonymous-references', '--dry-run', test_md] + with patch(_builtin + '.print') as m: + main() + self.assertIn("`A link to GitHub `__", + m.call_args[0][0]) From c53d57caa457b1842d3f3189611d5ab8980da2fb Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Mon, 6 Aug 2018 14:53:28 -0700 Subject: [PATCH 3/3] BUG: correctly propagate m2r_anonymous_references --- m2r.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/m2r.py b/m2r.py index 4e64471..6f51bc1 100644 --- a/m2r.py +++ b/m2r.py @@ -543,7 +543,8 @@ def parse(self, inputstrings, document): config = document.settings.env.config converter = M2R( no_underscore_emphasis=config.no_underscore_emphasis, - parse_relative_links=config.m2r_parse_relative_links + parse_relative_links=config.m2r_parse_relative_links, + anonymous_references=config.m2r_anonymous_references, ) super(M2RParser, self).parse(converter(inputstring), document) @@ -604,7 +605,8 @@ def run(self): config = self.state.document.settings.env.config converter = M2R( no_underscore_emphasis=config.no_underscore_emphasis, - parse_relative_links=config.m2r_parse_relative_links + parse_relative_links=config.m2r_parse_relative_links, + anonymous_references=config.m2r_anonymous_references, ) include_lines = statemachine.string2lines(converter(rawtext), tab_width,