Completed
Push — master ( f370bc...1b3b31 )
by P.R.
02:18
created

TocHtmlFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 17.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
dl 0
loc 76
ccs 5
cts 28
cp 0.1786
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 10 1
B write_contents() 0 17 5
A write_into_file() 0 18 1
A write_elements() 0 19 1
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.NodeStore import NodeStore
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
        HtmlFormatter.generate(self, node, file)
30
31
    # ------------------------------------------------------------------------------------------------------------------
32 1
    @staticmethod
33
    def write_into_file(node, file):
34
        """
35
        Writes data into opened file.
36
37
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
38
        :param file file: The output file.
39
        """
40
        attributes = {'role': 'navigation', 'class': 'table-of-contents'}
41
42
        file.write(Html.generate_tag('nav', attributes))
43
        file.write(Html.generate_element('h1', {}, node.argument))
44
45
        file.write('<ul>')
46
        TocHtmlFormatter.write_contents(node, file)
47
        file.write('</ul>')
48
49
        file.write('</nav>')
50
51
    # ------------------------------------------------------------------------------------------------------------------
52 1
    @staticmethod
53
    def write_contents(node, file):
54
        """
55
        Writes the contents into file.
56
57
        :param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node.
58
        :param file file: The output file.
59
        """
60
        depth = node.get_option_value('depth')
61
62
        for item in node.get_option_value('ids'):
63
64
            if depth and item['level'] <= int(depth):
65
                TocHtmlFormatter.write_elements(item, file)
66
67
            elif not depth:
68
                TocHtmlFormatter.write_elements(item, file)
69
70
    # ------------------------------------------------------------------------------------------------------------------
71 1
    @staticmethod
72
    def write_elements(item, file):
73
        """
74
        Write the containing elements.
75
76
        :param dict[str,str] item: The item which we outputs.
77
        :param file file: The output file.
78
        """
79
        class_attr = 'level{}'.format(item['level'])
80
        file.write(Html.generate_tag('li', {'class': class_attr}))
81
82
        file.write(Html.generate_tag('a', {'href': '#{}'.format(item['id'])}))
83
84
        number = item['number']
85
        file.write(Html.generate_element('span', {}, str(number)))
86
87
        file.write(' {}'.format(item['arg']))
88
        file.write('</a>')
89
        file.write('</li>')
90
91
92
# ----------------------------------------------------------------------------------------------------------------------
93
NodeStore.register_formatter('toc', 'html', TocHtmlFormatter)
94