-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
75 lines (61 loc) · 1.48 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
require 'presto'
require 'kramdown'
require 'RedCloth'
require 'pdfkit'
require 'rbconfig'
unless RbConfig::CONFIG['host_os'] =~ /darwin/
PDFKit.configure do |config|
config.wkhtmltopdf = './bin/wkhtmltopdf-amd64'
# config.default_options = {
# :page_size => 'a4',
# :print_media_type => true
# }
# config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end
end
class MyApp
include Presto::Api
http.map
def index
view.render :index
end
def debug
http.params.inspect
end
def converted
if http.request_method == 'POST'
format = http.params[:format]
text = http.params[:text]
converter = http.params[:converter] || 'markdown'
html_string = to_html(text, converter)
case format
when 'HTML' then
@text = text
@html_string = html_string
view.render :converted
when 'PDF' then
http.content_type do
http.mime_type '.pdf'
end
kit = PDFKit.new(html_string)
pdf = kit.to_pdf
http['Content-Type'] = http.mime_type('.pdf')
pdf
# http.inspect
else
http.params
end
else
view.render :index
end
end
private
def to_html(text, converter = 'markdown')
case converter
when 'markdown' then Kramdown::Document.new(text).to_html
when 'textile' then RedCloth.new(text).to_html
else
text
end
end
end