Test Failed
Push — master ( 728016...1ac98c )
by P.R.
01:34
created

HtmlFormat._read_configuration()   B

Complexity

Conditions 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 0
cts 18
cp 0
rs 8.0894
cc 5
crap 30
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
import os
10
11
from sdoc import sdoc2
12
from sdoc.error import SDocError
13
from sdoc.format.Format import Format
14
15
16
class HtmlFormat(Format):
17
    """
18
    Class for generating HTML
19
    """
20
21
    # ------------------------------------------------------------------------------------------------------------------
22
    def __init__(self, io, target_format, config):
23
        """
24
        Object constructor.
25
26
        :param cleo.styles.output_style.OutputStyle io: The IO object.
27
        :param str target_format: The name of the format (in the config file).
28
        :param configparser.ConfigParser config: The section in the config file for the target_format.
29
        """
30
        Format.__init__(self, io, config)
31
32
        self._enumerate = True
33
        """
34
        If set chapters, sections, etc. must be numbered.
35
36
        :type: bool
37
        """
38
39
        self._file_per_chapter = False
40
        """
41
        If set, will generate multiple .html files for each chapter.
42
43
        :type: bool
44
        """
45
46
        self._one_file = True
47
        """
48
        If set, will generate one .html file.
49
50
        :type: bool
51
        """
52
53
        self._target_dir = '.'
54
        """
55
        The directory where the document in the target format must be created.
56
57
        :type: str
58
        """
59
60
        self._read_configuration(target_format, config)
61
62
    # ------------------------------------------------------------------------------------------------------------------
63
    def _read_configuration(self, target_format, config):
64
        """
65
        Reads the configuration for this formatter.
66
67
        :param str target_format: The name of the format (in the config file).
68
        :param configparser.ConfigParser config: The section in the config file for the target_format.
69
        """
70
        section = 'format_' + target_format
71
72
        try:
73
            self._enumerate = config.getboolean(section, 'enumerate', fallback=self._enumerate)
74
        except ValueError:
75
            raise SDocError("Option 'enumerate' not set correctly")
76
77
        try:
78
            self._file_per_chapter = config.getboolean(section, 'file_per_chapter', fallback=self._file_per_chapter)
79
        except ValueError:
80
            raise SDocError("Option 'file_per_chapter' not set correctly")
81
82
        try:
83
            self._one_file = config.getboolean(section, 'one_file', fallback=self._one_file)
84
        except ValueError:
85
            raise SDocError("Option 'one_file' not set correctly")
86
87
        try:
88
            self._target_dir = config.get('sdoc', 'target_dir', fallback=self._target_dir)
89
        except ValueError:
90
            raise SDocError("Option 'target_dir' not set correctly")
91
92
    # ------------------------------------------------------------------------------------------------------------------
93
    @property
94
    def enumerate(self):
95
        """
96
        Getter for enumerate attribute.
97
98
        :rtype: bool
99
        """
100
        return self._enumerate
101
102
    # ------------------------------------------------------------------------------------------------------------------
103
    def generate(self):
104
        """
105
        Generating the document in HTML and returns the number of errors encountered.
106
107
        :rtype: int
108
        """
109
        # Activate numbering nodes.
110
        if self.enumerate:
111
            sdoc2.node_store.number_numerable()
112
113
        # Generate labels.
114
        sdoc2.node_store.parse_labels()
115
116
        # Generate table of contents.
117
        sdoc2.node_store.generate_toc()
118
119
        # Generate whole HTML output file.
120
        if self._one_file:
121
            file_name = os.path.join(self._target_dir, 'output.html')
122
            self._io.writeln('Writing <fso>{0!s}</fso>'.format(file_name))
123
            with open(file_name, 'wt', encoding='utf8') as general_file:
124
                formatter = sdoc2.node_store.create_formatter(self._io, 'document')
125
                formatter.generate(sdoc2.node_store.nodes[1], general_file)
126
                self._errors += formatter.errors
127
128
        # Generate in mode 'output file on each chapter'.
129
        if self._file_per_chapter:
130
            raise RuntimeError()
131
132
        return self._errors
133
134
# ----------------------------------------------------------------------------------------------------------------------
135