-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathverbatim.py
executable file
·88 lines (74 loc) · 2.32 KB
/
verbatim.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
import re
import sys
import os
tmpfile = 'tmp.txt'
trinket = False
files = False
if "--trinket" in sys.argv:
trinket = True
intrinketfiles = False
sys.stdout = open(tmpfile, "w")
if "--files" in sys.argv:
files = True
while True:
try:
line = input()
except:
break
# Use . instead of backslash to avoid regex weirdness across python versions
x = re.findall(r'^.VerbatimInput{(.*)}', line)
if not x :
if trinket and files:
trinketfilesstart = r"\begin{trinketfiles}" in line
trinketfilesstop = r"\end{trinketfiles}" in line
if trinketfilesstart:
# We've found extra trinket files to include
print('<--')
intrinketfiles = True
continue
elif trinketfilesstop:
# We're at the end of this block
print('-->')
intrinketfiles = False
continue
elif intrinketfiles:
# We're in a trinket files block; include the file
print("----{" + os.path.basename(line) + "}----")
with open(line) as filetoinclude:
print(filetoinclude.read())
continue
else:
# Otherwise, move on
print(line)
continue
else:
print(line)
continue
fn = x[0]
with open(fn) as fh:
# Pre code
# Open Wrapper
if trinket:
with open('trinket/trinket-script') as ts:
print(ts.read())
else:
print('~~~~ {.python}')
blank = True
# Code
for ln in fh:
print(ln.rstrip())
blank = len(ln.strip()) > 0
# Post Code
# Cannonical URL
if fn.startswith('../') :
# Add a blank unless there is one at end of file
if blank : print()
fu = fn.replace('../','https://www.py4e.com/')
print('# Code:', fu)
if trinket:
print("# Or select Download from this trinket's left-hand menu")
# Close wrapper
if trinket:
print('-->')
else:
print('~~~~')