Skip to content

Having only one list item in the root that is not last creates invalid html #24

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 9 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
37 changes: 23 additions & 14 deletions pydocx/DocxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,32 @@ def parse_list(self, el, text):
return self.parse_table_cell_contents(el, parsed)
return parsed

def _build_list(self, el, text):
# Get the list style for the pending list.
Copy link
Contributor

Choose a reason for hiding this comment

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

And this actually makes the code cleaner, anyway. Nice

lst_style = self.get_list_style(
el.num_id.num_id,
el.ilvl,
)

parsed = text
# Create the actual list and return it.
if lst_style == 'bullet':
return self.unordered_list(parsed)
else:
return self.ordered_list(
parsed,
lst_style,
)

def _parse_list(self, el, text):
parsed = self.parse_list_item(el, text)
num_id = el.num_id
ilvl = el.ilvl
# Everything after this point assumes the first element is not also the
# last. If the first element is also the last then early return by
# building and returning the completed list.
if el.is_last_list_item_in_root:
return self._build_list(el, parsed)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see a comment here. Why do we want to just call _build_list if it's the last list item in the root?

next_el = el.next

def is_same_list(next_el, num_id, ilvl):
Expand Down Expand Up @@ -523,20 +545,7 @@ def should_parse_last_el(last_el, first_el):
if parsed == '':
return parsed

# Get the list style for the pending list.
lst_style = self.get_list_style(
el.num_id.num_id,
el.ilvl,
)

# Create the actual list and return it.
if lst_style == 'bullet':
return self.unordered_list(parsed)
else:
return self.ordered_list(
parsed,
lst_style,
)
return self._build_list(el, parsed)

def parse_p(self, el, text):
if text == '':
Expand Down
19 changes: 10 additions & 9 deletions pydocx/parsers/Docx2Html.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,17 @@ def page_break(self):
def indent(self, text, just='', firstLine='', left='', right=''):
slug = '<div'
if just:
slug += " class='%(just)s"
slug += " class='%(just)s'"
if firstLine or left or right:
slug += "' style ="
if firstLine:
slug += "'text-indent:%(firstLine)spx;"
if left:
slug += "'margin-left:%(left)spx;"
if right:
slug += "'margin-right:%(right)spx;"
slug += "'>%(text)s</div>"
slug += " style='"
if firstLine:
slug += "text-indent:%(firstLine)spx;"
if left:
slug += "margin-left:%(left)spx;"
if right:
slug += "margin-right:%(right)spx;"
slug += "'"
slug += ">%(text)s</div>"
return slug % {
'text': text,
'just': just,
Expand Down
37 changes: 26 additions & 11 deletions pydocx/tests/test_docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,17 +784,32 @@ def test_justification():
)
actual_html = convert(file_path)
assert_html_equal(actual_html, '''
<html><body><p><div class='center'>Center Justified</div>
</p><p><div class='right'>Right justified</div></p>
<p><div class='right' style ='margin-right:96.0px;'>
Right justified and pushed in from right</div></p>
<p><div class='center' style ='margin-left:252.0px;'margin-right:96.0px;'>
Center justified and pushed in from left and it is
great and it is the coolest thing of all time and I like it and
I think it is cool</div></p><p>
<div' style ='margin-left:252.0px;'margin-right:96.0px;'>
Left justified and pushed in from left</div></p></body></html>
''')
<html><body>
<p>
<div class='center'>Center Justified</div>
</p>
<p>
<div class='right'>Right justified</div>
</p>
<p>
<div class='right' style='margin-right:96.0px;'>
Right justified and pushed in from right
</div>
</p>
<p>
<div class='center' style='margin-left:252.0px;margin-right:96.0px;'>
Center justified and pushed in from left and it is
great and it is the coolest thing of all time and I like it and
I think it is cool
</div>
</p>
<p>
<div style='margin-left:252.0px;margin-right:96.0px;'>
Left justified and pushed in from left
</div>
</p>
</body></html>
''')


def _converter(*args, **kwargs):
Expand Down
28 changes: 28 additions & 0 deletions pydocx/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,34 @@ def get_xml(self):
return xml


class SingleListItem(_TranslationTestCase):
expected_output = '''
<html><body>
<ol data-list-type="lower-alpha">
<li>AAA</li>
</ol>
<p>BBB</p>
</body></html>
'''

numbering_dict = {
'1': {
'0': 'lowerLetter',
}
}

def get_xml(self):
li = DXB.li(text='AAA', ilvl=0, numId=1)
p_tags = [
DXB.p_tag('BBB'),
]
body = li
for p_tag in p_tags:
body += p_tag
xml = DXB.xml(body)
return xml


class SimpleTableTest(_TranslationTestCase):
expected_output = '''
<html><body>
Expand Down