Completed
Push — master ( c0c100...b6926c )
by P.R.
02:23
created

TableHtmlFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 12.5%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 14
c 4
b 2
f 0
dl 0
loc 120
ccs 6
cts 48
cp 0.125
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 19 1
A _get_align() 0 14 2
A _generate_caption() 0 20 3
B _generate_table_cell() 0 24 3
B _generate_table_body() 0 29 5
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
            table_number = node.get_option_value('number')
52
53
            if table_number:
54
                inner_text = 'Tabel {}: {}'.format(table_number, node.caption)
55
            else:
56
                inner_text = node.caption
57
58
            return Html.generate_element('caption', {}, inner_text)
59
60
        return ''
61
62
    # ------------------------------------------------------------------------------------------------------------------
63 1
    @staticmethod
64
    def _generate_table_body(node):
65
        """
66
        Generates table with header.
67
68
        :param sdoc.sdoc2.node.TableNode.TableNode node: The table node.
69
70
        :rtype: str
71
        """
72
        html = '<tbody>'
73
74
        if node.column_headers:
75
            html += '<tr>'
76
            for column in node.column_headers:
77
                html += Html.generate_element('th', {}, column)
78
            html += '</tr>'
79
80
        for row in node.rows:
81
            header_column_counter = 0
82
            html += '<tr>'
83
            for col in row:
84
                align = TableHtmlFormatter._get_align(node.alignments, header_column_counter)
85
                html += TableHtmlFormatter._generate_table_cell(align, col)
86
                header_column_counter += 1
87
            html += '</tr>'
88
89
        html += '</tbody>'
90
91
        return html
92
93
    # ------------------------------------------------------------------------------------------------------------------
94 1
    @staticmethod
95
    def _generate_table_cell(align, col):
96
        """
97
        Returns the 'column' with HTML data.
98
99
        :param mixed col: The column in a table.
100
101
        :rtype: str
102
        """
103
        attributes = {}
104
105
        if align:
106
            attributes['style'] = "text-align: {0}".format(align)
107
108
        if isinstance(col, str):
109
            data = col
110
            is_html = False
111
        else:
112
            # Generates html in nested node ('col') with specified formatter.
113
            formatter = NodeStore.get_formatter('html', col.get_command())
114
            data = formatter.get_html(col)
115
            is_html = True
116
117
        return Html.generate_element('td', attributes, data, is_html)
118
119
    # ------------------------------------------------------------------------------------------------------------------
120 1
    @staticmethod
121
    def _get_align(align_list, column):
122
        """
123
        Returns the align or None.
124
125
        :param list[str|None] align_list: The list with alignments.
126
        :param int column: The number of column.
127
128
        :rtype: list[str|None] | None
129
        """
130
        if column in align_list:
131
            return align_list[column]
132
133
        return None
134
135
136
# ----------------------------------------------------------------------------------------------------------------------
137
NodeStore.register_formatter('table', 'html', TableHtmlFormatter)
138