-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
51 lines (44 loc) · 1.27 KB
/
main.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 'premailer'
require 'json'
require 'logger'
configure :production do
set :port, 80
end
get '/clean' do
<<-eos
<form method="POST">
<textarea name="html"></textarea>
<button>Submit</button>
<div>With warnings?:
<input type="checkbox" name="with_warnings" value="1"/>
</div>
<div>Remove comments:
<input type="checkbox" name="remove_comments" value="1"/>
</div>
<div>Remove script tags:
<input type="checkbox" name="remove_script_tags" value="1"/>
</div>
</form>
eos
end
post '/clean' do
error 400 if !params[:html]
logger = Logger.new('/var/log/passenger/premailer.log', 10, 10485760)
logger.info { "pid:#{$$} " + @params.inspect }
with_warnings = params[:with_warnings] == '1' ? true : false
remove_comments = params[:remove_comments] == '1' ? true : false
remove_script_tags = params[:remove_script_tags] == '1' ? true : false
html = params[:html]
premailer = Premailer.new(html,
:warn_level => Premailer::Warnings::SAFE,
:with_html_string => true,
:preserve_styles => true,
:remove_comments => remove_comments,
:remove_script_tags => remove_script_tags
)
content_type :json
data = {:html => premailer.to_inline_css}
data[:warnings] = premailer.warnings if with_warnings
data.to_json
end