Skip to content

Documents with sdt tags break content ordering #27

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

Merged
merged 4 commits into from
May 21, 2013
Merged
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
9 changes: 7 additions & 2 deletions pydocx/DocxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
TAGS_HOLDING_CONTENT_TAGS = (
'p',
'tbl',
'sdt',
)


Expand Down Expand Up @@ -573,15 +574,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

Expand Down
9 changes: 9 additions & 0 deletions pydocx/tests/document_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'p': 'p.xml',
'pict': 'pict.xml',
'r': 'r.xml',
'sdt': 'sdt.xml',
'sectPr': 'sectPr.xml',
'smartTag': 'smart_tag.xml',
'style': 'style.xml',
Expand Down Expand Up @@ -95,6 +96,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):
Expand Down
16 changes: 16 additions & 0 deletions pydocx/tests/templates/sdt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:sz w:val="22"/>
</w:rPr>
<w:alias w:val="PolicyTemplateTitle"/>
<w:tag w:val="PolicyTemplateTitle"/>
<w:id w:val="95087797"/>
<w:lock w:val="sdtLocked"/>
<w:text/>
</w:sdtPr>
<w:sdtContent>
{{ p_tag }}
</w:sdtContent>
</w:sdt>
24 changes: 24 additions & 0 deletions pydocx/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ class SameNumIdInTable(_TranslationTestCase):
</ol>
</body></html>
'''

# Ensure its not failing somewhere and falling back to decimal
numbering_dict = {
'1': {
Expand All @@ -913,5 +914,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 = '''
<html><body>
<ol data-list-type="decimal">
<li>AAA<br/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I gather from this, it looks like this should be in-line and not have a <br /> separating the two parts. Do you have any example document to verify this with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also be handled in #30

BBB
</li>
<li>CCC</li>
</ol>
</body></html>
'''

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