-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathdoctestify.py
More file actions
36 lines (30 loc) · 1.04 KB
/
doctestify.py
File metadata and controls
36 lines (30 loc) · 1.04 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
from pathlib import Path
VERSION = '0.1.0'
def doctestify(test):
lines = test.splitlines()
markdown_lines = []
in_code_block = False
for line in lines:
if line.startswith("\\") or "\\gset" in line or "-- pragma:hide" in line:
continue
if line.startswith("--") and not line.startswith("---"):
if in_code_block:
markdown_lines.append("```")
in_code_block = False
markdown_lines.append(line[3:])
else:
if not in_code_block:
markdown_lines.append("``` postgres-console")
in_code_block = True
line = line.replace("\u21B5", " ")
markdown_lines.append(line)
if in_code_block:
markdown_lines.append("```")
return "\n".join(markdown_lines)
if __name__ == '__main__':
import sys
test = Path(sys.argv[1])
infile = open(test, 'r')
doc = Path('docs', *test.with_suffix('.md').parts[1:])
outfile = open(doc, 'w+')
outfile.write(doctestify(infile.read()))