forked from nschloe/pytest-codeblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exdown.py
executable file
·161 lines (139 loc) · 5.05 KB
/
exdown.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
#!/usr/bin/python3
import argparse
import subprocess
import tempfile
import sys
from typing import List, Optional, Tuple
PARSER = argparse.ArgumentParser()
PARSER.add_argument("FILE", help="the file to parse", type=str)
PARSER.add_argument(
"-f",
"--focus",
help="the only extension to consider. I.e. if interested in ```ocaml ...``` blocks, pass -f ocaml",
type=str,
)
PARSER.add_argument(
"-x",
"--exec",
help="command to execute on each snippet (split on spaces)",
type=str,
)
ARGS = PARSER.parse_args()
def extract(f, *args, **kwargs):
with open(f, "r") as handle:
return from_buffer(handle, *args, **kwargs)
def parse_skip(line) -> Optional[List[int]]:
"""Returns None if the line is not a exdown-skip statement
Otherwise returns the lines to skip (numbering starts at 1).
The empty list indicates that the whole block must be skipped."""
line = line.lstrip()
searched = "exdown-skip"
idx = line.find(searched)
if idx < 0:
return None
suffix = line[idx + len(searched) :]
skipped_lines = []
for nb_line in suffix.lstrip().split(" "): # Parse to the right of exdown-skip
try:
nb_line = int(nb_line)
if nb_line < 1:
raise Exception(
f"Line numbers in {searched} start at 1, but received {nb_line}"
)
skipped_lines.append(nb_line)
except ValueError as _:
break
return skipped_lines
# Inline tests, to KISS
assert parse_skip("") is None
assert parse_skip("foobar") is None
assert parse_skip("<!-- exdown-skip -->") == []
assert parse_skip("<!-- exdown-skip") == []
assert parse_skip("whatever exdown-skip") == []
assert (
parse_skip("[//]: #exdown-skip") == []
) # comments in https://github.com/gnab/remark/wiki/Markdown#empty-link
assert parse_skip("<!-- exdown-skip 1-->") == []
assert parse_skip("// exdown-skip 1-->") == []
assert parse_skip("<!-- exdown-skip 3 -->") == [3]
assert parse_skip("// exdown-skip 3 -->") == [3]
assert parse_skip("<!-- exdown-skip 1 2 2048 -->") == [1, 2, 2048]
def from_buffer(
f, max_num_lines=10000, focus=None
) -> List[Tuple[str, int, Optional[str]]]:
"""returns the list of snippet. Each snippet comes
with its starting line number and its extension (if any)"""
out = []
previous_line = None
k = 1
while True:
line = f.readline()
k += 1
if not line:
# EOF
break
if line.lstrip()[:3] == "```":
syntax = line.strip()[3:]
num_leading_spaces = len(line) - len(line.lstrip())
lineno = k - 1
# read the block
code_block = []
while True:
line = f.readline()
k += 1
if not line:
raise RuntimeError("Hit end-of-file prematurely. Syntax error?")
if k > max_num_lines:
raise RuntimeError(
f"File too large (> {max_num_lines} lines). Set max_num_lines."
)
# check if end of block
if line.lstrip()[:3] == "```":
break
# Cut (at most) num_leading_spaces leading spaces
nls = min(num_leading_spaces, len(line) - len(line.lstrip()))
line = line[nls:]
code_block.append(line)
if focus and focus != syntax.strip():
continue
skip = parse_skip(previous_line) if previous_line else None
if skip is None:
pass # Do not skip
elif skip == []:
continue # Skip everything
else:
# Skip only selected lines. Remove lines starting by
# the ones with the highest number (sorted(..).reverse()).
# -1 because lines in exdown-skip stanzas start at 1
skip = sorted(skip)
skip.reverse()
for skipped_line in skip:
code_block.pop(skipped_line - 1)
out.append(("".join(code_block), lineno, syntax))
previous_line = line
return out
def _exec(snippet: str, lineno: int, ext: Optional[str]):
ext = ("." + ext) if ext else None
with tempfile.NamedTemporaryFile(prefix="exdown_", suffix=ext) as tmp_file:
print(snippet)
tmp_file.write(snippet.encode("utf-8"))
tmp_file.flush()
cmd = ARGS.exec.split(" ") + [tmp_file.name]
# cmd_str = " ".join(cmd)
# print(f"> {cmd_str}")
result = subprocess.run(cmd, check=False, stderr=sys.stderr, stdout=sys.stdout)
rc = result.returncode
if rc != 0:
print(f"{ARGS.FILE}:{lineno} exdown snippet ERROR")
sys.exit(rc)
def main():
for f in [ARGS.FILE]:
out = extract(f, focus=ARGS.focus)
for p in out:
code_str = p[0]
if ARGS.exec:
_exec(code_str, p[1], p[2])
else: # print snippet
print(code_str)
if __name__ == "__main__":
main()