Merge pull request #91 from kbenzie/kbenzie/fix_md_links

Fix hard error on cross-linking markdown document
This commit is contained in:
Eric Holscher 2018-04-27 09:15:27 -07:00 committed by GitHub
commit 450909bdcf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View file

@ -1,6 +1,7 @@
"""Docutils CommonMark parser"""
import sys
from os.path import splitext
from docutils import parsers, nodes
from sphinx import addnodes
@ -126,7 +127,16 @@ class CommonMarkParser(parsers.Parser):
def visit_link(self, mdnode):
ref_node = nodes.reference()
ref_node['refuri'] = mdnode.destination
# Check destination is supported for cross-linking and remove extension
destination = mdnode.destination
_, ext = splitext(destination)
# TODO check for other supported extensions, such as those specified in
# the Sphinx conf.py file but how to access this information?
# TODO this should probably only remove the extension for local paths,
# i.e. not uri's starting with http or other external prefix.
if ext.replace('.', '') in self.supported:
destination = destination.replace(ext, '')
ref_node['refuri'] = destination
# TODO okay, so this is acutally not always the right line number, but
# these mdnodes won't have sourcepos on them for whatever reason. This
# is better than 0 though.
@ -136,11 +146,12 @@ class CommonMarkParser(parsers.Parser):
ref_node['title'] = mdnode.title
next_node = ref_node
url_check = urlparse(mdnode.destination)
url_check = urlparse(destination)
if not url_check.scheme and not url_check.fragment:
wrap_node = addnodes.pending_xref(
reftarget=mdnode.destination,
reftarget=destination,
reftype='any',
refdomain=None, # Added to enable cross-linking
refexplicit=True,
refwarn=True
)

View file

@ -183,7 +183,7 @@ class TestParsing(unittest.TestCase):
<?xml version="1.0" ?>
<document source="&lt;string&gt;">
<paragraph>
<pending_xref refexplicit="True" reftarget="/foo" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/foo" reftype="any" refwarn="True">
<reference refuri="/foo">link</reference>
</pending_xref>
</paragraph>
@ -198,7 +198,7 @@ class TestParsing(unittest.TestCase):
<?xml version="1.0" ?>
<document source="&lt;string&gt;">
<paragraph>
<pending_xref refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
<reference refuri="foo">link</reference>
</pending_xref>
</paragraph>
@ -274,21 +274,21 @@ class TestParsing(unittest.TestCase):
<bullet_list>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/1" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/1" reftype="any" refwarn="True">
<reference refuri="/1">List item 1</reference>
</pending_xref>
</paragraph>
</list_item>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/2" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/2" reftype="any" refwarn="True">
<reference refuri="/2">List item 2</reference>
</pending_xref>
</paragraph>
</list_item>
<list_item>
<paragraph>
<pending_xref refexplicit="True" reftarget="/3" reftype="any" refwarn="True">
<pending_xref refdomain="None" refexplicit="True" reftarget="/3" reftype="any" refwarn="True">
<reference refuri="/3">List item 3</reference>
</pending_xref>
</paragraph>