-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
writers.py
156 lines (126 loc) · 5.04 KB
/
writers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Custom write module."""
from docutils.nodes import ( # type: ignore
Element,
SkipNode,
comment,
literal_block,
section,
)
from sphinx.writers.html5 import HTML5Translator
from .nodes import revealjs_break
def has_child_sections(node: Element, name: str):
"""Search has specified section in children."""
nodes = set([n.tagname for n in node.children])
return name in nodes
def find_child_section(node: Element, name: str):
"""Search and return first specified section in children."""
for n in node.children:
if n.tagname == name:
return n
return None
class RevealjsSlideTranslator(HTML5Translator):
"""Translate Reveal.js HTML class."""
permalink_text = False
def __init__(self, builder, *args, **kwds): # noqa: D107
super().__init__(builder, *args, **kwds)
self.builder.add_permalinks = False
self._nest_step = 0
def visit_section(self, node: section):
"""Begin ``section`` node.
- Find first ``revealjs_section`` node and build attributes string.
- When enter next section, nest level.
"""
self.section_level += 1
if self.section_level >= 4:
return
meta = find_child_section(node, "revealjs_section")
if meta is not None:
attrs = meta.attributes_str()
else:
attrs = ""
if node.attributes.get("ids") and self.config.revealjs_use_section_ids:
attrs += ' id="{}"'.format(node.attributes["ids"][-1])
if self._nest_step > 0:
self.body.append("</section>\n" * self._nest_step)
self._nest_step = 0
if self.section_level == 1:
self._nest_step = 2
self.builder.revealjs_slide = find_child_section(node, "revealjs_slide")
elif has_child_sections(node, "section"):
self._nest_step = 1
if self._nest_step > 0:
v_meta = find_child_section(node, "revealjs_vertical")
v_attrs = v_meta.attributes_str() if v_meta is not None else ""
self.body.append(f"<section {v_attrs}>\n")
self.body.append(f"<section {attrs}>\n")
def depart_section(self, node: section):
"""End ``section``.
Dedent section level
"""
self.section_level -= 1
if self.section_level >= 3:
return
self.body.append("</section>\n")
def visit_comment(self, node: comment):
"""Begin ``comment`` node.
comment node render as speaker note.
"""
if self.builder.app.config.revealjs_notes_from_comments:
self.body.append('<aside class="notes">\n')
else:
raise SkipNode
def depart_comment(self, node: comment):
"""End ``comment`` node.
Close speaker note.
"""
if self.builder.app.config.revealjs_notes_from_comments:
self.body.append("</aside>\n")
def visit_literal_block(self, node: literal_block):
"""Begin ``literal_block`` .
Override base method, and open simply ``pre`` and ``code`` tags.
"""
lang = node["language"]
# add section id as data-id if it is exists
if "data-id" in node:
self.body.append(f"<pre data-id=\"{node['data-id']}\">")
elif isinstance(node.parent, section) and len(node.parent["ids"]):
self.body.append(f"<pre data-id=\"{node.parent['ids'][0]}\">")
else:
self.body.append("<pre>")
self.body.append(f'<code data-trim data-noescape class="{lang}"')
# use the emphasize-lines directive to create line for line animations
if "data-line-numbers" in node:
self.body.append(f" data-line-numbers=\"{node['data-line-numbers']}\"")
else:
# show line numbers
if node["linenos"]:
self.body.append(" data-line-numbers")
if "data-ln-start-from" in node:
self.body.append(f" data-ln-start-from=\"{node['data-ln-start-from']}\"")
if "data-line-numbers" not in node:
self.body.append(" data-line-numbers")
self.body.append(">")
def depart_literal_block(self, node: literal_block):
"""End ``literal_block``.
Override base method, and close begun tags.
"""
self.body.append("</code></pre>\n")
def not_write(self, node):
"""visit/depart function for declare "no write"."""
pass
def visit_revealjs_break(self, node: revealjs_break):
"""Close current section."""
self.body.append("</section>\n")
def depart_revealjs_break(self, node: revealjs_break):
"""Open as next section.
If node does not have attribute 'notitle',
render title from current original section.
"""
attrs = node.attributes_str()
self.body.append(f"<section {attrs}>\n")
if "notitle" not in node.attributes:
title = find_child_section(node.parent, "title")
self.body.append(f"<h{self.section_level}>")
self.body.append(title.children[0])
self.body.append(f"</h{self.section_level}>")
self.body.append("\n")