Passed
Push — master ( 8ff2fc...2dab2b )
by P.R.
04:18 queued 10s
created

TableHtmlFormatter._generate_table_cell()   A

Complexity

Conditions 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 8.2077

Importance

Changes 0
Metric Value
eloc 12
dl 23
loc 23
ccs 2
cts 12
cp 0.1666
rs 9.8
c 0
b 0
f 0
cc 3
nop 2
crap 8.2077
1 1
from typing import Any, List, Optional
2
3 1
from sdoc.helper.Html import Html
4 1
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
5 1
from sdoc.sdoc2.node.TableNode import TableNode
6 1
from sdoc.sdoc2.NodeStore import NodeStore
7
8
9 1 View Code Duplication
class TableHtmlFormatter(HtmlFormatter):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
    """
11
    HtmlFormatter for generating HTML code for table.
12
    """
13
14
    # ------------------------------------------------------------------------------------------------------------------
15 1
    def generate(self, node: TableNode, file: Any) -> None:
16
        """
17
        Generates the HTML code for a table node.
18
19
        :param TableNode node: The table node.
20
        :param any file: The output file.
21
        """
22
        attributes = {'class': node.get_option_value('class'),
23
                      'id':    node.get_option_value('id')}
24
25
        html = Html.generate_tag('table', attributes)
26
27
        html += TableHtmlFormatter._generate_caption(node)
28
29
        html += self._generate_table_body(node)
30
31
        html += '</table>'
32
33
        file.write(html)
34
35
    # ------------------------------------------------------------------------------------------------------------------
36 1
    @staticmethod
37 1
    def _generate_caption(node: TableNode) -> str:
38
        """
39
        Generates the caption for the table in HTML representation.
40
41
        :param TableNode node: The table node.
42
43
        :rtype: str
44
        """
45
        if node.caption:
46
            table_number = node.get_option_value('number')
47
48
            if table_number:
49
                inner_text = 'Tabel {}: {}'.format(table_number, node.caption)  # TODO Internationalization
50
            else:
51
                inner_text = node.caption
52
53
            return Html.generate_element('caption', {}, inner_text)
54
55
        return ''
56
57
    # ------------------------------------------------------------------------------------------------------------------
58 1
    @staticmethod
59 1
    def _generate_table_body(node: TableNode) -> str:
60
        """
61
        Generates table with header.
62
63
        :param TableNode node: The table node.
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 1
    def _generate_table_cell(align: Optional[str], cell: Any) -> str:
89
        """
90
        Returns the 'column' with HTML data.
91
92
        :param str|None align:
93
        :param any cell: The column in a table.
94
        """
95
        attributes = {}
96
97
        if align:
98
            attributes['style'] = "text-align: {0}".format(align)
99
100
        if isinstance(cell, str):
101
            data = cell
102
            is_html = False
103
        else:
104
            # Generates html in nested node ('cell') with specified formatter.
105
            formatter = NodeStore.get_formatter('html', cell.get_command())
106
            data = formatter.get_html(cell)
107
            is_html = True
108
109
        return Html.generate_element('td', attributes, data, is_html)
110
111
    # ------------------------------------------------------------------------------------------------------------------
112 1
    @staticmethod
113 1
    def _get_align(align_list: List[Optional[str]], column: int) -> Optional[List[Optional[str]]]:
114
        """
115
        Returns the align or None.
116
117
        :param list[str|None] align_list: The list with alignments.
118
        :param int column: The number of column.
119
        """
120
        if column in range(len(align_list)):
121
            return align_list[column]
122
123
        return None
124
125
126
# ----------------------------------------------------------------------------------------------------------------------
127
NodeStore.register_formatter('table', 'html', TableHtmlFormatter)
128