sdoc.sdoc2.formatter.html.FigureHtmlFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 82.43 %

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
wmc 5
eloc 30
dl 61
loc 74
ccs 12
cts 26
cp 0.4615
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A FigureHtmlFormatter._write_caption() 17 17 3
A FigureHtmlFormatter.write_into_file() 24 24 1
A FigureHtmlFormatter.generate() 10 10 1

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 1
from typing import Any
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.FigureNode import FigureNode
6 1
from sdoc.sdoc2.NodeStore import NodeStore
7
8
9 1 View Code Duplication
class FigureHtmlFormatter(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 figures.
12
    """
13
14
    # ------------------------------------------------------------------------------------------------------------------
15 1
    def generate(self, node: FigureNode, file: Any) -> None:
16
        """
17
        Generates the HTML code for a figure node.
18
19
        :param FigureNode node: The figure node.
20
        :param any file: The output file.
21
        """
22
        self.write_into_file(node, file)
23
24
        HtmlFormatter.generate(self, node, file)
25
26
    # ------------------------------------------------------------------------------------------------------------------
27 1
    @staticmethod
28 1
    def _write_caption(node: FigureNode, file: Any) -> None:
29
        """
30
        Generates the caption for the table in HTML representation.
31
32
        :param FigureNode node: The figure node.
33
        :param any file: The output file.
34
        """
35
        if node.caption:
36
            figure_number = node.get_option_value('number')
37
38
            if figure_number:
39
                inner_text = 'Figuur {}: {}'.format(figure_number, node.caption)  # TODO Internationalization
40
            else:
41
                inner_text = node.caption
42
43
            file.write(Html.generate_element('figcaption', {}, inner_text))
44
45
    # ------------------------------------------------------------------------------------------------------------------
46 1
    @staticmethod
47 1
    def write_into_file(node: FigureNode, file: Any) -> None:
48
        """
49
        Writes data into opened HTML file.
50
51
        :param FigureNode node: The figure node.
52
        :param any file: The output file.
53
        """
54
        # Creating dicts with attributes for each type of element.
55
        figure_attributes = {'id': node.get_option_value('id')}
56
57
        img_attributes = {'src':    node.get_option_value('src'),
58
                          'width':  node.get_option_value('width'),
59
                          'height': node.get_option_value('height'),
60
                          'alt':    node.caption}
61
62
        # Creating elements.
63
        file.write(Html.generate_tag('figure', figure_attributes))
64
65
        file.write(Html.generate_void_element('img', img_attributes))
66
67
        FigureHtmlFormatter._write_caption(node, file)
68
69
        file.write('</figure>')
70
71
72
# ----------------------------------------------------------------------------------------------------------------------
73
NodeStore.register_formatter('figure', 'html', FigureHtmlFormatter)
74