From 2579ff0c2fbb4b0034dd69c1f64a99dd5328dd27 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Thu, 16 May 2013 15:41:03 -0400 Subject: [PATCH 1/5] refs #24: added a test showing that certain lists generate invalid html. Also updated the justification test case to generate valid html as well. --- pydocx/tests/test_docx.py | 6 +++--- pydocx/tests/test_xml.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pydocx/tests/test_docx.py b/pydocx/tests/test_docx.py index 19f8d48d..03a54753 100644 --- a/pydocx/tests/test_docx.py +++ b/pydocx/tests/test_docx.py @@ -750,13 +750,13 @@ def test_justification(): assert_html_equal(actual_html, '''

Center Justified

Right justified

-

+

Right justified and pushed in from right

-

+

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

- +

Left justified and pushed in from left

''') diff --git a/pydocx/tests/test_xml.py b/pydocx/tests/test_xml.py index 8b6d04aa..405e5981 100644 --- a/pydocx/tests/test_xml.py +++ b/pydocx/tests/test_xml.py @@ -716,3 +716,31 @@ def get_xml(self): xml = DXB.xml(body) return xml + + +class TestCase(_TranslationTestCase): + expected_output = ''' + +
    +
  1. AAA
  2. +
+

BBB

+ + ''' + + 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 From cdaed19a041a9103e9510540d17d2403b00515ef Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Thu, 16 May 2013 15:44:18 -0400 Subject: [PATCH 2/5] refs #24: fixed the justification handler --- pydocx/parsers/Docx2Html.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pydocx/parsers/Docx2Html.py b/pydocx/parsers/Docx2Html.py index 87b8ed49..f97e39be 100644 --- a/pydocx/parsers/Docx2Html.py +++ b/pydocx/parsers/Docx2Html.py @@ -147,16 +147,17 @@ def page_break(self): def indent(self, text, just='', firstLine='', left='', right=''): slug = ' Date: Thu, 16 May 2013 15:44:43 -0400 Subject: [PATCH 3/5] refs #24: Early return if the first list element is also the last. --- pydocx/DocxParser.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 1654b912..bfd2c030 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -426,10 +426,29 @@ 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. + lst_style = self.get_list_style( + el.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 + if el.is_last_list_item_in_root: + return self._build_list(el, parsed) next_el = el.next def is_same_list(next_el, num_id, ilvl): @@ -488,20 +507,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, - 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 == '': From ca82b6ce73193367f03a6c21f087e1021224e63d Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 17 May 2013 13:56:31 -0400 Subject: [PATCH 4/5] refs #24: test cleanup --- pydocx/tests/test_docx.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/pydocx/tests/test_docx.py b/pydocx/tests/test_docx.py index 03a54753..f19f1898 100644 --- a/pydocx/tests/test_docx.py +++ b/pydocx/tests/test_docx.py @@ -748,17 +748,32 @@ def test_justification(): ) actual_html = convert(file_path) assert_html_equal(actual_html, ''' -

Center Justified
-

Right justified

-

-Right justified and pushed in from right

-

-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

-

-Left justified and pushed in from left

-''') + +

+

Center Justified
+

+

+

Right justified
+

+

+

+ Right justified and pushed in from right +
+

+

+

+ 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 +
+

+

+

+ Left justified and pushed in from left +
+

+ + ''') def _converter(*args, **kwargs): From aacf1ae050b54742afbfadfb74a21ff7425a6f5c Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Tue, 21 May 2013 10:46:33 -0400 Subject: [PATCH 5/5] refs #24: added a comment --- pydocx/DocxParser.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 3f53ca3a..e13604f1 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -482,6 +482,9 @@ 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) next_el = el.next