This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
105 lines (75 loc) · 2.07 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
$LOAD_PATH.unshift(File.join(File.dirname(File.realpath(__FILE__)), 'lib'))
require 'rubygems'
# If you're using bundler, you will need to add this
require 'bundler/setup'
require 'sinatra/base'
require 'json'
require 'html/pipeline'
require 'RedCloth'
require 'haml'
# require 'slim'
require 'uri'
require './lib/html/pipeline/haml.rb'
# require 'pry-remote'
class HtmlCompilerApp < Sinatra::Base
set :protection, :except => :frame_options
configure :production do
require 'newrelic_rpm'
end
helpers do
def render_html(html, filter)
context = {
:gfm => true
}
if filter == 'Textile'
filter = HTML::Pipeline::TextileFilter
elsif filter == 'Haml'
filter = HTML::Pipeline::HamlFilter
else
filter = HTML::Pipeline::MarkdownFilter
end
output = HTML::Pipeline.new([filter], context).call(html)[:output]
return output.to_html if output.respond_to?(:to_html)
return output.to_s
end
def origin
return request.env["HTTP_ORIGIN"] if origin_allowed? request.env["HTTP_ORIGIN"]
unless request.referer.nil?
uri = URI.parse(request.referer)
referer = URI.parse('')
referer.scheme = uri.scheme
referer.host = uri.host
return referer.to_s if origin_allowed? referer.to_s
end
return false
end
def origin_allowed?(uri)
return false if uri.nil?
return uri.match(/^http:\/\/(.+\.){0,1}sassmeister\.(com|dev|((\d+\.){4}xip\.io))/)
end
end
before do
params[:syntax].downcase! unless params[:syntax].nil?
if origin
headers 'Access-Control-Allow-Origin' => origin
else
status 403
end
end
get '/' do
erb :render, :layout => false
end
post '/' do
case params[:syntax]
when 'haml'
return render_html(params[:input], 'Haml')
when 'markdown'
return render_html(params[:input], 'Markdown')
when 'textile'
return render_html(params[:input], 'Textile')
else
return params[:input]
end
end
run! if app_file == $0
end