-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.rb
203 lines (176 loc) · 4.75 KB
/
route.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# encoding: UTF-8
require "sinatra/base"
require "rdiscount"
require "erb"
require "rack-flash"
require "./parserSetence.rb"
Sinatra::Base.set :markdown, :layout_engine => :erb
class TreeEditer < Sinatra::Base
use Rack::Session::Pool
use Rack::Flash
# enable :sessions
set :root, File.dirname(__FILE__)
set :public_folder, Proc.new {File.join(root, "public")}
set :views, Proc.new {File.join(root, "views")}
set :files=>[]
=begin
set :current_setence=>""
set :current_index=>0
set :setence_index=>[]
set :current_file=>""
set :setence_hash=>{}
set :files=>[]
set :text_area=>""
set :graph_area=>""
=end
view_path = root + "/views/"
public_path = root + "/public/"
layout 'background'
configure do
settings.files = Dir["TreeBank/*"]
end
helpers do
def setence_segment content
setence_index = {}
index = 0
setence_regex = Regexp.new("<S[ ]*ID=([0-9]*)>([^<]*)<\/S>", Regexp::MULTILINE)
content.scan(setence_regex).each do |setence_item|
setence_index.store index, setence_item[1]
index += 1
end
setence_index
end
def setence_segment_singleline content
setence_index = {}
index = 0
content.each_line do |setence|
setence_index.store index, setence
index += 1
end
setence_index
end
end
def simple_loadfile filename
content = File.read(filename)
setence_hash = setence_segment content
end
def loadfile filename
content = File.read(filename)
setence_hash = setence_segment_singleline content
session[:current_file] = filename
session[:setence_index] = setence_hash.keys
setence_hash
end
get "/" do
@files = settings.files
if session[:current_file] == nil then
session[:current_file] = settings.files[0]
session[:current_index] = 0
end
setence_hash = loadfile session[:current_file]
current_setence = setence_hash[session[:current_index]]
puts current_setence
@index = session[:setence_index]
@current_index = session[:current_index]
@current_file = session[:current_file]
begin
session[:text_area], @graph_area = parserSetence current_setence
rescue => e
@graph_area = e.message + "<br><br>Note: the judge function may still have bugs"
session[:text_area] = current_setence
puts e.message
puts e.backtrace
end
@setence = session[:text_area]
erb :index, :layout => :background
end
post '/choose' do
if session[:current_file] != params[:treeBankFileName] then
session[:current_file] = params[:treeBankFileName]
end
if params[:index] == nil then
session[:current_index] = 0
else
session[:current_index] = params[:index].to_i
end
redirect "/"
end
post '/nextfile' do
if session[:current_file] == "" then
session[:current_file] = settings.files[0]
end
session[:current_index] = 0
temp = settings.files.find_index(session[:current_file]) + 1
if temp < settings.files.size then
session[:current_file] = settings.files[temp]
@hasNextFile = ""
else
@hasNextFile = "disabled"
end
redirect "/"
end
post '/prefile' do
if session[:current_file] == "" then
session[:current_file] = settings.files[0]
end
session[:current_index] = 0
temp = settings.files.find_index(session[:current_file]) - 1
if temp >= 0 then
session[:current_file] = settings.files[temp]
@hasPreFile = ""
else
@hasPreFile = "disabled"
end
redirect "/"
end
post '/presetence' do
if session[:current_index] - 1 < 0 then
@hasPreSetence = "disabled"
else
@hasPreSetence = ""
session[:current_index] -= 1
end
redirect "/"
end
post '/nextsetence' do
p session[:current_index]
if session[:current_index] + 1 >= session[:setence_index].size then
@hasPreSetence = "disabled"
else
@hasPreSetence = ""
session[:current_index] += 1
end
redirect "/"
end
post '/edit_tree' do
current_setence = params[:content]
begin
session[:text_area], @graph_area = parserSetence current_setence
rescue => e
@graph_area = e.message
session[:text_area] = current_setence
puts e.message
puts e.backtrace
end
@setence = session[:text_area]
@files = settings.files
@index = session[:setence_index]
@current_index = session[:current_index]
@current_file = session[:current_file]
erb :index, :layout => :background
end
post '/write_file' do
setence_hash = loadfile session[:current_file]
setence_hash[session[:current_index]] = oneLine session[:text_area]
outstream = File.open(session[:current_file], "w")
setence_hash.each do |key, value|
outstream.write value
end
outstream.close
redirect "/"
end
not_found do
markdown File.read("#{public_path}not_found.md"), :layout => :background
end
run!
end