forked from d2l-ai/d2l-book
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrst.py
273 lines (256 loc) · 9.66 KB
/
rst.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""utilities to handle rst files"""
import re
import logging
from typing import Dict
import nbconvert
import nbformat
from nbformat import notebooknode
from d2lbook import notebook
from d2lbook import common
from d2lbook import markdown
def convert_notebook(nb: notebooknode.NotebookNode, resources: Dict[str, str]):
nb = _process_nb(nb)
writer = nbconvert.RSTExporter()
body, resources = writer.from_notebook_node(nb, resources)
body = _process_rst(body)
return body, resources
def _process_nb(nb):
# add empty lines before and after a mark/fence
new_cells = []
for cell in nb.cells:
if cell.cell_type == 'markdown':
md_cells = markdown.split_markdown(cell.source)
for i, md_cell in enumerate(md_cells):
if i < len(md_cells) - 1 and md_cells[i+1]['type'] == 'code':
md_cells[i]['source'] += '\n'
if md_cell['type'] == 'markdown':
lines = md_cells[i]['source'].split('\n')
for j, line in enumerate(lines):
m = common.md_mark_pattern.match(line)
if (m is not None
and m[1] not in ('ref', 'numref', 'eqref')
and m.end() == len(line)):
lines[j] = '\n'+line+'\n'
md_cells[i]['source'] = '\n'.join(lines)
new_cells.append(nbformat.v4.new_markdown_cell(
markdown.join_markdown_cells(md_cells)))
else:
new_cells.append(cell)
# hide/show
hide_all = False
for cell in new_cells:
if cell.cell_type == 'code':
src = cell.source.lower()
if '#@hide_all' in src:
hide_all = True
if hide_all or '# hide outputs' in src or ('#@hide' in src and '#@hide_code' not in src) or '#@hide_output' in src:
cell.outputs = []
if hide_all or '# hide code' in src or ('#@hide' in src and '#@hide_output' not in src) or '#@hide_code' in src:
cell.source = ''
return notebook.create_new_notebook(nb, new_cells)
def _process_rst(body):
def delete_lines(lines, deletes):
return [line for i, line in enumerate(lines) if i not in deletes]
def indented(line):
return line.startswith(' ')
def blank(line):
return len(line.strip()) == 0
def look_behind(i, cond, lines):
indices = []
while i < len(lines) and cond(lines[i]):
indices.append(i)
i = i + 1
return indices
lines = body.split('\n')
# deletes: indices of lines to be deleted
i, deletes = 0, []
while i < len(lines):
line = lines[i]
# '.. code:: toc' -> '.. toctree::', then remove consecutive empty lines
# after the current line
if line.startswith('.. code:: toc'):
# convert into rst's toc block
lines[i] = '.. toctree::'
blanks = look_behind(i+1, blank, lines)
deletes.extend(blanks)
i += len(blanks)
# .. code:: eval_rst
#
#
# .. only:: html
#
# References
# ==========
# ->
#
#
#
# .. only:: html
#
# References
# ==========
elif line.startswith('.. code:: eval_rst'):
# make it a rst block
deletes.append(i)
j = i + 1
while j < len(lines):
line_j = lines[j]
if indented(line_j):
lines[j] = line_j[3:]
if lines[j].strip().startswith('.. '):
lines[j] = '\n'+lines[j].strip()
elif not blank(line_j):
break
j += 1
i = j
elif line.startswith('.. parsed-literal::'):
# add a output class so we can add customized css
lines[i] += '\n :class: output'
i += 1
# .. figure:: ../img/jupyter.png
# :alt: Output after running Jupyter Notebook. The last row is the URL
# for port 8888.
#
# Output after running Jupyter Notebook. The last row is the URL for
# port 8888.
#
# :width:``700px``
#
# :label:``fig_jupyter``
#->
# .. _fig_jupyter:
#
# .. figure:: ../img/jupyter.png
# :width: 700px
#
# Output after running Jupyter Notebook. The last row is the URL for
# port 8888.
elif indented(line) and ':alt:' in line:
# Image caption, remove :alt: block, it cause trouble for long captions
caps = look_behind(i, lambda l: indented(l) and not blank(l), lines)
deletes.extend(caps)
i += len(caps)
# .. table:: Dataset versus computer memory and computational power
# +-...
# |
# +-...
#
# :label:``tab_intro_decade``
# ->
# .. _tab_intro_decade:
#
# .. table:: Dataset versus computer memory and computational power
#
# +-...
# |
# +-...
#
elif line.startswith('.. table::'):
# Add indent to table caption for long captions
caps = look_behind(i+1, lambda l: not indented(l) and not blank(l),
lines)
for j in caps:
lines[j] = ' ' + lines[j]
i += len(caps) + 1
else:
i += 1
# change :label:my_label: into rst format
lines = delete_lines(lines, deletes)
deletes = []
for i, line in enumerate(lines):
pos, new_line = 0, ''
while True:
match = common.rst_mark_pattern.search(line, pos)
if match is None:
new_line += line[pos:]
break
# e.g., case :math:`x`, :numref:`y`, match[0] = ':math:'
elif match[2] is None:
end = match.end()
new_line += line[pos:end]
pos = end
continue
start, end = match.start(), match.end()
# e.g., origin=':label:``fig_jupyter``', key='label', value='fig_jupyter'
origin, key, value = match[0], match[1], match[2]
assert value.startswith('``') and value.endswith('``'), value
value = value[2:-2]
new_line += line[pos:start]
pos = end
# assert key in ['label', 'eqlabel', 'ref', 'numref', 'eqref', 'width', 'height'], 'unknown key: ' + key
if key == 'label':
new_line += '.. _' + value + ':'
elif key in ['ref', 'numref', 'cite']:
new_line += ':'+key+':`'+value+'`'
elif key == 'eqref':
new_line += ':eq:`'+value+'`'
elif key in ['class', 'func', 'mod']:
new_line += ':py:'+key+':`'+value+'`'
# .. math:: f
#
# :eqlabel:``gd-taylor``
# ->
# .. math:: f
# :label: gd-taylor
elif key == 'eqlabel':
new_line += ' :label: '+value
if blank(lines[i-1]):
deletes.append(i-1)
elif key in ['width', 'height']:
new_line += ' :'+key+': '+value
elif key == 'bibliography':
# a hard coded plain bibtex style...
new_line += ('.. bibliography:: ' + value +
'\n :style: apa')
# '\n :style: apa\n :all:') MM 20200104 removed ':all:' so only the cited references get printed
else:
logging.fatal(f'unknown key {key}')
lines[i] = new_line
lines = delete_lines(lines, deletes)
def move(i, j): # move line i to line j
lines.insert(j, lines[i])
if i > j:
del lines[i+1]
else:
del lines[i]
# move :width: or :width: just below .. figure::
for i, line in enumerate(lines):
if line.startswith('.. figure::'):
for j in range(i+1, len(lines)):
line_j = lines[j]
if not indented(line_j) and not blank(line_j):
break
if line_j.startswith(' :width:') or line_j.startswith(' :height:'):
move(j, i+1)
# move .. _label: before a image, a section, or a table
lines.insert(0, '')
i = 0
while i < len(lines):
line = lines[i]
if line.startswith('.. _'):
for j in range(i-1, -1, -1):
line_j = lines[j]
if (line_j.startswith('.. table:')
or line_j.startswith('.. figure:')):
move(i, j-1)
lines.insert(j-1, '')
i += 1 # Due to insertion of a blank line
break
if (len(set(line_j)) == 1
and line_j[0] in ['=','~','_', '-']):
k = max(j-2, 0)
move(i, k)
lines.insert(k, '')
i += 1 # Due to insertion of a blank line
break
i += 1
# change .. image:: to .. figure:: to they will be center aligned
for i, line in enumerate(lines):
if '.. image::' in line:
lines[i] = line.replace('.. image::', '.. figure::')
# sometimes the code results contains vt100 codes, widely used for
# coloring, while it is not supported by latex.
for i, l in enumerate(lines):
lines[i] = re.sub(r'\x1b\[[\d;]*K', '',
re.sub(r'\x1b\[[\d;]*m', '', l))
return '\n'.join(lines)