forked from rmondello/Marx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marx.rb
executable file
·108 lines (90 loc) · 2.33 KB
/
Marx.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
107
108
#!/usr/bin/ruby
# Marx
# Richard Mondello
# https://github.com/rmondello/Marx
# Constants
FORMAT = "pdf" # either "pdf" or "html"
STYLESHEET = nil # style.css
MARKDOWN = "Markdown.pl"
PDF = "wkhtmltopdf"
ERROR_CODE = 1
BANNER = "Usage: Marx.rb in.mdown out.(pdf|html) [options]"
# Dependency check
def prg_exists?(prg)
`which #{prg}`
$? == 0
end
unless prg_exists? MARKDOWN
puts "Error: could not locate Markdown script"
puts "http://daringfireball.net/projects/markdown/"
exit ERROR_CODE
end
unless prg_exists? PDF
puts "Error: could not locate html to pdf script"
puts "http://code.google.com/p/wkhtmltopdf/"
exit ERROR_CODE
end
# Options parsing
require "optparse"
require "tempfile"
options = {}
optparse = OptionParser.new do |opts|
opts.banner = BANNER
options[:stylesheet] = STYLESHEET
opts.on( '-s', '--stylesheet file', 'use file as stylesheet' ) do |file|
options[:stylesheet] = file
end
options[:format] = nil
opts.on( '-f', '--format fmt',
'html|pdf, overrides file extension inference' ) do |fmt|
options[:format] = fmt
end
opts.on( '-h', '--help', 'display this screen' ) do
puts opts
exit ERROR_CODE
end
end
input = ARGV.shift
output = ARGV.shift
unless input and output
puts BANNER
exit ERROR_CODE
end
begin
optparse.parse!
rescue => e
e.recover(ARGV) # put rejected arguments back
end
# Output format inference
unless options[:format] # -f (--format) flag overrides filename
if /(.htm|.html)$/.match(output)
options[:format] = "html"
elsif /(.pdf$)/.match(output)
options[:format] = "pdf"
else
options[:format] = FORMAT
end
end
# Run scripts
input = File.expand_path(input).gsub(/\s/, '\ ')
output = File.expand_path(output).gsub(/\s/, '\ ')
temp = Tempfile.new 'Marx'
temp2 = Tempfile.new 'Marx'
# check for stylesheet
`#{MARKDOWN} #{input} > #{temp.path}`
if options[:stylesheet]
stylesheet = File.expand_path options[:stylesheet]
`cat #{stylesheet} #{temp.path} > #{temp2.path}`
`mv #{temp2.path} #{temp.path}`
end
if /(htm|html)/i.match options[:format]
`mv #{temp.path} #{output}`
elsif /(pdf)/i.match options[:format]
`cat #{temp.path} | #{PDF} --page-size Letter #{ARGV.join ' '} - - > #{output}`
else
puts "Error: No output format specified."
exit ERROR_CODE
end
# Clean up
temp.close!
temp2.close!