Skip to content

Commit e72768a

Browse files
authored
Merge pull request #26 from BroadbandForum/feature/misc-improvements
Miscellaneous improvements (will add more detail later)
2 parents 0d33750 + 750c6ab commit e72768a

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

sphinx_markdown_parser/markdown_parser.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import html
77
import markdown
88
from markdown import util
9+
import os.path
910
import urllib.parse
1011

1112
from pydash import _
1213
import re
1314
import yaml
1415

16+
from sphinx import addnodes
17+
1518
__all__ = ['MarkdownParser']
1619

1720
TAGS_INLINE = set("""
@@ -277,7 +280,11 @@ def pop_node(self):
277280

278281
def new_section(self, heading):
279282
section = nodes.section()
280-
anchor = to_html_anchor("".join(heading.itertext()))
283+
if heading.get('id'):
284+
anchor_text = heading.get('id')
285+
else:
286+
anchor_text = "".join(heading.itertext())
287+
anchor = to_html_anchor(anchor_text)
281288
section['ids'] = [anchor]
282289
section['names'] = [anchor]
283290
return section
@@ -291,7 +298,10 @@ def start_new_section(self, lvl, heading):
291298
self.reset_w_old()
292299
self.parse_stack_h.append(lvl)
293300
assert isinstance(self.parse_stack_w[-1], nodes.section)
294-
return nodes.title()
301+
title = nodes.title()
302+
if heading.get('class'):
303+
title['classes'] = heading.get('class').split()
304+
return title
295305

296306
def visit_script(self, node):
297307
if node.attrib.get("type", "").split(";")[0] == "math/tex":
@@ -356,15 +366,43 @@ def visit_em(self, node):
356366
def visit_br(self, node):
357367
return nodes.Text('\n')
358368

369+
# note: logic is based on CommonMarkParser.visit_link()
359370
def visit_a(self, node):
360371
reference = nodes.reference()
361372
href = node.attrib.pop('href', '')
362-
try:
363-
r = urllib.parse.urlparse(href)
364-
if r.path.endswith(".md"):
365-
href = urllib.parse.urlunparse(r._replace(path = r.path[:-3] + ".html"))
366-
except:
367-
pass
373+
url_check = urllib.parse.urlparse(href)
374+
if not url_check.scheme and not url_check.fragment:
375+
# remove .md or .markdown extension
376+
href_root, href_ext = os.path.splitext(href)
377+
if href_ext in {'.md', '.markdown'}:
378+
href = href_root
379+
380+
# remove leading '~'; it causes link text to be last component only
381+
last_only = href.startswith('~')
382+
if last_only:
383+
href = href[1:]
384+
385+
# always add an 'any' reference
386+
reference = addnodes.pending_xref(reftarget=href, reftype='any',
387+
refdomain=None, refexplicit=True,
388+
refwarn=True)
389+
390+
# generate default link text
391+
text = re.sub(r'.+\.', '', href) if last_only else href
392+
393+
# add text if none was supplied, so can use [](url) to get ReST
394+
# link behaviour
395+
def adjust_text(node_):
396+
node_text = (node_.text or '').strip()
397+
if not node_text:
398+
node_.text = text
399+
400+
if not list(node):
401+
adjust_text(node)
402+
else:
403+
# note: have to use ` ` (with a space) to get code style
404+
adjust_text(list(node)[0])
405+
368406
reference['refuri'] = href
369407
return reference
370408

sphinx_markdown_parser/transform.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from .states import DummyStateMachine
1313

14-
1514
class AutoStructify(transforms.Transform):
1615
"""Automatically try to transform blocks to sphinx directives.
1716
@@ -39,6 +38,8 @@ def __init__(self, *args, **kwargs):
3938
suffix_set = set(['md', 'rst'])
4039

4140
default_config = {
41+
'auto_toc_tree_maxdepth': 1,
42+
'auto_toc_tree_numbered': None,
4243
'auto_toc_tree_section': None,
4344
'commonmark_suffixes': ['.md'],
4445
'enable_auto_doc_ref': False,
@@ -125,6 +126,8 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
125126
"""
126127
if not self.config['enable_auto_toc_tree']:
127128
return None
129+
maxdepth = self.config.get('auto_toc_tree_maxdepth', 1)
130+
numbered = self.config.get('auto_toc_tree_numbered', None)
128131
# when auto_toc_tree_section is set
129132
# only auto generate toctree under the specified section title
130133
sec = self.config['auto_toc_tree_section']
@@ -147,11 +150,12 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
147150
if title.astext().strip() != sec:
148151
return None
149152

150-
numbered = None
151-
if isinstance(node, nodes.bullet_list):
153+
if numbered is not None:
154+
pass
155+
elif isinstance(node, nodes.bullet_list):
152156
numbered = 0
153157
elif isinstance(node, nodes.enumerated_list):
154-
numbered = 1
158+
numbered = 999
155159

156160
if numbered is None:
157161
return None
@@ -168,10 +172,18 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
168172
ref = par.children[0]
169173
if isinstance(ref, addnodes.pending_xref):
170174
ref = ref.children[0]
171-
if not isinstance(ref, nodes.reference):
175+
if isinstance(ref, nodes.Text):
176+
text = ref.astext()
177+
title, uri, docpath = text, text, None
178+
elif isinstance(ref, nodes.reference):
179+
# TODO check that this can't happen and, if so, get rid of
180+
# parse_ref()
181+
self.reporter.warning('AutoStructify unexpected reference '
182+
'%r' % ref)
183+
title, uri, docpath = self.parse_ref(ref)
184+
else:
172185
return None
173-
title, uri, docpath = self.parse_ref(ref)
174-
if title is None or uri.startswith('#'):
186+
if uri.startswith('#'):
175187
return None
176188
if docpath:
177189
refs.append((title, docpath))
@@ -183,10 +195,11 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
183195
return self.state_machine.run_directive(
184196
'toctree',
185197
options={
186-
'maxdepth': 1,
198+
'caption': sec,
199+
'maxdepth': maxdepth,
187200
'numbered': numbered
188201
},
189-
content=['%s <%s>' % (k, v) for k, v in refs]
202+
content=[v for _, v in refs]
190203
)
191204

192205
def auto_inline_code(self, node):

0 commit comments

Comments
 (0)