-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
94 lines (76 loc) · 1.68 KB
/
app.rb
File metadata and controls
94 lines (76 loc) · 1.68 KB
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
require 'sinatra/base'
require 'csv'
require 'erb'
include ERB::Util
$FR_INDEX = "index-fr.csv"
class MyApp < Sinatra::Base
# enable :sessions
require './helpers'
set :environment, :production
get '/' do
erb :index, :locals => {:index => true, :classicDescription => true}
end
get '/humans.txt' do
content_type 'text/plain'
erb :humans, :layout => false
end
get '/sitemap.xml' do
content_type 'text/xml'
erb :sitemap, :layout => false
end
get '/:url' do
postsIndex = nil
post = nil
locals = {}
if params[:url] == "blog" then
postsIndex = getPostsIndex
post = getPost postsIndex[0][2]
locals[:classicDescription] = true # used to not change the html description
else
if params[:url] =~ /[a-zA-Z0-9](-|[a-zA-Z0-9])*/ then
post = getPost params[:url]
else
not_found
end
end
if post.nil? then
not_found
end
# when anything except /blog, should look for the index
if postsIndex.nil? then
postsIndex = getPostsIndex
end
locals[:postsIndex] = postsIndex
locals[:post] = post
erb :blog, :locals => locals
end
def getPostsIndex()
return CSV.read $FR_INDEX, {:col_sep => ','}
end
def getPost(title)
post = Array.new
begin
i = 0
File.open('posts/' + title + '.html', 'r').each_line do |line|
if i < 5 then
post << line
i += 1
else
post[4] << line
end
end
rescue
post = nil
end
return post
end
not_found do
postsIndex = getPostsIndex
erb :'404', :locals => {:postsIndex => postsIndex}
end
error do
postsIndex = getPostsIndex
erb :'500', :locals => {:postsIndex => postsIndex}
end
run! if app_file == $0
end