Skip to content

Commit 1b3bb13

Browse files
committed
Merge pull request #24 from OpenScienceFramework/issue_24
Having only one list item in the root that is not last creates invalid html
2 parents e6f277c + f52f0f5 commit 1b3bb13

File tree

4 files changed

+87
-34
lines changed

4 files changed

+87
-34
lines changed

pydocx/DocxParser.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,32 @@ def parse_list(self, el, text):
461461
return self.parse_table_cell_contents(el, parsed)
462462
return parsed
463463

464+
def _build_list(self, el, text):
465+
# Get the list style for the pending list.
466+
lst_style = self.get_list_style(
467+
el.num_id.num_id,
468+
el.ilvl,
469+
)
470+
471+
parsed = text
472+
# Create the actual list and return it.
473+
if lst_style == 'bullet':
474+
return self.unordered_list(parsed)
475+
else:
476+
return self.ordered_list(
477+
parsed,
478+
lst_style,
479+
)
480+
464481
def _parse_list(self, el, text):
465482
parsed = self.parse_list_item(el, text)
466483
num_id = el.num_id
467484
ilvl = el.ilvl
485+
# Everything after this point assumes the first element is not also the
486+
# last. If the first element is also the last then early return by
487+
# building and returning the completed list.
488+
if el.is_last_list_item_in_root:
489+
return self._build_list(el, parsed)
468490
next_el = el.next
469491

470492
def is_same_list(next_el, num_id, ilvl):
@@ -523,20 +545,7 @@ def should_parse_last_el(last_el, first_el):
523545
if parsed == '':
524546
return parsed
525547

526-
# Get the list style for the pending list.
527-
lst_style = self.get_list_style(
528-
el.num_id.num_id,
529-
el.ilvl,
530-
)
531-
532-
# Create the actual list and return it.
533-
if lst_style == 'bullet':
534-
return self.unordered_list(parsed)
535-
else:
536-
return self.ordered_list(
537-
parsed,
538-
lst_style,
539-
)
548+
return self._build_list(el, parsed)
540549

541550
def parse_p(self, el, text):
542551
if text == '':

pydocx/parsers/Docx2Html.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,17 @@ def page_break(self):
147147
def indent(self, text, just='', firstLine='', left='', right=''):
148148
slug = '<div'
149149
if just:
150-
slug += " class='%(just)s"
150+
slug += " class='%(just)s'"
151151
if firstLine or left or right:
152-
slug += "' style ="
153-
if firstLine:
154-
slug += "'text-indent:%(firstLine)spx;"
155-
if left:
156-
slug += "'margin-left:%(left)spx;"
157-
if right:
158-
slug += "'margin-right:%(right)spx;"
159-
slug += "'>%(text)s</div>"
152+
slug += " style='"
153+
if firstLine:
154+
slug += "text-indent:%(firstLine)spx;"
155+
if left:
156+
slug += "margin-left:%(left)spx;"
157+
if right:
158+
slug += "margin-right:%(right)spx;"
159+
slug += "'"
160+
slug += ">%(text)s</div>"
160161
return slug % {
161162
'text': text,
162163
'just': just,

pydocx/tests/test_docx.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -784,17 +784,32 @@ def test_justification():
784784
)
785785
actual_html = convert(file_path)
786786
assert_html_equal(actual_html, '''
787-
<html><body><p><div class='center'>Center Justified</div>
788-
</p><p><div class='right'>Right justified</div></p>
789-
<p><div class='right' style ='margin-right:96.0px;'>
790-
Right justified and pushed in from right</div></p>
791-
<p><div class='center' style ='margin-left:252.0px;'margin-right:96.0px;'>
792-
Center justified and pushed in from left and it is
793-
great and it is the coolest thing of all time and I like it and
794-
I think it is cool</div></p><p>
795-
<div' style ='margin-left:252.0px;'margin-right:96.0px;'>
796-
Left justified and pushed in from left</div></p></body></html>
797-
''')
787+
<html><body>
788+
<p>
789+
<div class='center'>Center Justified</div>
790+
</p>
791+
<p>
792+
<div class='right'>Right justified</div>
793+
</p>
794+
<p>
795+
<div class='right' style='margin-right:96.0px;'>
796+
Right justified and pushed in from right
797+
</div>
798+
</p>
799+
<p>
800+
<div class='center' style='margin-left:252.0px;margin-right:96.0px;'>
801+
Center justified and pushed in from left and it is
802+
great and it is the coolest thing of all time and I like it and
803+
I think it is cool
804+
</div>
805+
</p>
806+
<p>
807+
<div style='margin-left:252.0px;margin-right:96.0px;'>
808+
Left justified and pushed in from left
809+
</div>
810+
</p>
811+
</body></html>
812+
''')
798813

799814

800815
def _converter(*args, **kwargs):

pydocx/tests/test_xml.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,34 @@ def get_xml(self):
718718
return xml
719719

720720

721+
class SingleListItem(_TranslationTestCase):
722+
expected_output = '''
723+
<html><body>
724+
<ol data-list-type="lower-alpha">
725+
<li>AAA</li>
726+
</ol>
727+
<p>BBB</p>
728+
</body></html>
729+
'''
730+
731+
numbering_dict = {
732+
'1': {
733+
'0': 'lowerLetter',
734+
}
735+
}
736+
737+
def get_xml(self):
738+
li = DXB.li(text='AAA', ilvl=0, numId=1)
739+
p_tags = [
740+
DXB.p_tag('BBB'),
741+
]
742+
body = li
743+
for p_tag in p_tags:
744+
body += p_tag
745+
xml = DXB.xml(body)
746+
return xml
747+
748+
721749
class SimpleTableTest(_TranslationTestCase):
722750
expected_output = '''
723751
<html><body>

0 commit comments

Comments
 (0)