Completed
Push — master ( b50573...8e7f82 )
by P.R.
01:58
created

PartHtmlFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 15.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
dl 0
loc 44
ccs 3
cts 19
cp 0.1579
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate_part_node() 0 14 2
A generate() 0 22 3
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
import os
10
11 1
from sdoc.helper.Html import Html
12 1
from sdoc.sdoc2 import node_store
13 1
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
14
15
16 1
class PartHtmlFormatter(HtmlFormatter):
17
    """
18
    HtmlFormatter for generating HTML code for parts.
19
    """
20
21
    # ------------------------------------------------------------------------------------------------------------------
22 1
    def generate(self, node, file):
23
        """
24
        Generates the HTML code for a part node.
25
26
        :param sdoc.sdoc2.node.HeadingNode.HeadingNode node: The heading node.
27
        :param file file: The output file.
28
        """
29
        if os.path.exists('output.html'):
30
            os.remove('output.html')
31
32
        file_name = 'output_{0}.html'.format(node.argument)
33
        self._io.writeln('Writing <fso>{0!s}</fso>'.format(file_name))
34
        with open(file_name, 'w') as file:
35
            file.write('<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="NL" lang="NL">')
36
            file.write('<head><meta charset="UTF-8"/><title>sdoc</title></head>')
37
            file.write('<body>')
38
39
            self.generate_part_node(node, file)
40
            super().generate(node, file)
41
42
            file.write('</body>')
43
            file.write('</html>')
44
45
    # ------------------------------------------------------------------------------------------------------------------
46 1
    @staticmethod
47
    def generate_part_node(node, file):
48
        """
49
        Generates the HTML code for part node.
50
51
        :param sdoc.sdoc2.node.HeadingNode.HeadingNode node: The heading node.
52
        :param file file: The output file.
53
        """
54
        # Set id attribute to heading node.
55
        attributes = {'id': node.get_option_value('id'), 'class': 'part'}
56
57
        number = node.get_option_value('number')
58
        text_in_tag = '{0} {1!s}'.format('' if not number else number, node.argument)
59
        file.write(Html.generate_element('div', attributes, text_in_tag))
60
61
# ----------------------------------------------------------------------------------------------------------------------
62
node_store.register_formatter('part', 'html', PartHtmlFormatter)
63