-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
174 lines (152 loc) · 4.82 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
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
# frozen_string_literal: true
require "redcarpet"
require "sinatra"
require "base64"
require "securerandom"
require "json"
require "rsyntaxtree"
require_relative "lib/rsyntaxtree_web/version"
$RSYNTAXTREE_VER = Gem.loaded_specs["rsyntaxtree"].version.to_s
# Use Google Analytics code only if the code file exists
ga_path = File.dirname(__FILE__) + "/google_analytics_tracking_code"
if File.exist?(ga_path)
gfile = File.open(ga_path, "r:UTF-8:UTF-8")
$GOOGLE_CODE = gfile.read
gfile.close
else
$GOOGLE_CODE = ""
end
class CustomRenderer < Redcarpet::Render::HTML
def image(link, title, alt_text)
if title =~ /([^=\s]+)=([^=\s]+)/
%(<a href="#{link}" target="_blank"><img src="#{link}" #{$1}="#{$2}" class=md-img' alt="#{alt_text}" /></a>)
else
%(<a href="#{link}" target="_blank"><img src="#{link}" title="#{title}" class='md-img' alt="#{alt_text}" /></a>)
end
end
def table(header, body)
"<table class='table table-sm table-bordered '>" \
"<thead>#{header}</thead>" \
"<tbody>#{body}</tbody>" \
"</table>"
end
end
markdown = Redcarpet::Markdown.new(CustomRenderer, autolink: true, fenced_code_blocks: true, tables: true, with_toc_data: true)
about_md = File.read(File.dirname(__FILE__) + "/about.md")
ABOUT_HTML = markdown.render(about_md)
about_md_ja = File.read(File.dirname(__FILE__) + "/about_ja.md")
ABOUT_HTML_JA = markdown.render(about_md_ja)
configure do
enable :sessions
end
# the default / route, whose views are in the '/views' directory
get '/' do
erb :index
end
# set '/ja' to the Japanese version of the site, whose views are in the '/views/ja' directory
get '/ja' do
erb :"ja/index", :layout => :"ja/layout"
end
post '/check' do
data = params["data"]
begin
result = RSyntaxTree::RSGenerator.check_data(data)
if result
{ status: "success", message: "OK" }.to_json
else
{ status: "failure", message: "NG" }.to_json
end
rescue RSTError => e
{ status: "failure", message: e.message.gsub("\n", "<br />") }.to_json
rescue StandardError
{ status: "failure", message: "Error: invalid input" }.to_json
end
end
# make sure the image is generated by really generating it
post '/check_plus' do
data = params["data"]
begin
result = RSyntaxTree::RSGenerator.check_data(data)
return { status: "failure", message: "NG" }.to_json unless result
rs_generator = RSyntaxTree::RSGenerator.new(params)
tree = rs_generator.draw_tree
tree ? { status: "success", message: "OK" }.to_json : raise
rescue RSTError => e
{ status: "failure", message: e.message.gsub("\n", "<br />") }.to_json
rescue StandardError
{ status: "failure", message: "Error: invalid input" }.to_json
end
end
post '/draw_png' do
basename = "syntree.png"
rs_generator = RSyntaxTree::RSGenerator.new(params)
png_blob = rs_generator.draw_png
response.headers['content_type'] = "image/png"
response.headers['content_length'] = png_blob.size.to_s
response.headers['content_disposition'] = "inline" + %(; filename="#{basename}")
{ status: "success", "png" => Base64.encode64(png_blob) }.to_json
rescue RSTError => e
{ status: "failure", message: e.message.gsub("\n", "<br />") }.to_json
rescue StandardError
{ status: "failure", message: "Error: invalid input" }.to_json
end
post '/draw_svg' do
basename = "syntree.svg"
rs_generator = RSyntaxTree::RSGenerator.new(params)
svg = rs_generator.draw_svg
response.headers['content_type'] = "image/svg+xml"
response.headers['content_length'] = svg.size.to_s
response.headers['content_disposition'] = "inline" + %(; filename="#{basename}")
{ status: "success", svg: Base64.encode64(svg) }.to_json
rescue RSTError => e
{ status: "failure", message: e.message.gsub("\n", "<br />") }.to_json
rescue StandardError
{ status: "failure", message: "Error: invalid input" }.to_json
end
post '/download_svg' do
begin
rs_generator = RSyntaxTree::RSGenerator.new(params)
svg = rs_generator.draw_svg
rescue StandardError
error 500
end
content_type 'image/svg+xml'
attachment 'syntree.svg'
svg
end
post '/download_png' do
begin
rs_generator = RSyntaxTree::RSGenerator.new(params)
png = rs_generator.draw_png
rescue StandardError
error 500
end
content_type 'image/png'
attachment 'syntree.png'
png
end
post '/download_pdf' do
begin
rs_generator = RSyntaxTree::RSGenerator.new(params)
pdf = rs_generator.draw_pdf
rescue StandardError
error 500
end
content_type 'application/pdf'
attachment 'syntree.pdf'
pdf
end
sample1 = []
sample1 << "[S"
sample1 << " [NP |R| SyntaxTree]"
sample1 << " [VP"
sample1 << " [V generates]"
sample1 << " [NP"
sample1 << " [Adj #\\+multilingual\\"
sample1 << " \\+beautiful]"
sample1 << " [NP syntax\\"
sample1 << " trees]"
sample1 << " ]"
sample1 << " ]"
sample1 << "]"
$SAMPLE1 = sample1.join("\n")