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