-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.rb
106 lines (93 loc) · 2.4 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
106
# -*- coding: utf-8 -*-
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/r18n'
require 'rack-flash'
require 'haml/html'
require './haml_ext'
require 'open-uri'
require 'hpricot'
require 'nkf'
require 'sprockets'
require 'oulu'
require 'uglifier'
require 'autoprefixer-rails'
Encoding.default_external = 'utf-8'
class App < Sinatra::Base
configure do
use Rack::Session::Cookie
use Rack::Flash
use Rack::Static, :urls => ['/images'], :root => 'public'
set :app_file, __FILE__
set :haml, {:attr_wrapper => '"', :ugly => false}
set :raise_errors, true
register Sinatra::R18n
end
configure do
Oulu.load_paths.each do |path|
assets.append_path(path)
end
end
assets = Sprockets::Environment.new do |env|
# This ensures sprockets can find the CSS files
env.append_path "assets/stylesheets/"
env.append_path "assets/javascripts/"
end
AutoprefixerRails.install(assets)
configure :development do
register Sinatra::Reloader
end
helpers do
alias h escape_html
end
get '/assets/stylesheets/*' do
env["PATH_INFO"].sub!("/assets/stylesheets", "")
assets.call(env)
end
get '/assets/javascripts/*' do
env["PATH_INFO"].sub!("/assets/javascripts", "")
assets.call(env)
end
get '/' do
if params[:url]
content_type 'text/plain', :charset => 'utf-8'
begin
ua = request.user_agent || "Ruby/#{RUBY_VERSION}"
source = NKF.nkf('-w', open(params[:url], "User-Agent" => ua){|f| f.read })
html2haml(source)
rescue Haml::SyntaxError => e
case e.message
when 'Invalid doctype'
halt 500, t.invalid_doctype
else
halt 500, e.message
end
end
else
haml :index
end
end
post '/' do
begin
if params[:source].empty?
flash[:error] = t.required_html_tags
else
@html = html2haml(params[:source])
end
rescue Haml::SyntaxError => e
case e.message
when 'Invalid doctype'
flash[:error] = t.invalid_doctype
else
flash[:error] = e.message
end
end
haml :created
end
private
def html2haml(html)
html5 = (doctype = Hpricot(html).children.detect{ |e| e.doctype? }) ? doctype.public_id.nil? : false
haml = Haml::HTML.new(html.gsub(/\t/, ' ')).render
Haml::Engine.new(haml, :attr_wrapper => '"', :format => html5 ? :html5 : :xhtml).render
end
end