1
|
1 |
|
from typing import Any |
2
|
|
|
|
3
|
1 |
|
from sdoc.helper.Html import Html |
4
|
1 |
|
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter |
5
|
1 |
|
from sdoc.sdoc2.node.FigureNode import FigureNode |
6
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
View Code Duplication |
class FigureHtmlFormatter(HtmlFormatter): |
|
|
|
|
10
|
|
|
""" |
11
|
|
|
HtmlFormatter for generating HTML code for figures. |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
15
|
1 |
|
def generate(self, node: FigureNode, file: Any) -> None: |
16
|
|
|
""" |
17
|
|
|
Generates the HTML code for a figure node. |
18
|
|
|
|
19
|
|
|
:param FigureNode node: The figure node. |
20
|
|
|
:param any file: The output file. |
21
|
|
|
""" |
22
|
|
|
self.write_into_file(node, file) |
23
|
|
|
|
24
|
|
|
HtmlFormatter.generate(self, node, file) |
25
|
|
|
|
26
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
27
|
1 |
|
@staticmethod |
28
|
1 |
|
def _write_caption(node: FigureNode, file: Any) -> None: |
29
|
|
|
""" |
30
|
|
|
Generates the caption for the table in HTML representation. |
31
|
|
|
|
32
|
|
|
:param FigureNode node: The figure node. |
33
|
|
|
:param any file: The output file. |
34
|
|
|
""" |
35
|
|
|
if node.caption: |
36
|
|
|
figure_number = node.get_option_value('number') |
37
|
|
|
|
38
|
|
|
if figure_number: |
39
|
|
|
inner_text = 'Figuur {}: {}'.format(figure_number, node.caption) # TODO Internationalization |
40
|
|
|
else: |
41
|
|
|
inner_text = node.caption |
42
|
|
|
|
43
|
|
|
file.write(Html.generate_element('figcaption', {}, inner_text)) |
44
|
|
|
|
45
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
46
|
1 |
|
@staticmethod |
47
|
1 |
|
def write_into_file(node: FigureNode, file: Any) -> None: |
48
|
|
|
""" |
49
|
|
|
Writes data into opened HTML file. |
50
|
|
|
|
51
|
|
|
:param FigureNode node: The figure node. |
52
|
|
|
:param any file: The output file. |
53
|
|
|
""" |
54
|
|
|
# Creating dicts with attributes for each type of element. |
55
|
|
|
figure_attributes = {'id': node.get_option_value('id')} |
56
|
|
|
|
57
|
|
|
img_attributes = {'src': node.get_option_value('src'), |
58
|
|
|
'width': node.get_option_value('width'), |
59
|
|
|
'height': node.get_option_value('height'), |
60
|
|
|
'alt': node.caption} |
61
|
|
|
|
62
|
|
|
# Creating elements. |
63
|
|
|
file.write(Html.generate_tag('figure', figure_attributes)) |
64
|
|
|
|
65
|
|
|
file.write(Html.generate_void_element('img', img_attributes)) |
66
|
|
|
|
67
|
|
|
FigureHtmlFormatter._write_caption(node, file) |
68
|
|
|
|
69
|
|
|
file.write('</figure>') |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
73
|
|
|
NodeStore.register_formatter('figure', 'html', FigureHtmlFormatter) |
74
|
|
|
|