From ab1b4c7a0db0d88aee3e79c7f7f16afcb91075a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ely=C3=A9zer=20Rezende?= Date: Mon, 3 Apr 2017 09:26:20 -0300 Subject: [PATCH] Improve docstring parser to not generate a docinfo 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. --- testimony/parser.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/testimony/parser.py b/testimony/parser.py index 539fb06..72bcc35 100644 --- a/testimony/parser.py +++ b/testimony/parser.py @@ -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.""" @@ -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: