Skip to content

Commit

Permalink
implement SphinxPreproc following the implementation by @jacsmith21 in
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Jun 4, 2018
1 parent 6e60ee6 commit c373b79
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
62 changes: 59 additions & 3 deletions pydoc_markdown/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,67 @@ def _reorganize_module(self, module):


class SphinxPreproc(nr.interface.Implementation):
nr.interface.implements(IPreprocessor)
nr.interface.implements(ITextPreprocessor)

@nr.interface.override
def preprocess(self, root):
pass # TODO
def preprocess_text(self, node):
nodes = [] # A list of nodes that act as a substitute.
in_codeblock = False
keyword = None
components = {}

for line in node.text.split('\n'):
if line.startswith("```"):
in_codeblock = not in_codeblock

if not in_codeblock:
match = re.match(r':(?:param|parameter)\s+(\w+)\s*:(.*)?$', line)
if match:
keyword = 'Arguments'
param = match.group(1)
text = match.group(2)
text = text.strip()

component = components.get(keyword, [])
component.append('- `{}`: {}'.format(param, text))
components[keyword] = component
continue

match = re.match(r':(?:return|returns)\s*:(.*)?$', line)
if match:
keyword = 'Returns'
text = match.group(1)
text = text.strip()

component = components.get(keyword, [])
component.append(text)
components[keyword] = component
continue

match = re.match(':(?:raises|raise)\s+(\w+)\s*:(.*)?$', line)
if match:
keyword = 'Raises'
exception = match.group(1)
text = match.group(2)
text = text.strip()

component = components.get(keyword, [])
component.append('- `{}`: {}'.format(exception, text))
components[keyword] = component
continue

if keyword is not None:
components[keyword].append(line)
else:
nodes.append(Text(line + '\n'))

for key, items in components.items():
if not items: continue
nodes.append(Text('\n\n'))
nodes.append(Text('**{}**:\n'.format(key)))
nodes.extend(Text(x + '\n') for x in items)

node.substitute(nodes)


class Renderer(nr.interface.Implementation):
Expand Down
27 changes: 23 additions & 4 deletions pydoc_markdown/core/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def remove(self):
if parent:
parent._children.remove(self)

def _before_attach_to_parent(self, parent):
pass

def append(self, child):
if not isinstance(child, Node):
raise TypeError('expected Node instance, got {}'
Expand All @@ -77,6 +80,25 @@ def insert(self, index, child):
child._parent = weakref.ref(self)
self._children.insert(index, child)

def collapse_text(self):
"""
Collapse multiple #Text nodes in the children of this node to one.
"""

text = ''
remove = []
for child in list(self._children):
if isinstance(child, Text):
text += child.text
remove.append(child)
elif text:
self.insert(self._children.index(child), Text(text))
child.collapse_text()
if text:
self.append(Text(text))
for node in remove:
node.remove()

def substitute(self, arg):
"""
Substitute this node with the node or collection of nodes specified
Expand Down Expand Up @@ -122,9 +144,6 @@ def generator(node, this):
for node in generator(self, this):
visitor(node)

def _before_attach_to_parent(self, parent):
pass


class Text(Node):
"""
Expand All @@ -139,7 +158,7 @@ def __init__(self, text):
def __repr__(self):
text = self.text
if len(text) > 20:
text = text[19:] + '…'
text = text[:19] + '…'
return 'Text(text={!r})'.format(text)

def append(self, child):
Expand Down
1 change: 1 addition & 0 deletions pydoc_markdown/core/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def recursion(node):
for child in list(node.children):
recursion(child)
recursion(root)
root.collapse_text()

def preprocess_text(self, text_node):
pass
Expand Down

0 comments on commit c373b79

Please sign in to comment.