Skip to content

Commit

Permalink
Improve docstring parser to not generate a docinfo
Browse files Browse the repository at this point in the history
Disable the DocInfo transformation. Without that transformation the
first field list element will not be transformed into a docinfo element.
This will normalize the parsed content when there is or there isn't any
element before the tokens.

Thanks @mbukatov for the sample code on how to disable the DocInfo
transformation.
  • Loading branch information
elyezer committed Apr 3, 2017
1 parent febd815 commit ab1b4c7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion testimony/parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# coding=utf-8
"""Docstring parser utilities for Testimony."""
from docutils.core import publish_string
from docutils.readers import standalone
from docutils.transforms import frontmatter
from xml.etree import ElementTree

from testimony.constants import DEFAULT_MINIMUM_TOKENS, DEFAULT_TOKENS


class _NoDocInfoReader(standalone.Reader):
"""Reader that does not do the DocInfo transformation.
Extend standalone reader and drop the DocInfo transformation. Without that
transformation, the first field list element will remain a field list and
won't be converted to a docinfo element.
"""

def get_transforms(self):
"""Get default transforms without DocInfo."""
transforms = standalone.Reader.get_transforms(self)
transforms.remove(frontmatter.DocInfo)
return transforms


class DocstringParser(object):
"""Parse docstring extracting tokens."""

Expand Down Expand Up @@ -55,7 +72,8 @@ def parse(self, docstring=None):
# Parse the docstring with the docutils RST parser and output the
# result as XML, this ease the process of getting the tokens
# information.
docstring_xml = publish_string(docstring, writer_name='xml')
docstring_xml = publish_string(
docstring, reader=_NoDocInfoReader(), writer_name='xml')
root = ElementTree.fromstring(docstring_xml)
tokens = root.findall('./field_list/field')
for token in tokens:
Expand Down

0 comments on commit ab1b4c7

Please sign in to comment.