forked from ultracoldYEG/spinor-gpe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
74 lines (63 loc) · 2.2 KB
/
preprocess.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
"""Modification of code generating a README.rst file from other .rst files.
Run this script before pushing documentation changes.
ORIGINAL AUTHOR: rendaw
YEAR: 2017
LICENSE: https://opensource.org/licenses/BSD-2-Clause
SOURCE: https://github.com/github/markup/issues/1104
"""
import glob
import re
import os.path
for source in glob.glob('./**/*.rst.src', recursive=True):
dirname = os.path.dirname(source)
def include(match):
# Reads a .rst file and returns its contents as a string
with open(os.path.join(dirname, match.group('filename')), 'r') as f:
body = f.read()
return body
def literalinclude(match):
# Reads a .rst file and returns its contents as a code block directive
with open(os.path.join(dirname, match.group('filename')), 'r') as f:
body = f.read()
out = '.. code-block::'
if match.group('language'):
out += ' ' + match.group('language')
out += '\n'
body = body.splitlines()
start = match.group('start')
end = match.group('end')
if start is not None:
body = body[int(start) - 1:int(end)]
deindent = None
for line in body:
line_indent = len(re.search('^( *)', line).group(1))
if deindent is None or line_indent < deindent:
deindent = line_indent
out += '\n'
out += '\n'.join(' ' + line[deindent:] for line in body)
return out
dest = re.sub('\\.src', '', source)
with open(source, 'r') as f:
text = f.read()
# Substitude contents of file for every '.. include' directive
text = re.sub(
'^\\.\\. include:: (?P<filename>.*)$',
include,
text,
flags=re.M,
)
# Substitude contents of file for every '.. literalinclude' directive
# Replace it with a code block
text = re.sub(
'^\\.\\. literalinclude:: (?P<filename>.*)$'
'(?:\\s^(?:'
'(?: :language: (?P<language>.*))|'
'(?: (?P<linenos>:linenos:))|'
'(?: :lines: (?P<start>.*)-(?P<end>.*))'
')$)*',
literalinclude,
text,
flags=re.M,
)
with open(dest, 'w') as f:
f.write(text)