forked from benbalter/word-to-markdown-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
45 lines (38 loc) · 1.21 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
# frozen_string_literal: true
require 'word-to-markdown'
require 'sinatra'
require 'html/pipeline'
require 'rack/coffee'
require 'tempfile'
module WordToMarkdownServer
class App < Sinatra::Base
helpers do
def html_escape(text)
Rack::Utils.escape_html(text)
end
end
use Rack::Coffee, root: 'public', urls: '/assets/javascripts'
get '/' do
render_template :index, error: nil
end
post '/' do
unless /docx?$/i.match?(params['doc'][:filename])
error = 'It looks like you tried to upload something other than a Word Document.'
render_template :index, error: error
end
md = CGI.escapeHTML(WordToMarkdown.new(params['doc'][:tempfile]).to_s)
html = HTML::Pipeline::MarkdownFilter.new(md).call
render_template :display, md: md, html: html, filename: params['doc'][:filename].sub(/\.docx?$/, '')
end
post '/raw' do
file = Tempfile.new('word-to-markdown')
File.write file.path, request.env['rack.request.form_vars']
markdown = WordToMarkdown.new(file.path).to_s
file.unlink
markdown
end
def render_template(template, locals = {})
halt erb template, layout: :layout, locals: locals
end
end
end