forked from sitevalidator/sourceviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
51 lines (40 loc) · 1.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
require 'sinatra'
require 'open-uri'
require 'open_uri_redirections'
set :show_exceptions, false
get '/show' do
@url = params[:url]
@source = open(@url, allow_redirections: :all, "User-Agent" => user_agent).read
@theme = pick_theme
erb :show
end
get '/500' do
return "SORRY! We could not process this page."
end
error 500 do
redirect to('/500')
end
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
private
def user_agent
params[:user_agent] || default_user_agent
end
def default_user_agent
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36"
end
# Takes the theme if available, or uses the default
def pick_theme
highlight_themes.include?(params[:theme]) ? params[:theme] : 'default'
end
# Available themes from http://cdnjs.com/libraries/highlight.js/
def highlight_themes
%w(arta ascetic brown_paper dark default docco far foundation github
googlecode idea ir_black magula mono-blue monokai monokai_sublime obsidian
pojoaque railscasts rainbow school_book solarized_dark solarized_light
sunburst tomorrow-night-blue tomorrow-night-bright tomorrow-night-eighties
tomorrow-night tomorrow vs xcode zenburn)
end