Skip to content

Commit c8649fc

Browse files
committed
Merge pull request #29 from OpenScienceFramework/issue_29
Stop overriding the handlers in the test cases
2 parents a23937a + 50cc9ed commit c8649fc

File tree

5 files changed

+256
-420
lines changed

5 files changed

+256
-420
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ OR, let's say FOO is your new favorite markup language. Simply customize your ow
166166

167167
The base parser `Docx2Html` relies on certain css class being set for certain behaviour to occur. Currently these include:
168168

169-
* class `insert` -> Turns the text green.
170-
* class `delete` -> Turns the text red and draws a line through the text.
171-
* class `center` -> Aligns the text to the center.
172-
* class `right` -> Aligns the text to the right.
173-
* class `left` -> Aligns the text to the left.
174-
* class `comment` -> Turns the text blue.
169+
* class `pydocx-insert` -> Turns the text green.
170+
* class `pydocx-delete` -> Turns the text red and draws a line through the text.
171+
* class `pydocx-center` -> Aligns the text to the center.
172+
* class `pydocx-right` -> Aligns the text to the right.
173+
* class `pydocx-left` -> Aligns the text to the left.
174+
* class `pydocx-comment` -> Turns the text blue.
175175
* class `pydocx-underline` -> Underlines the text.

pydocx/parsers/Docx2Html.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from pydocx.DocxParser import DocxParser
22

33
import xml.sax.saxutils
4-
import textwrap
54

65

76
class Docx2Html(DocxParser):
@@ -21,16 +20,22 @@ def head(self):
2120
}
2221

2322
def style(self):
24-
return textwrap.dedent('''<style>.insert{{color:red}}.delete
25-
{{color:red; text-decoration:line-through}}.center
26-
{{text-align:center}}.right{{text-align:right}}
27-
.left{{text-align:left}} .comment{{color:blue}}
28-
.pydocx-underline {text-decoration: underline;}
29-
body{{width:%(width)spx; margin:0px auto;
30-
}}</style>''') % {
23+
result = (
24+
'<style>'
25+
'.pydocx-insert {color:green;}'
26+
'.pydocx-delete {color:red;text-decoration:line-through;}'
27+
'.pydocx-center {text-align:center;}'
28+
'.pydocx-right {text-align:right;}'
29+
'.pydocx-left {text-align:left;}'
30+
'.pydocx-comment {color:blue;}'
31+
'.pydocx-underline {text-decoration: underline;}'
32+
'body {width:%(width)spx;margin:0px auto;}'
33+
'</style>'
34+
) % {
35+
#multiple by (4/3) to get to px
3136
'width': (self.page_width * (4 / 3)),
3237
}
33-
#multiple by (4/3) to get to px
38+
return result
3439

3540
def escape(self, text):
3641
return xml.sax.saxutils.quoteattr(text)[1:-1]
@@ -49,7 +54,7 @@ def heading(self, text, heading_value):
4954

5055
def insertion(self, text, author, date):
5156
return (
52-
"<span class='insert' author='%(author)s' "
57+
"<span class='pydocx-insert' author='%(author)s' "
5358
"date='%(date)s'>%(text)s</span>"
5459
) % {
5560
'author': author,
@@ -83,7 +88,7 @@ def image(self, path, x, y):
8388

8489
def deletion(self, text, author, date):
8590
return (
86-
"<span class='delete' author='%(author)s' "
91+
"<span class='pydocx-delete' author='%(author)s' "
8792
"date='%(date)s'>%(text)s</span>"
8893
) % {
8994
'author': author,
@@ -97,8 +102,9 @@ def list_element(self, text):
97102
}
98103

99104
def ordered_list(self, text, list_style):
100-
return "<ol>%(text)s</ol>" % {
105+
return '<ol list-style-type="%(list_style)s">%(text)s</ol>' % {
101106
'text': text,
107+
'list_style': list_style,
102108
}
103109

104110
def unordered_list(self, text):
@@ -121,7 +127,7 @@ def tab(self):
121127
return '&nbsp&nbsp&nbsp&nbsp'
122128

123129
def table(self, text):
124-
return '<table border=1>' + text + '</table>'
130+
return '<table border="1">' + text + '</table>'
125131

126132
def table_row(self, text):
127133
return '<tr>' + text + '</tr>'
@@ -145,7 +151,7 @@ def page_break(self):
145151
def indent(self, text, just='', firstLine='', left='', right=''):
146152
slug = '<div'
147153
if just:
148-
slug += " class='%(just)s'"
154+
slug += " class='pydocx-%(just)s'"
149155
if firstLine or left or right:
150156
slug += " style='"
151157
if firstLine:

pydocx/tests/__init__.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
)
1212
from unittest import TestCase
1313

14+
STYLE = (
15+
'<style>'
16+
'.pydocx-insert {color:green;}'
17+
'.pydocx-delete {color:red;text-decoration:line-through;}'
18+
'.pydocx-center {text-align:center;}'
19+
'.pydocx-right {text-align:right;}'
20+
'.pydocx-left {text-align:left;}'
21+
'.pydocx-comment {color:blue;}'
22+
'.pydocx-underline {text-decoration: underline;}'
23+
'body {width:612px;margin:0px auto;}'
24+
'</style>'
25+
)
26+
27+
BASE_HTML = '''
28+
<html>
29+
<head>
30+
%s
31+
</head>
32+
<body>%%s</body>
33+
</html>
34+
''' % STYLE
35+
1436

1537
def assert_html_equal(actual_html, expected_html):
1638
assert collapse_html(
@@ -78,6 +100,10 @@ def _build_data(
78100
remove_namespaces(document_xml),
79101
)
80102

103+
# This is the standard page width for a word document, Also the page
104+
# width that we are looking for in the test.
105+
self.page_width = 612
106+
81107
def _parse_rels_root(self, *args, **kwargs):
82108
if self._test_rels_dict is None:
83109
return {}
@@ -92,38 +118,15 @@ def get_list_style(self, num_id, ilvl):
92118
def _parse_styles(self):
93119
return {}
94120

95-
def head(self):
96-
return ''
97-
98-
def table(self, text):
99-
return '<table>' + text + '</table>'
100-
101-
def ordered_list(self, text, list_style):
102-
list_type_conversions = {
103-
'decimal': 'decimal',
104-
'decimalZero': 'decimal-leading-zero',
105-
'upperRoman': 'upper-roman',
106-
'lowerRoman': 'lower-roman',
107-
'upperLetter': 'upper-alpha',
108-
'lowerLetter': 'lower-alpha',
109-
'ordinal': 'decimal',
110-
'cardinalText': 'decimal',
111-
'ordinalText': 'decimal',
112-
}
113-
return '<ol data-list-type="{list_style}">{text}</ol>'.format(
114-
list_style=list_type_conversions.get(list_style, 'decimal'),
115-
text=text,
116-
)
117-
118121

119122
DEFAULT_NUMBERING_DICT = {
120123
'1': {
121124
'0': 'decimal',
122125
'1': 'decimal',
123126
},
124127
'2': {
125-
'0': 'none',
126-
'1': 'none',
128+
'0': 'lowerLetter',
129+
'1': 'lowerLetter',
127130
},
128131
}
129132

@@ -159,4 +162,4 @@ def test_expected_output(self):
159162
numbering_dict=self.numbering_dict,
160163
).parsed
161164

162-
assert_html_equal(html, self.expected_output)
165+
assert_html_equal(html, BASE_HTML % self.expected_output)

0 commit comments

Comments
 (0)