Completed
Pull Request — master (#43)
by Oleg
02:07
created

TocHtmlFormatter.write_elements()   A

Complexity

Conditions 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.729

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 1
cts 10
cp 0.1
rs 9.4285
cc 1
crap 1.729
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 import node_store
11 1
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
12
13
14 1
class TocHtmlFormatter(HtmlFormatter):
15
    """
16
    HtmlFormatter for generating HTML code for table of contents.
17
    """
18
19
    # ------------------------------------------------------------------------------------------------------------------
20 1
    def generate(self, node, file):
21
        """
22
        Generates the HTML code for a table of contents node.
23
24
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
25
        :param file file: The output file.
26
        """
27
        self.write_into_file(node, file)
28
29
        super().generate(node, file)
30
31
    # ------------------------------------------------------------------------------------------------------------------
32 1
    def generate_chapter(self, node, file):
33
        """
34
        Generates the HTML code for a table of contents node.
35
36
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
37
        :param file file: The output file.
38
        """
39
        if file:
40
            self.write_into_file(node, file)
41
42
        super().generate_chapter(node, file)
43
44
    # ------------------------------------------------------------------------------------------------------------------
45 1
    @staticmethod
46
    def write_into_file(node, file):
47
        """
48
        Writes data into opened file.
49
50
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
51
        :param file file: The output file.
52
        """
53
        attributes = {'role': 'navigation', 'class': 'table-of-contents'}
54
55
        file.write(Html.generate_tag('nav', attributes))
56
        file.write(Html.generate_element('h1', {}, node.argument))
57
58
        file.write('<ul>')
59
        TocHtmlFormatter.write_contents(node, file)
60
        file.write('</ul>')
61
62
        file.write('</nav>')
63
64
    # ------------------------------------------------------------------------------------------------------------------
65 1
    @staticmethod
66
    def write_contents(node, file):
67
        """
68
        Writes the contents into file.
69
70
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
71
        :param file file: The output file.
72
        """
73
        depth = node.get_option_value('depth')
74
75
        for item in node.get_option_value('ids'):
76
77
            if depth and item['level'] <= int(depth):
78
                TocHtmlFormatter.write_elements(item, file)
79
80
            elif not depth:
81
                TocHtmlFormatter.write_elements(item, file)
82
83
    # ------------------------------------------------------------------------------------------------------------------
84 1
    @staticmethod
85
    def write_elements(item, file):
86
        """
87
        Write the containing elements.
88
89
        :param dict[str,str] item: The item which we outputs.
90
        :param file file: The output file.
91
        """
92
        class_attr = 'level{}'.format(item['level'])
93
        file.write(Html.generate_tag('li', {'class': class_attr}))
94
95
        id = '#{}'.format(item['id'])
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
96
        file.write(Html.generate_tag('a', {'href': id}))
97
98
        number = item['number']
99
        file.write(Html.generate_element('span', {}, number))
100
101
        file.write(' {}'.format(item['arg']))
102
        file.write('</a>')
103
        file.write('</li>')
104
105
# ----------------------------------------------------------------------------------------------------------------------
106
node_store.register_formatter('toc', 'html', TocHtmlFormatter)
107