-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_nodejs_example_strings.py
More file actions
45 lines (39 loc) · 1.22 KB
/
Copy pathfix_nodejs_example_strings.py
File metadata and controls
45 lines (39 loc) · 1.22 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
from pathlib import Path
import re
path = Path('src/data/nodejs/interview.js')
text = path.read_text(encoding='utf-8')
lines = text.splitlines()
out_lines = []
in_example = False
example_indent = ''
example_lines = []
for line in lines:
if not in_example:
match = re.match(r'^(\s*example:\s*)`(.*)$', line)
if match:
in_example = True
example_indent = match.group(1)
rest = match.group(2)
example_lines = [rest]
continue
out_lines.append(line)
continue
# inside example block
end_match = re.match(r'^(.*)`\s*,\s*$', line)
if end_match:
example_lines.append(end_match.group(1))
escaped = []
for ex_line in example_lines:
escaped_line = ex_line.replace('\\', '\\\\').replace('"', '\\"')
escaped.append(escaped_line)
joined = '\\n'.join(escaped)
out_lines.append(f'{example_indent}"{joined}",')
in_example = False
example_indent = ''
example_lines = []
continue
example_lines.append(line)
if in_example:
raise SystemExit('Unterminated example block')
path.write_text('\n'.join(out_lines) + '\n', encoding='utf-8')
print('Updated', path)