Completed
Push — master ( 4c8e7d...ffbd85 )
by P.R.
01:53
created

TableHtmlFormatter.generate_column()   B

Complexity

Conditions 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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