From ff5ce639cfe006442abf08f6f12645860e303834 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 May 2013 17:01:41 -0400 Subject: [PATCH 1/2] refs #27: added a test showing how an sdt tag should be handled. --- pydocx/tests/document_builder.py | 9 +++++++++ pydocx/tests/templates/sdt.xml | 16 ++++++++++++++++ pydocx/tests/test_xml.py | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 pydocx/tests/templates/sdt.xml diff --git a/pydocx/tests/document_builder.py b/pydocx/tests/document_builder.py index 7286cc8e..c3289bba 100644 --- a/pydocx/tests/document_builder.py +++ b/pydocx/tests/document_builder.py @@ -9,6 +9,7 @@ 'p': 'p.xml', 'pict': 'pict.xml', 'r': 'r.xml', + 'sdt': 'sdt.xml', 'sectPr': 'sectPr.xml', 'smartTag': 'smart_tag.xml', 'style': 'style.xml', @@ -85,6 +86,14 @@ def smart_tag(self, run_tags): } return template.render(**kwargs) + @classmethod + def sdt_tag(self, p_tag): + template = env.get_template(templates['sdt']) + kwargs = { + 'p_tag': p_tag, + } + return template.render(**kwargs) + @classmethod def li(self, text, ilvl, numId, bold=False): if isinstance(text, str): diff --git a/pydocx/tests/templates/sdt.xml b/pydocx/tests/templates/sdt.xml new file mode 100644 index 00000000..fe9a7e77 --- /dev/null +++ b/pydocx/tests/templates/sdt.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + {{ p_tag }} + + diff --git a/pydocx/tests/test_xml.py b/pydocx/tests/test_xml.py index caa9f211..86aea933 100644 --- a/pydocx/tests/test_xml.py +++ b/pydocx/tests/test_xml.py @@ -764,6 +764,7 @@ class SameNumIdInTable(_TranslationTestCase): ''' + # Ensure its not failing somewhere and falling back to decimal numbering_dict = { '1': { @@ -786,5 +787,28 @@ def get_xml(self): lis += table lis += DXB.li(text='CCC', ilvl=0, numId=1) body = lis + + xml = DXB.xml(body) + return xml + + +class SDTTestCase(_TranslationTestCase): + expected_output = ''' + +
    +
  1. AAA
    + BBB +
  2. +
  3. CCC
  4. +
+ + ''' + + def get_xml(self): + body = '' + body += DXB.li(text='AAA', ilvl=0, numId=0) + body += DXB.sdt_tag(p_tag=DXB.p_tag(text='BBB')) + body += DXB.li(text='CCC', ilvl=0, numId=0) + xml = DXB.xml(body) return xml From e2029cdddc53be6ef4ba4fd28ee39c7a080d1698 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 May 2013 17:02:09 -0400 Subject: [PATCH 2/2] refs #27: correctly handle sdt tags (including break separating) --- pydocx/DocxParser.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 3bf9b295..ca62319c 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -334,7 +334,7 @@ def _set_next(self, body): def _get_children(el): # We only care about children if they have text in them. children = [] - for child in self._filter_children(el, ['p', 'tbl']): + for child in self._filter_children(el, ['p', 'tbl', 'sdt']): has_descendant_with_tag = False if child.has_descendant_with_tag('t'): has_descendant_with_tag = True @@ -555,15 +555,19 @@ def parse_p(self, el, text): return parsed def _should_append_break_tag(self, next_el): + paragraph_like_tags = [ + 'p', + 'sdt', + ] if next_el.is_list_item: return False if next_el.previous is None: return False if next_el.previous.is_last_list_item_in_root: return False - if next_el.previous.tag != 'p': + if next_el.previous.tag not in paragraph_like_tags: return False - if next_el.tag != 'p': + if next_el.tag not in paragraph_like_tags: return False return True