forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verbatim.py
executable file
·105 lines (87 loc) · 2.64 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
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 = raw_input()
except:
break
x = re.findall('\\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('../','http://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 '~~~~'
if trinket:
# Clean up
sys.stdout.close()
sys.stdout = sys.__stdout__
if files:
# Remove -->\n<--
with open(tmpfile, 'r') as f:
cleanfile = re.sub('-->\s*?\n<--\s*?\n', '', f.read(), flags=re.MULTILINE)
print cleanfile
else:
with open(tmpfile, 'r') as f:
print f.read()
os.remove(tmpfile)