Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common mechanism for parsing docutils afield_lists to dicts` #77

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build
build*
json
maxref
maxref.zip
Expand Down
19 changes: 19 additions & 0 deletions flucoma/doc/rst/docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
from docutils import nodes, utils
from docutils.parsers.rst import roles

def parse_fieldlist(node):
"""
node should be the parent of a bunch of fields
"""

res = {}

f = node.next_node(nodes.field)

while f is not None:
n = f.next_node(nodes.field_name)
v = f.next_node(nodes.field_body)
# print(n)
res[n.astext().strip()] = v.rawsource.strip()
f = f.next_node(nodes.field,siblings=True,descend=False)

return (res if bool(res) else None)


def fluid_object_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
"""Create a link to a FluCoMa object
Expand Down
88 changes: 88 additions & 0 deletions flucoma/doc/test/test_fieldlist_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import unittest

from docutils import nodes
from docutils.core import publish_doctree

from flucoma.doc.rst.docutils import parse_fieldlist

unnested = """\

:0: 0-stuff
:1: 1-stuff
:2: 2-stuff
"""

nested_content="""\
:0-0: nest0
:0-1: nest1
:0-2: nest2
:0-3:
Let's put some more ``content`` **in** here

* list0
* list1
* list2

:0-4: and relax\
"""

nested_nested_content="""\
Let's put some more ``content`` **in** here

* list0
* list1
* list2\
"""

nested1 = """\
:0:
:0-0: nest0
:0-1: nest1
:0-2: nest2
:0-3:
Let's put some more ``content`` **in** here

* list0
* list1
* list2

:0-4: and relax

:1: can we stop yet?
"""

class TestFieldListParse(unittest.TestCase):
def test_unnested_parse(self):
tree = publish_doctree(source=unnested)
# print(tree.pformat())
parsed = parse_fieldlist(tree)

self.assertEqual(list(parsed.keys()),['0','1','2'])
self.assertEqual(list(parsed.values()),['0-stuff','1-stuff','2-stuff'])

def test_nested_parse_top(self):
tree = publish_doctree(source=nested1)
parsed = parse_fieldlist(tree)

self.assertEqual(list(parsed.keys()),['0','1'])
self.assertEqual(list(parsed.values()),[nested_content,'can we stop yet?'])

def test_nested_parse_inner(self):
tree = publish_doctree(source=nested1)

parent = tree.children[0]


for i,n in enumerate(parent.children): #top level fields
contains_subfields = n.next_node(nodes.field)
if i == 0:
self.assertTrue(contains_subfields)
else:
self.assertFalse(contains_subfields)

if contains_subfields:
subfields = parse_fieldlist(n)
self.assertEqual(list(subfields.keys()),['0-0','0-1','0-2','0-3','0-4'])
self.assertEqual(list(subfields.values()),[
'nest0','nest1','nest2',nested_nested_content,'and relax'
])