-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.py
executable file
·177 lines (163 loc) · 5.58 KB
/
graphics.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Wes Weimer
#
# This allows students with minimal knowledge to produce pretty pictures of
# HTML webpages.
#
import sys
import subprocess
import os
# If you want the output filenames to be different (e.g., based on
# environment variables), just change them here.
output_latex_filename = "./sam"
# If you want to put the static images elsewhere, just change this.
image_directory = "" # make "" for current directory
# The example image output requires these packages:
#
# pdflatex (aka "pdftex")
# imagemagick (for "convert")
# ghostscript (called by "convert")
outfile = None
logfile = None
import base64
import json
import sys
def word(x):
global outfile
for i in range(len(x)):
if x[i] == '_':
outfile.write("\_")
elif x[i] != '\\':
outfile.write(x[i])
outfile.write (" ")
def warning(x):
global outfile
outfile.write("{\\color{red}{\\bf{" + x + "}}}")
closetags = []
def pushclosing(x):
global closetags
closetags = [x] + closetags
def begintag(tag,args):
global outfile
global logfile
tag = tag.lower()
# need "IMG"
logfile.write("TAG + " + tag + "\n")
if tag == "a":
if "href" in args:
target = args["href"]
outfile.write("\\href{" + target + "}{\underline{")
pushclosing("}}")
else:
warning("invalid 'a' tag: no 'href' argument")
pushclosing("")
elif tag == "img":
if "src" in args:
target = args["src"]
filename = image_directory + target
if os.path.isfile(filename):
if "height" in args and "width" in args:
h = args["height"]
w = args["width"]
outfile.write("\\includegraphics[height=" + h + "px, width=" + w + "px]{" + filename + "}")
pushclosing("")
else:
outfile.write("\\includegraphics{" + filename + "}")
pushclosing("")
else:
warning("'img' " + target + " not found (predefined local images only, sorry)")
pushclosing("")
else:
warning("invalid 'img' tag: no 'src' argument")
pushclosing("")
elif tag == "b" or tag == "strong":
outfile.write("\\textbf{")
pushclosing("}")
elif tag == "ul":
outfile.write("\\begin{itemize}")
pushclosing("\\end{itemize}")
elif tag == "ol":
outfile.write("\\begin{enumerate}")
pushclosing("\\end{enumerate}")
elif tag == "li":
outfile.write("\\item{")
pushclosing("}")
elif tag == "big":
outfile.write("{\\Large ")
pushclosing("}")
elif tag == "tt" or tag == "code":
outfile.write("{\\tt ")
pushclosing("}")
elif tag == "small":
outfile.write("{\\footnotesize ")
pushclosing("}")
elif tag == "i" or tag == "em":
outfile.write("\\emph{")
pushclosing("}")
elif tag == "hr":
outfile.write("{\\begin{center} \\line(1,0){400} \\end{center}}")
pushclosing("")
elif tag == "h1":
outfile.write("\\section*{")
pushclosing("}")
elif tag == "h2":
outfile.write("\\subsection*{")
pushclosing("}")
elif tag == "h3":
outfile.write("\\subsubsection*{")
pushclosing("}")
elif tag == "p" or tag == "br":
outfile.write("\n~\n\n\\noindent ")
pushclosing("\n")
else:
pushclosing("")
def endtag():
global outfile
global logfile
global closetags
if closetags == []:
raise IndexError
tag = closetags[0]
closetags = closetags[1:]
logfile.write("TAG -\n")
outfile.write(tag)
def initialize():
global outfile
global logfile
global output_latex_filename
outfile = open(output_latex_filename + ".tex",'w+')
logfile = open(output_latex_filename + ".taglog",'w+')
outfile.write("""
\\documentclass{article}
\\usepackage{fullpage}
\\usepackage{hyperref}
\\hypersetup{
colorlinks,%
citecolor=blue,%
filecolor=blue,%
linkcolor=blue,%
urlcolor=blue
}
\\usepackage{graphicx}
\\usepackage{color}
\\usepackage{url}
\\usepackage{geometry}
\\pagestyle{empty}
\\begin{document}
\\mag 1440
""")
def finalize():
global outfile
global logfile
logfile.close()
outfile.write("""
\\end{document}
""")
#print "Writing TEX Output: " + output_latex_filename + ".tex"
outfile.close()
#print "Rendering PDF Graphics: " + output_latex_filename + ".pdf"
cmd = "pdflatex " + output_latex_filename + ".tex > sam < sam"
subprocess.call(cmd,shell=True)
#print "Rendering PNG Graphics: " + output_latex_filename + ".png"
cmd = "convert " + output_latex_filename + ".pdf " + \
output_latex_filename + ".png"
subprocess.call(cmd,shell=True)