forked from benbalter/markdown-to-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
62 lines (51 loc) · 1.19 KB
/
server.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
require 'sinatra'
require 'octokit'
require 'dotenv'
require 'pdfkit'
module MarkdownToPDF
class App < Sinatra::Base
configure :development do
Dotenv.load
end
configure :production do
require 'wkhtmltopdf-heroku'
require 'rack-ssl-enforcer'
use Rack::SslEnforcer
end
def nwo
"#{params['owner']}/#{params['repo']}"
end
def path
params['splat'].join('/').gsub(/\.pdf$/, '.md')
end
def ref
params['ref']
end
def client
@client ||= Octokit::Client.new access_token: ENV['GITHUB_TOKEN']
end
def markdown
@markdown ||= begin
response = client.contents nwo, path: path, ref: ref
Base64.decode64(response.content).force_encoding('utf-8')
end
end
def html
@html ||= client.markdown markdown, context: nwo
end
def stylesheet
File.expand_path 'public/bootstrap/dist/css/bootstrap.css'
end
def kit
@kit ||= begin
kit = PDFKit.new(html, page_size: 'Letter')
kit.stylesheets << stylesheet
kit
end
end
get '/:owner/:repo/blob/:ref/*' do
content_type 'application/pdf'
kit.to_pdf
end
end
end