Merge branch 'master' of github.com:rtfd/recommonmark into release/0.6.0

This commit is contained in:
Eric Holscher 2019-08-09 11:20:11 -07:00
commit be6126ee3a
4 changed files with 71 additions and 2 deletions

View file

@ -105,12 +105,16 @@ See https://github.com/rtfd/recommonmark/blob/master/docs/conf.py for a full exa
AutoStructify comes with the following options. See [http://recommonmark.readthedocs.org/en/latest/auto_structify.html](http://recommonmark.readthedocs.org/en/latest/auto_structify.html) for more information about the specific features.
* __enable_auto_toc_tree__: enable the Auto Toc Tree feature.
* __auto_toc_maxdepth__: The max depth of the Auto Toc. Defaults to 1.
* __auto_toc_tree_section__: when True, Auto Toc Tree will only be enabled on section that matches the title.
* __enable_auto_doc_ref__: enable the Auto Doc Ref feature. **Deprecated**
* __enable_math__: enable the Math Formula feature.
* __enable_inline_math__: enable the Inline Math feature.
* __enable_eval_rst__: enable the evaluate embedded reStructuredText feature.
* __url_resolver__: a function that maps a existing relative position in the document to a http link
* __known_url_schemes__: a list of url schemes to treat as URLs, schemes not in this list will be assumed to be Sphinx cross-references.
Defaults to `None`, which means treat all URL schemes as URLs.
Example: `['http', 'https', 'mailto']`
## Development

View file

@ -25,12 +25,22 @@ class CommonMarkParser(parsers.Parser):
supported = ('md', 'markdown')
translate_section_name = None
default_config = {
'known_url_schemes': None,
}
def __init__(self):
self._level_to_elem = {}
def parse(self, inputstring, document):
self.document = document
self.current_node = document
self.config = self.default_config.copy()
try:
new_cfg = self.document.settings.env.config.recommonmark_config
self.config.update(new_cfg)
except AttributeError:
pass
self.setup_parse(inputstring, document)
self.setup_sections()
parser = Parser()
@ -153,7 +163,16 @@ class CommonMarkParser(parsers.Parser):
next_node = ref_node
url_check = urlparse(destination)
if not url_check.scheme and not url_check.fragment:
# If there's not a url scheme (e.g. 'https' for 'https:...' links),
# or there is a scheme but it's not in the list of known_url_schemes,
# then assume it's a cross-reference and pass it to Sphinx as an `:any:` ref.
known_url_schemes = self.config.get('known_url_schemes')
if known_url_schemes:
scheme_known = url_check.scheme in known_url_schemes
else:
scheme_known = bool(url_check.scheme)
if not url_check.fragment and not scheme_known:
wrap_node = addnodes.pending_xref(
reftarget=unquote(destination),
reftype='any',

View file

@ -41,6 +41,7 @@ class AutoStructify(transforms.Transform):
default_config = {
'enable_auto_doc_ref': False,
'auto_toc_maxdepth': 1,
'auto_toc_tree_section': None,
'enable_auto_toc_tree': True,
'enable_eval_rst': True,
@ -48,6 +49,7 @@ class AutoStructify(transforms.Transform):
'enable_inline_math': True,
'commonmark_suffixes': ['.md'],
'url_resolver': lambda x: x,
'known_url_schemes': None,
}
def parse_ref(self, ref):
@ -179,7 +181,10 @@ class AutoStructify(transforms.Transform):
self.current_level)
return self.state_machine.run_directive(
'toctree',
options={'maxdepth': 1, 'numbered': numbered},
options={
'maxdepth': self.config['auto_toc_maxdepth'],
'numbered': numbered,
},
content=['%s <%s>' % (k, v) for k, v in refs])
def auto_inline_code(self, node):

View file

@ -17,6 +17,7 @@ class TestParsing(unittest.TestCase):
def assertParses(self, source, expected, alt=False): # noqa
parser = CommonMarkParser()
parser.parse(dedent(source), new_document('<string>'))
self.maxDiff = None
self.assertMultiLineEqual(
dedent(expected).lstrip(),
dedent(parser.document.asdom().toprettyxml(indent=' ')),
@ -223,6 +224,46 @@ class TestParsing(unittest.TestCase):
"""
)
def test_known_schemes(self):
self.assertParses(
"""
[https link](https://example.com)
[http link](http://example.com)
[mailto link](mailto:admin@example.com)
[custom scheme](custom:example.com)
[ref link](path/to/file:heading)
[ref link with spaces](<path/to/file:heading with spaces>)
""",
"""
<?xml version="1.0" ?>
<document source="&lt;string&gt;">
<paragraph>
<reference refuri="https://example.com">https link</reference>
<reference refuri="http://example.com">http link</reference>
<reference refuri="mailto:admin@example.com">mailto link</reference>
<reference refuri="custom:example.com">custom scheme</reference>
<pending_xref refdomain="None" refexplicit="True" reftarget="path/to/file:heading" reftype="any" refwarn="True">
<reference refuri="path/to/file:heading">ref link</reference>
</pending_xref>
<pending_xref refdomain="None" refexplicit="True" reftarget="path/to/file:heading with spaces" reftype="any" refwarn="True">
<reference refuri="path/to/file:heading%20with%20spaces">ref link with spaces</reference>
</pending_xref>
</paragraph>
</document>
"""
)
pass
def test_image(self):
self.assertParses(
"""