-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
executable file
·149 lines (120 loc) · 4.23 KB
/
app.rb
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
# SinatraTex - A Sinatra Web App which lets you convert LaTeX to different formats (pdf/png)
#
# 1) post to /pdf converts tex to pdf | params: { name: 'filename', tex: 'tex' }
# 2) get to /png converts math tex to png | params: { tex: "y=f(x)" }
# Example: localhost:9292/png?tex=y=f(x)
#
# Dependencies: LaTeX, STIX Fonts
require "rubygems"
require "sinatra/base"
require 'digest'
require 'json'
load 'settings.rb'
class SinatraTex < Sinatra::Base
def log(type, msg)
remote_ip = request.ip
date = Time.now.strftime("%d/%b/%Y %T %Z")
f = open(LOGFILE, 'a')
f.write("%s [%s] %s \"%s\"\n" % [remote_ip, date, type, msg])
f.close()
end
post "/pdf" do
name = params[:name]
tex = params[:tex]
images = JSON.parse(params[:images]) if params[:images]
# Prepare images
if images && images.any?
images.each do |image|
# Check if image with same name exists, if not download it with wget
image_path = "%s/%s" % [TEMP_IMAGES, image["name"]]
unless File.exists?(image_path)
system("wget '#{image["url"]}' -O '#{image_path}'")
end
end
end
# Prepare data
m = Digest::MD5.new
m.update("%s%s" % [name, tex])
uid = m.hexdigest
# Compute the resulting pdf path
pdf_path = "%s/%s.pdf" % [TEMP_PDF, uid]
# This file already exists
if File.exists?(pdf_path)
# Return existing pdf
log("render pdf", "%s" % [name])
content_type "application/pdf"
return File.read(File.join(pdf_path))
end
tex_path = '%s/%s.tex' % [TEMP_TEX, uid]
tex_file = File.open(tex_path, 'w')
tex_file.write(tex)
tex_file.close
# system("pdflatex --interaction=nonstopmode -output-directory=%s %s" % [TEMP_PDF, tex_path])
# system("pdflatex --interaction=nonstopmode -output-directory=%s %s" % [TEMP_PDF, tex_path])
`latexmk -f -pdf -output-directory=#{TEMP_PDF} #{tex_path}`
log("render pdf", "%s" % [name])
if File.exists?(pdf_path)
content_type "application/pdf"
return File.read(File.join(pdf_path))
else
dvi_log_path = "%s/%s.log" % [TEMP_DVI, uid]
log("error", "The compilation failed, see %s in the dvi temporary directory" % [dvi_log_path])
end
end
# The png conversion is based on Thomas Pelletier's Tex2Png (https://github.com/pelletier/tex2png), thanks for the bacon!
get "/png" do
# LaTeX document skeleton
document_top = "\\documentclass{article}
\\usepackage{stix}
\\usepackage{mathtools}
\\everymath{\\displaystyle}
\\begin{document}
\\pagestyle{empty}
$"
document_bottom = "$
\\end{document}"
# Document resolution, hardcoded.
resolution = 119
tex = params[:tex]
# Prepare data
m = Digest::MD5.new
m.update("%s" % [tex])
uid = m.hexdigest
# Compute the resulting png path
png_path = "%s/%s.png" % [TEMP_PNG, uid]
# This file already exists
if File.exists?(png_path)
# Return existing image
log("render png", "%s" % [tex])
content_type "image/png"
return File.read(File.join(png_path))
end
# Assemble the complete document source code (head + corp + foot)
complete_tex_doc = "%s%s%s" % [document_top, tex, document_bottom]
# Write the tex file
tex_path = '%s/%s.tex' % [TEMP_TEX, uid]
tex_file = File.open(tex_path, 'w')
tex_file.write(complete_tex_doc)
tex_file.close()
# Convert to dvi
system("latex --interaction=nonstopmode -output-directory=%s %s" % [TEMP_DVI, tex_path])
log("render png", "%s" % [tex])
# We do not need the tex file anymore
system("rm %s" % [tex_path])
# Compute the complete dvi file path
dvi_path = "%s/%s.dvi" % [TEMP_DVI, uid]
# The compilation succeeded, have fun with png
if File.exists?(dvi_path)
# Convert to png
system("dvipng -T tight -bg Transparent -D %s -o %s %s" % [resolution, png_path, dvi_path])
# Remove the dvi file
system("rm %s" % [dvi_path])
# Return the result of our beautiful work
content_type "image/png"
File.read(File.join(png_path))
else
dvi_log_path = "%s/%s.log" % [TEMP_DVI, uid]
log("error", "The compilation failed, see %s in the dvi temporary directory" % [dvi_log_path])
end
end
end