Completed
Push — master ( 07bd4f...72d0af )
by P.R.
02:34
created

TableHtmlFormatter.generate_table()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 9.3211

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 1
cts 9
cp 0.1111
rs 9.4285
c 1
b 0
f 0
cc 3
crap 9.3211
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
        attributes = {'class': node.get_option_value('class'),
28
                      'id':    node.get_option_value('id')}
29
30
        html = Html.generate_tag('table', attributes)
31
32
        html += TableHtmlFormatter._generate_caption(node)
33
34
        html += self._generate_table_body(node)
35
36
        html += '</table>'
37
38
        file.write(html)
39
40
    # ------------------------------------------------------------------------------------------------------------------
41 1
    @staticmethod
42
    def _generate_caption(node):
43
        """
44
        Generates the caption for the table in HTML representation.
45
46
        :param sdoc.sdoc2.node.TableNode.TableNode node: The table node.
47
48
        :rtype: str
49
        """
50
        if node.caption:
51
            return Html.generate_element('caption', {}, node.caption)
52
53
        return ''
54
55
    # ------------------------------------------------------------------------------------------------------------------
56 1
    @staticmethod
57
    def _generate_table_body(node):
58
        """
59
        Generates table with header.
60
61
        :param sdoc.sdoc2.node.TableNode.TableNode node: The table node.
62
63
        :rtype: str
64
        """
65
        html = '<tbody>'
66
67
        if node.column_headers:
68
            html += '<tr>'
69
            for column in node.column_headers:
70
                html += Html.generate_element('th', {}, column)
71
            html += '</tr>'
72
73
        for row in node.rows:
74
            header_column_counter = 0
75
            html += '<tr>'
76
            for col in row:
77
                align = TableHtmlFormatter._get_align(node.alignments, header_column_counter)
78
                html += TableHtmlFormatter._generate_table_cell(align, col)
79
                header_column_counter += 1
80
            html += '</tr>'
81
82
        html += '</tbody>'
83
84
        return html
85
86
    # ------------------------------------------------------------------------------------------------------------------
87 1
    @staticmethod
88
    def _generate_table_cell(align, col):
89
        """
90
        Returns the 'column' with HTML data.
91
92
        :param mixed col: The column in a table.
93
94
        :rtype: str
95
        """
96
        attributes = {}
97
98
        if align:
99
            attributes['style'] = "text-align: {0}".format(align)
100
101
        if isinstance(col, str):
102
            data = col
103
            is_html = False
104
        else:
105
            # Generates html in nested node ('col') with specified formatter.
106
            formatter = NodeStore.get_formatter('html', col.get_command())
107
            data = formatter.get_html(col)
108
            is_html = True
109
110
        return Html.generate_element('td', attributes, data, is_html)
111
112
    # ------------------------------------------------------------------------------------------------------------------
113 1
    @staticmethod
114
    def _get_align(align_list, column):
115
        """
116
        Returns the align or None.
117
118
        :param list[str|None] align_list: The list with alignments.
119
        :param int column: The number of column.
120
121
        :rtype: list[str|None] | None
122
        """
123
        if column in align_list:
124
            return align_list[column]
125
126
        return None
127
128
129
# ----------------------------------------------------------------------------------------------------------------------
130
NodeStore.register_formatter('table', 'html', TableHtmlFormatter)
131