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 #26 from jakevdp/anonymous-links
Browse files Browse the repository at this point in the history
ENH: add anonymous_references option
  • Loading branch information
miyakogi authored Aug 13, 2018
2 parents fc7b644 + c53d57c commit b77c275
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 23 additions & 5 deletions m2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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 ''
Expand Down Expand Up @@ -364,20 +370,29 @@ 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(
'<a href="{link}" title="{title}">{text}</a>'.format(
link=link, title=title, text=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'
Expand Down Expand Up @@ -528,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)

Expand Down Expand Up @@ -589,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,
Expand All @@ -604,6 +621,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(
Expand Down
2 changes: 2 additions & 0 deletions tests/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
__content__

## サブタイトル

[A link to GitHub](http://github.com/)
2 changes: 2 additions & 0 deletions tests/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ SubTitle

サブタイトル
------------

`A link to GitHub <http://github.com/>`_
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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 <http://github.com/>`__",
m.call_args[0][0])
16 changes: 16 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def test_link(self):
self.assertEqual(
out, '\nthis is a `link <http://example.com/>`_.\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 <http://example.com/>`__.\n')

def test_link_with_rel_link_enabled(self):
src = 'this is a [link](http://example.com/).'
out = self.conv_no_check(
Expand All @@ -151,6 +157,16 @@ def test_link_with_rel_link_enabled(self):
self.assertEqual(
out, '\nthis is a `link <http://example.com/>`_.\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 <http://example.com/>`__.\n')

def test_anchor(self):
src = 'this is an [anchor](#anchor).'
out = self.conv_no_check(
Expand Down

0 comments on commit b77c275

Please sign in to comment.