1
|
|
|
""" |
2
|
|
|
SDoc |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
9
|
1 |
|
from sdoc.helper.Html import Html |
10
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
11
|
1 |
|
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class TableHtmlFormatter(HtmlFormatter): |
15
|
|
|
""" |
16
|
|
|
HtmlFormatter for generating HTML code for table. |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
20
|
1 |
|
def generate(self, node, file): |
21
|
|
|
""" |
22
|
|
|
Generates the HTML code for a table node. |
23
|
|
|
|
24
|
|
|
:param sdoc.sdoc2.node.TableNode.TableNode node: The table node. |
25
|
|
|
:param file file: The output file. |
26
|
|
|
""" |
27
|
|
|
self.write_into_file(node, file) |
28
|
|
|
|
29
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
30
|
1 |
|
def write_into_file(self, node, file): |
31
|
|
|
""" |
32
|
|
|
Writes data into opened file. |
33
|
|
|
|
34
|
|
|
:param sdoc.sdoc2.node.TableNode.TableNode node: The table node. |
35
|
|
|
:param file file: The output file. |
36
|
|
|
""" |
37
|
|
|
# Attributes for table. |
38
|
|
|
table_attrs = {'class': node.get_option_value('class'), |
39
|
|
|
'id': node.get_option_value('id')} |
40
|
|
|
|
41
|
|
|
rows = '' |
42
|
|
|
rows += TableHtmlFormatter.generate_caption(node) |
43
|
|
|
|
44
|
|
|
if node.column_headers: |
45
|
|
|
rows += self.generate_table_with_header(node) |
46
|
|
|
else: |
47
|
|
|
rows += self.generate_table(node) |
48
|
|
|
|
49
|
|
|
html_table = Html.generate_element('table', table_attrs, rows, True) |
50
|
|
|
|
51
|
|
|
file.write(html_table) |
52
|
|
|
|
53
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
54
|
1 |
|
@staticmethod |
55
|
|
|
def generate_caption(node): |
56
|
|
|
""" |
57
|
|
|
Generates the caption for the table in HTML representation. |
58
|
|
|
|
59
|
|
|
:param sdoc.sdoc2.node.TableNode.TableNode node: The table node. |
60
|
|
|
|
61
|
|
|
:rtype: str |
62
|
|
|
""" |
63
|
|
|
if node.caption: |
64
|
|
|
html_caption = Html.generate_element('caption', {}, node.caption, True) |
65
|
|
|
|
66
|
|
|
return html_caption |
67
|
|
|
|
68
|
|
|
else: |
69
|
|
|
return '' |
70
|
|
|
|
71
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
72
|
1 |
|
@staticmethod |
73
|
|
|
def generate_table(node): |
74
|
|
|
""" |
75
|
|
|
Generates table without header. |
76
|
|
|
|
77
|
|
|
:param sdoc.sdoc2.node.TableNode.TableNode node: The table node. |
78
|
|
|
|
79
|
|
|
:rtype: str |
80
|
|
|
""" |
81
|
|
|
columns = '' |
82
|
|
|
rows = '' |
83
|
|
|
|
84
|
|
|
for row in node.rows: |
85
|
|
|
for column in row: |
86
|
|
|
columns += Html.generate_element('td', {}, column) |
87
|
|
|
rows += Html.generate_element('tr', {}, columns, True) |
88
|
|
|
columns = '' |
89
|
|
|
|
90
|
|
|
return rows |
91
|
|
|
|
92
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
93
|
1 |
|
@staticmethod |
94
|
|
|
def generate_table_with_header(node): |
95
|
|
|
""" |
96
|
|
|
Generates table with header. |
97
|
|
|
|
98
|
|
|
:param sdoc.sdoc2.node.TableNode.TableNode node: The table node. |
99
|
|
|
|
100
|
|
|
:rtype: str |
101
|
|
|
""" |
102
|
|
|
table_header = '' |
103
|
|
|
columns = '' |
104
|
|
|
rows = '' |
105
|
|
|
|
106
|
|
|
for column in node.column_headers: |
107
|
|
|
table_header += Html.generate_element('th', {}, column, True) |
108
|
|
|
table_header = Html.generate_element('tr', {}, table_header, True) |
109
|
|
|
|
110
|
|
|
header_column_counter = 0 |
111
|
|
|
|
112
|
|
|
for row in node.rows: |
113
|
|
|
for col in row: |
114
|
|
|
align = TableHtmlFormatter.get_align(node.alignments, header_column_counter) |
115
|
|
|
|
116
|
|
|
columns += TableHtmlFormatter.generate_column(align, col) |
117
|
|
|
|
118
|
|
|
header_column_counter += 1 |
119
|
|
|
rows += Html.generate_element('tr', {}, columns, True) |
120
|
|
|
columns = '' |
121
|
|
|
header_column_counter = 0 |
122
|
|
|
|
123
|
|
|
return table_header + rows |
124
|
|
|
|
125
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
126
|
1 |
|
@staticmethod |
127
|
|
|
def generate_column(align, col): |
128
|
|
|
""" |
129
|
|
|
Returns the 'column' with HTML data. |
130
|
|
|
|
131
|
|
|
:param mixed col: The column in a table. |
132
|
|
|
|
133
|
|
|
:rtype: str |
134
|
|
|
""" |
135
|
|
|
if isinstance(col, str): |
136
|
|
|
if align: |
137
|
|
|
column = Html.generate_element('td', {'style': "text-align: {0}".format(align)}, col) |
138
|
|
|
else: |
139
|
|
|
column = Html.generate_element('td', {}, col) |
140
|
|
|
|
141
|
|
|
else: |
142
|
|
|
# Generates html in nested node ('col') with specified formatter. |
143
|
|
|
formatter = NodeStore.get_formatter('html', col.get_command()) |
144
|
|
|
node_html = formatter.get_html(col) |
145
|
|
|
|
146
|
|
|
if align: |
147
|
|
|
column = Html.generate_element('td', |
148
|
|
|
{'style': "text-align: {0}".format(align)}, |
149
|
|
|
node_html, |
150
|
|
|
is_html=True) |
151
|
|
|
else: |
152
|
|
|
column = Html.generate_element('td', {}, node_html, is_html=True) |
153
|
|
|
|
154
|
|
|
return column |
155
|
|
|
|
156
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
157
|
1 |
|
@staticmethod |
158
|
|
|
def get_align(align_list, column): |
159
|
|
|
""" |
160
|
|
|
Returns the align or None. |
161
|
|
|
|
162
|
|
|
:param list[str|None] align_list: The list with alignments. |
163
|
|
|
:param int column: The number of column. |
164
|
|
|
|
165
|
|
|
:rtype: list[str|None] | None |
166
|
|
|
""" |
167
|
|
|
try: |
168
|
|
|
return align_list[column] |
169
|
|
|
except IndexError: |
170
|
|
|
return None |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
174
|
|
|
NodeStore.register_formatter('table', 'html', TableHtmlFormatter) |
175
|
|
|
|