Test Failed
Push — master ( 714282...4cb748 )
by P.R.
01:55 queued 13s
created

sdoc.sdoc2.formatter.html.TableHtmlFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 89.84 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
eloc 60
dl 115
loc 128
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A TableHtmlFormatter.generate() 19 19 1
A TableHtmlFormatter._generate_caption() 20 20 3
A TableHtmlFormatter._get_align() 12 12 2
A TableHtmlFormatter._generate_table_cell() 23 23 3
A TableHtmlFormatter._generate_table_body() 27 27 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from typing import Any, List, Optional
2
3
from sdoc.helper.Html import Html
4
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
5
from sdoc.sdoc2.node.TableNode import TableNode
6
from sdoc.sdoc2.NodeStore import NodeStore
7
8
9 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
    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
    @staticmethod
37
    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
    @staticmethod
59
    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
    @staticmethod
88
    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
    @staticmethod
113
    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