-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathconverter.py
More file actions
318 lines (268 loc) · 10.6 KB
/
converter.py
File metadata and controls
318 lines (268 loc) · 10.6 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import json
from pathlib import Path
from typing import Callable, Iterable, Optional
import attr
import nbformat as nbf
import yaml
from myst_parser.main import MdParserConfig
from sphinx.environment import BuildEnvironment
from sphinx.util import import_object, logging
NOTEBOOK_VERSION = 4
CODE_DIRECTIVE = "{code-cell}"
RAW_DIRECTIVE = "{raw-cell}"
LOGGER = logging.getLogger(__name__)
@attr.s
class NbConverter:
func: Callable[[str], nbf.NotebookNode] = attr.ib()
config: MdParserConfig = attr.ib()
def get_nb_converter(
path: str,
env: BuildEnvironment,
source_iter: Optional[Iterable[str]] = None,
) -> Optional[NbConverter]:
"""Get function, to convert a source string to a Notebook."""
# Standard notebooks take priority
if path.endswith(".ipynb"):
return NbConverter(
lambda text: nbf.reads(text, as_version=NOTEBOOK_VERSION), env.myst_config
)
# we check suffixes ordered by longest first, to ensure we get the "closest" match
for source_suffix in sorted(
env.config.nb_custom_formats.keys(), key=len, reverse=True
):
if path.endswith(source_suffix):
(
converter,
converter_kwargs,
commonmark_only,
) = env.config.nb_custom_formats[source_suffix]
converter = import_object(converter)
a = NbConverter(
lambda text: converter(text, **(converter_kwargs or {})),
env.myst_config
if commonmark_only is None
else attr.evolve(env.myst_config, commonmark_only=commonmark_only),
)
return a
# If there is no source text then we assume a MyST Notebook
if source_iter is None:
# Check if docname exists
return NbConverter(
lambda text: myst_to_notebook(
text,
config=env.myst_config,
add_source_map=True,
path=path,
),
env.myst_config,
)
# Given the source lines, we check it can be recognised as a MyST Notebook
if is_myst_notebook(source_iter):
# Check if docname exists
return NbConverter(
lambda text: myst_to_notebook(
text,
config=env.myst_config,
add_source_map=True,
path=path,
),
env.myst_config,
)
# Otherwise, we return None,
# to imply that it should be parsed as as standard Markdown file
return None
def is_myst_notebook(line_iter: Iterable[str]) -> bool:
"""Is the text file a MyST based notebook representation?"""
# we need to distinguish between markdown representing notebooks
# and standard notebooks.
# Therefore, for now we require that, at a mimimum we can find some top matter
# containing the jupytext format_name
yaml_lines = []
for i, line in enumerate(line_iter):
if i == 0 and not line.startswith("---"):
return False
if i != 0 and (line.startswith("---") or line.startswith("...")):
break
yaml_lines.append(line.rstrip() + "\n")
try:
front_matter = yaml.safe_load("".join(yaml_lines))
except Exception:
return False
if front_matter is None: # this can occur for empty files
return False
if (
front_matter.get("jupytext", {})
.get("text_representation", {})
.get("format_name", None)
!= "myst"
):
return False
if "name" not in front_matter.get("kernelspec", {}):
raise IOError(
"A myst notebook text-representation requires " "kernelspec/name metadata"
)
if "display_name" not in front_matter.get("kernelspec", {}):
raise IOError(
"A myst notebook text-representation requires "
"kernelspec/display_name metadata"
)
return True
class MystMetadataParsingError(Exception):
"""Error when parsing metadata from myst formatted text"""
class LoadFileParsingError(Exception):
"""Error when parsing files for code-blocks/code-cells"""
def strip_blank_lines(text):
text = text.rstrip()
while text and text.startswith("\n"):
text = text[1:]
return text
class MockDirective:
option_spec = {"options": True}
required_arguments = 0
optional_arguments = 1
has_content = True
def read_fenced_cell(token, cell_index, cell_type):
from myst_parser.parse_directives import DirectiveParsingError, parse_directive_text
try:
_, options, body_lines = parse_directive_text(
directive_class=MockDirective,
first_line="",
content=token.content,
validate_options=False,
)
except DirectiveParsingError as err:
raise MystMetadataParsingError(
"{0} cell {1} at line {2} could not be read: {3}".format(
cell_type, cell_index, token.map[0] + 1, err
)
)
return options, body_lines
def read_cell_metadata(token, cell_index):
metadata = {}
if token.content:
try:
metadata = json.loads(token.content.strip())
except Exception as err:
raise MystMetadataParsingError(
"Markdown cell {0} at line {1} could not be read: {2}".format(
cell_index, token.map[0] + 1, err
)
)
if not isinstance(metadata, dict):
raise MystMetadataParsingError(
"Markdown cell {0} at line {1} is not a dict".format(
cell_index, token.map[0] + 1
)
)
return metadata
def load_code_from_file(nb_path, file_name, token, body_lines, encoding="utf-8"):
"""load source code from a file."""
if nb_path is None:
raise LoadFileParsingError("path to notebook not supplied for :load:")
file_path = Path(nb_path).parent.joinpath(file_name).resolve()
if len(body_lines):
line = token.map[0] if token.map else 0
msg = (
f"{nb_path}:{line} content of code-cell is being overwritten by "
f":load: {file_name}"
)
LOGGER.warning(msg)
try:
body_lines = file_path.read_text(encoding=encoding).split("\n")
except Exception:
raise LoadFileParsingError("Can't read file from :load: {}".format(file_path))
return body_lines
def myst_to_notebook(
text,
config: MdParserConfig,
code_directive=CODE_DIRECTIVE,
raw_directive=RAW_DIRECTIVE,
add_source_map=False,
path: Optional[str] = None,
):
"""Convert text written in the myst format to a notebook.
:param text: the file text
:param code_directive: the name of the directive to search for containing code cells
:param raw_directive: the name of the directive to search for containing raw cells
:param add_source_map: add a `source_map` key to the notebook metadata,
which is a list of the starting source line number for each cell.
:param path: path to notebook (required for :load:)
:raises MystMetadataParsingError if the metadata block is not valid JSON/YAML
NOTE: we assume here that all of these directives are at the top-level,
i.e. not nested in other directives.
"""
# TODO warn about nested code-cells
from myst_parser.main import default_parser
# parse markdown file up to the block level (i.e. don't worry about inline text)
inline_config = attr.evolve(
config, renderer="html", disable_syntax=(config.disable_syntax + ["inline"])
)
parser = default_parser(inline_config)
tokens = parser.parse(text + "\n")
lines = text.splitlines()
md_start_line = 0
# get the document metadata
metadata_nb = {}
if tokens[0].type == "front_matter":
metadata = tokens.pop(0)
md_start_line = metadata.map[1]
try:
metadata_nb = yaml.safe_load(metadata.content)
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as error:
raise MystMetadataParsingError("Notebook metadata: {}".format(error))
# create an empty notebook
nbf_version = nbf.v4
kwargs = {"metadata": nbf.from_dict(metadata_nb)}
notebook = nbf_version.new_notebook(**kwargs)
source_map = [] # this is a list of the starting line number for each cell
def _flush_markdown(start_line, token, md_metadata):
"""When we find a cell we check if there is preceding text.o"""
endline = token.map[0] if token else len(lines)
md_source = strip_blank_lines("\n".join(lines[start_line:endline]))
meta = nbf.from_dict(md_metadata)
if md_source:
source_map.append(start_line)
notebook.cells.append(
nbf_version.new_markdown_cell(source=md_source, metadata=meta)
)
# iterate through the tokens to identify notebook cells
nesting_level = 0
md_metadata = {}
for token in tokens:
nesting_level += token.nesting
if nesting_level != 0:
# we ignore fenced block that are nested, e.g. as part of lists, etc
continue
if token.type == "fence" and token.info.startswith(code_directive):
_flush_markdown(md_start_line, token, md_metadata)
options, body_lines = read_fenced_cell(token, len(notebook.cells), "Code")
# Parse :load: or load: tags and populate body with contents of file
if "load" in options:
body_lines = load_code_from_file(
path, options["load"], token, body_lines
)
meta = nbf.from_dict(options)
source_map.append(token.map[0] + 1)
notebook.cells.append(
nbf_version.new_code_cell(source="\n".join(body_lines), metadata=meta)
)
md_metadata = {}
md_start_line = token.map[1]
elif token.type == "fence" and token.info.startswith(raw_directive):
_flush_markdown(md_start_line, token, md_metadata)
options, body_lines = read_fenced_cell(token, len(notebook.cells), "Raw")
meta = nbf.from_dict(options)
source_map.append(token.map[0] + 1)
notebook.cells.append(
nbf_version.new_raw_cell(source="\n".join(body_lines), metadata=meta)
)
md_metadata = {}
md_start_line = token.map[1]
elif token.type == "myst_block_break":
_flush_markdown(md_start_line, token, md_metadata)
md_metadata = read_cell_metadata(token, len(notebook.cells))
md_start_line = token.map[1]
_flush_markdown(md_start_line, None, md_metadata)
if add_source_map:
notebook.metadata["source_map"] = source_map
return notebook