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

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 66.04 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 4
eloc 24
dl 35
loc 53
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A HeadingHtmlFormatter.generate() 9 9 1
A HeadingHtmlFormatter.generate_heading_node() 18 18 3

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.HeadingNode import HeadingNode
6 1
from sdoc.sdoc2.NodeStore import NodeStore
7
8
9 1 View Code Duplication
class HeadingHtmlFormatter(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 headings.
12
    """
13
14
    # ------------------------------------------------------------------------------------------------------------------
15 1
    def generate(self, node: HeadingNode, file: Any) -> None:
16
        """
17
        Generates the HTML code for a heading node.
18
19
        :param HeadingNode node: The heading node.
20
        :param any file: The output file.
21
        """
22 1
        self.generate_heading_node(node, file)
23 1
        HtmlFormatter.generate(self, node, file)
24
25
    # ------------------------------------------------------------------------------------------------------------------
26 1
    @staticmethod
27 1
    def generate_heading_node(node: HeadingNode, file: Any) -> None:
28
        """
29
        Generates the HTML code for heading node.
30
31
        :param HeadingNode node: The heading node.
32
        :param any file: The output file.
33
        """
34
        # Set id attribute to heading node.
35 1
        attributes = {'id': node.get_option_value('id')}
36
37 1
        if node.numbering:
38 1
            number = node.get_option_value('number')
39 1
            text_in_tag = '{0} {1}'.format('' if not number else number, node.argument)
40
        else:
41
            text_in_tag = '{0}'.format(node.argument)
42
43 1
        file.write(Html.generate_element('h{0:d}'.format(node.get_hierarchy_level() + 2), attributes, text_in_tag))
44
45
46
# ----------------------------------------------------------------------------------------------------------------------
47 1
NodeStore.register_formatter('part', 'html', HeadingHtmlFormatter)
48 1
NodeStore.register_formatter('chapter', 'html', HeadingHtmlFormatter)
49 1
NodeStore.register_formatter('section', 'html', HeadingHtmlFormatter)
50 1
NodeStore.register_formatter('subsection', 'html', HeadingHtmlFormatter)
51 1
NodeStore.register_formatter('sub2section', 'html', HeadingHtmlFormatter)
52
NodeStore.register_formatter('sub3section', 'html', HeadingHtmlFormatter)
53