Completed
Push — master ( 793cf7...172351 )
by P.R.
04:33
created

TableHtmlFormatter.write_into_file()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.5185

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 1
cts 7
cp 0.1429
rs 9.4285
c 0
b 0
f 0
cc 2
crap 4.5185
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 import node_store
11 1
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
12 1
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 1
class TableHtmlFormatter(HtmlFormatter):
16
    """
17
    HtmlFormatter for generating HTML code for table.
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
40
        if node.column_headers:
41
            rows = self.generate_table_with_header(node)
42
        else:
43
            rows = self.generate_table(node)
44
45
        html_table = Html.generate_element('table', table_attrs, rows, True)
46
47
        file.write(html_table)
48
49
    # ------------------------------------------------------------------------------------------------------------------
50 1
    @staticmethod
51
    def generate_table(node):
52
        """
53
        Generates table without header.
54
55
        :param sdoc.sdoc2.node.TableNode.TableNode node: The table node.
56
57
        :rtype: str
58
        """
59
        columns = ''
60
        rows = ''
61
62
        for row in node.rows:
63
            for column in row:
64
                columns += Html.generate_element('td', {}, column)
65
            rows += Html.generate_element('tr', {}, columns, True)
66
            columns = ''
67
68
        return rows
69
70
    # ------------------------------------------------------------------------------------------------------------------
71 1
    @staticmethod
72
    def generate_table_with_header(node):
73
        """
74
        Generates table with header.
75
76
        :param sdoc.sdoc2.node.TableNode.TableNode node: The table node.
77
78
        :rtype: str
79
        """
80
        table_header = ''
81
        columns = ''
82
        rows = ''
83
84
        for column in node.column_headers:
85
            table_header += Html.generate_element('th', {}, column, True)
86
        table_header = Html.generate_element('tr', {}, table_header, True)
87
88
        header_column_counter = 0
89
90
        for row in node.rows:
91
            for col in row:
92
                align = TableHtmlFormatter.get_align(node.alignments, header_column_counter)
93
94
                columns += TableHtmlFormatter.generate_column(align, col)
95
96
                header_column_counter += 1
97
            rows += Html.generate_element('tr', {}, columns, True)
98
            columns = ''
99
            header_column_counter = 0
100
101
        return table_header + rows
102
103
    # ------------------------------------------------------------------------------------------------------------------
104 1
    @staticmethod
105
    def generate_column(align, col):
106
        """
107
        Returns the 'column' with HTML data.
108
109
        :param mixed col: The column in a table.
110
111
        :rtype: str
112
        """
113
        if isinstance(col, str):
114
            if align:
115
                column = Html.generate_element('td', {'style': "text-align: {0}".format(align)}, col)
116
            else:
117
                column = Html.generate_element('td', {}, col)
118
119
        else:
120
            # Generates html in nested node ('col') with specified formatter.
121
            formatter = node_store.get_formatter('html', col.get_command())
122
            node_html = formatter.get_html(col)
123
124
            if align:
125
                column = Html.generate_element('td',
126
                                               {'style': "text-align: {0}".format(align)},
127
                                               node_html,
128
                                               is_html=True)
129
            else:
130
                column = Html.generate_element('td', {}, node_html, is_html=True)
131
132
        return column
133
134
    # ------------------------------------------------------------------------------------------------------------------
135 1
    @staticmethod
136
    def get_align(align_list, column):
137
        """
138
        Returns the align or None.
139
140
        :param list[str|None] align_list: The list with alignments.
141
        :param int column: The number of column.
142
143
        :rtype: list[str|None] | None
144
        """
145
        try:
146
            return align_list[column]
147
        except IndexError:
148
            return None
149
150
151
# ----------------------------------------------------------------------------------------------------------------------
152
node_store.register_formatter('table', 'html', TableHtmlFormatter)
153