Completed
Pull Request — master (#22)
by Oleg
01:36
created

generate_chapter()   B

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 24
rs 8.9713
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
from sdoc.helper.Html import Html
10
from sdoc.sdoc2 import node_store
11
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
12
13
14
class HeadingHtmlFormatter(HtmlFormatter):
15
    """
16
    HtmlFormatter for generating HTML code for headings.
17
    """
18
    # ------------------------------------------------------------------------------------------------------------------
19
    def generate(self, node, file):
20
        """
21
        Generates the HTML code for a heading node.
22
23
        :param sdoc.sdoc2.node.HeadingNode.HeadingNode node: The heading node.
24
        :param file file: The output file.
25
        """
26
        self.generate_heading_node(node, file)
27
28
        super().generate(node, file)
29
30
    # ------------------------------------------------------------------------------------------------------------------
31
    def generate_chapter(self, node, file):
32
        """
33
        Generates HTML file structure if the node is chapter otherwise use method for generating heading node.
34
35
        :param sdoc.sdoc2.node.HeadingNode.HeadingNode node: The heading node.
36
        :param file file: The output file.
37
        """
38
        if node.get_command() == 'chapter':
39
            file = open('output_{0}.html'.format(node.argument), 'w')
40
41
            file.write('<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="NL" lang="NL">')
42
            file.write('<head><meta charset="UTF-8"/><title>sdoc</title></head>')
43
            file.write('<body>')
44
45
            self.generate_heading_node(node, file)
46
            super().generate_chapter(node, file)
47
48
            file.write('</body>')
49
            file.write('</html>')
50
            file.close()
51
        else:
52
            if file:
53
                self.generate_heading_node(node, file)
54
                super().generate_chapter(node, file)
55
56
    # ------------------------------------------------------------------------------------------------------------------
57
    def generate_heading_node(self, node, file):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
58
        """
59
        Generates the HTML code for heading node.
60
61
        :param sdoc.sdoc2.node.HeadingNode.HeadingNode node: The heading node.
62
        :param file file: The output file.
63
        """
64
        # Set id attribute to heading node.
65
        attributes = {'id': node.get_option_value('id')}
66
67
        number = node.get_option_value('number')
68
        text_in_tag = '{0} {1!s}'.format('' if not number else number, node.argument)
69
        file.write(Html.generate_element('h{0:d}'.format(node.get_hierarchy_level()), attributes, text_in_tag))
70
71
# ----------------------------------------------------------------------------------------------------------------------
72
node_store.register_formatter('chapter', 'html', HeadingHtmlFormatter)
73
node_store.register_formatter('section', 'html', HeadingHtmlFormatter)
74
node_store.register_formatter('subsection', 'html', HeadingHtmlFormatter)
75
node_store.register_formatter('sub2section', 'html', HeadingHtmlFormatter)
76
node_store.register_formatter('sub3section', 'html', HeadingHtmlFormatter)
77
node_store.register_formatter('sub4section', 'html', HeadingHtmlFormatter)
78
node_store.register_formatter('sub5section', 'html', HeadingHtmlFormatter)
79