mirror of
https://github.com/vale981/recommonmark
synced 2025-03-05 10:01:39 -05:00
Merge branch 'master' of github.com:rtfd/recommonmark into release/0.6.0
This commit is contained in:
commit
be6126ee3a
4 changed files with 71 additions and 2 deletions
|
@ -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.
|
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.
|
* __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.
|
* __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_auto_doc_ref__: enable the Auto Doc Ref feature. **Deprecated**
|
||||||
* __enable_math__: enable the Math Formula feature.
|
* __enable_math__: enable the Math Formula feature.
|
||||||
* __enable_inline_math__: enable the Inline Math feature.
|
* __enable_inline_math__: enable the Inline Math feature.
|
||||||
* __enable_eval_rst__: enable the evaluate embedded reStructuredText 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
|
* __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
|
## Development
|
||||||
|
|
||||||
|
|
|
@ -25,12 +25,22 @@ class CommonMarkParser(parsers.Parser):
|
||||||
supported = ('md', 'markdown')
|
supported = ('md', 'markdown')
|
||||||
translate_section_name = None
|
translate_section_name = None
|
||||||
|
|
||||||
|
default_config = {
|
||||||
|
'known_url_schemes': None,
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._level_to_elem = {}
|
self._level_to_elem = {}
|
||||||
|
|
||||||
def parse(self, inputstring, document):
|
def parse(self, inputstring, document):
|
||||||
self.document = document
|
self.document = document
|
||||||
self.current_node = 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_parse(inputstring, document)
|
||||||
self.setup_sections()
|
self.setup_sections()
|
||||||
parser = Parser()
|
parser = Parser()
|
||||||
|
@ -153,7 +163,16 @@ class CommonMarkParser(parsers.Parser):
|
||||||
next_node = ref_node
|
next_node = ref_node
|
||||||
|
|
||||||
url_check = urlparse(destination)
|
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(
|
wrap_node = addnodes.pending_xref(
|
||||||
reftarget=unquote(destination),
|
reftarget=unquote(destination),
|
||||||
reftype='any',
|
reftype='any',
|
||||||
|
|
|
@ -41,6 +41,7 @@ class AutoStructify(transforms.Transform):
|
||||||
|
|
||||||
default_config = {
|
default_config = {
|
||||||
'enable_auto_doc_ref': False,
|
'enable_auto_doc_ref': False,
|
||||||
|
'auto_toc_maxdepth': 1,
|
||||||
'auto_toc_tree_section': None,
|
'auto_toc_tree_section': None,
|
||||||
'enable_auto_toc_tree': True,
|
'enable_auto_toc_tree': True,
|
||||||
'enable_eval_rst': True,
|
'enable_eval_rst': True,
|
||||||
|
@ -48,6 +49,7 @@ class AutoStructify(transforms.Transform):
|
||||||
'enable_inline_math': True,
|
'enable_inline_math': True,
|
||||||
'commonmark_suffixes': ['.md'],
|
'commonmark_suffixes': ['.md'],
|
||||||
'url_resolver': lambda x: x,
|
'url_resolver': lambda x: x,
|
||||||
|
'known_url_schemes': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
def parse_ref(self, ref):
|
def parse_ref(self, ref):
|
||||||
|
@ -179,7 +181,10 @@ class AutoStructify(transforms.Transform):
|
||||||
self.current_level)
|
self.current_level)
|
||||||
return self.state_machine.run_directive(
|
return self.state_machine.run_directive(
|
||||||
'toctree',
|
'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])
|
content=['%s <%s>' % (k, v) for k, v in refs])
|
||||||
|
|
||||||
def auto_inline_code(self, node):
|
def auto_inline_code(self, node):
|
||||||
|
|
|
@ -17,6 +17,7 @@ class TestParsing(unittest.TestCase):
|
||||||
def assertParses(self, source, expected, alt=False): # noqa
|
def assertParses(self, source, expected, alt=False): # noqa
|
||||||
parser = CommonMarkParser()
|
parser = CommonMarkParser()
|
||||||
parser.parse(dedent(source), new_document('<string>'))
|
parser.parse(dedent(source), new_document('<string>'))
|
||||||
|
self.maxDiff = None
|
||||||
self.assertMultiLineEqual(
|
self.assertMultiLineEqual(
|
||||||
dedent(expected).lstrip(),
|
dedent(expected).lstrip(),
|
||||||
dedent(parser.document.asdom().toprettyxml(indent=' ')),
|
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="<string>">
|
||||||
|
<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):
|
def test_image(self):
|
||||||
self.assertParses(
|
self.assertParses(
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue