Passed
Branch master (0442a2)
by P.R.
02:00
created

HtmlFormat.one_file()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
import sdoc
10
from sdoc.error import SDocError
11
from sdoc.format.Format import Format
12
from sdoc.sdoc2 import node_store
0 ignored issues
show
Unused Code introduced by
Unused node_store imported from sdoc.sdoc2
Loading history...
13
14
15
class HtmlFormat(Format):
16
    """
17
    Class for generating HTML
18
    """
19
20
    # ------------------------------------------------------------------------------------------------------------------
21
    def __init__(self, config):
22
        """
23
        Object constructor.
24
25
        :param configparser.SectionProxy config: The section in the config file for the target_format.
26
        """
27
        super().__init__(config)
28
29
        self._enumerate = True
30
        """
31
        If set chapters, sections, etc. must be numbered.
32
33
        :type: bool
34
        """
35
36
        self._file_per_chapter = False
37
        """
38
        If set, will generate multiple .html files for each chapter.
39
40
        :type: bool
41
        """
42
43
        self._one_file = True
44
        """
45
        If set, will generate one .html file.
46
47
        :type: bool
48
        """
49
50
        self._read_configuration(config)
51
52
    # ------------------------------------------------------------------------------------------------------------------
53
    def _read_configuration(self, config):
54
        """
55
        Reads the configuration for this formatter.
56
57
        :param configparser.SectionProxy config: The section in the config file for the target_format.
58
        """
59
        try:
60
            self._enumerate = config.getboolean('enumerate', fallback=self._enumerate)
61
        except ValueError:
62
            raise SDocError("Option 'enumerate' not set correctly")
63
64
        try:
65
            self._file_per_chapter = config.getboolean('file_per_chapter', fallback=self._file_per_chapter)
66
        except ValueError:
67
            raise SDocError("Option 'file_per_chapter' not set correctly")
68
69
        try:
70
            self._one_file = config.getboolean('one_file', fallback=self._one_file)
71
        except ValueError:
72
            raise SDocError("Option 'one_file' not set correctly")
73
74
    # ------------------------------------------------------------------------------------------------------------------
75
    @property
76
    def enumerate(self):
77
        """
78
        Getter for enumerate attribute.
79
80
        :rtype: bool
81
        """
82
        return self._enumerate
83
84
    # ------------------------------------------------------------------------------------------------------------------
85
    @property
86
    def file_per_chapter(self):
87
        """
88
        Getter for file_per_chapter attribute.
89
90
        :rtype: bool
91
        """
92
        return self._file_per_chapter
93
94
    # ------------------------------------------------------------------------------------------------------------------
95
    @property
96
    def one_file(self):
97
        """
98
        Getter for one_file attribute.
99
100
        :rtype: bool
101
        """
102
        return self._one_file
103
104
    # ------------------------------------------------------------------------------------------------------------------
105
    def generate(self):
106
        """
107
        Starts generating HTML file.
108
        """
109
        # Activate numbering nodes.
110
        if self.enumerate:
111
            sdoc.sdoc2.node_store.number_numerable()
112
113
        # Generate labels.
114
        sdoc.sdoc2.node_store.parse_labels()
115
116
        # Generate whole HTML output file.
117
        if self.one_file:
118
            general_file = open('output.html', 'w')
119
            formatter = sdoc.sdoc2.node_store.create_formatter('document')
120
            formatter.generate(sdoc.sdoc2.node_store.nodes[1], general_file)
121
122
        # Generate in mode 'output file on each chapter'.
123
        if self.file_per_chapter:
124
            formatter = sdoc.sdoc2.node_store.create_formatter('document')
125
            formatter.generate_chapter(sdoc.sdoc2.node_store.nodes[1], None)
126
127
# ----------------------------------------------------------------------------------------------------------------------
128