Debian Codegraph @

bin/maruku

raw symbols SCIP
1 #!/usr/bin/env ruby
2
3 dir = File.join(File.dirname(__FILE__), '..', 'lib')
4 $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
5
6 require 'maruku'
7 require 'optparse'
8
9
10 def cli_puts(s)
11 $stderr.puts(s) if MaRuKu::Globals[:verbose]
12 end
13
14 def benchmark(section)
15 t = Time.now
16 res = yield
17 cli_puts("%s finished in %.2f seconds." % [section, Time.now - t])
18 res
19 end
20
21 export = :html
22 break_on_error = false
23 using_math = false
24 using_mathml = false
25 output_file = nil
26
27 opt = OptionParser.new do |opts|
28 opts.banner = "Usage: #{File.basename($0)} [options] [file1.md] [file2.md] ..."
29
30 opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
31 MaRuKu::Globals[:verbose] = v
32 end
33
34 opts.on("-u", "--[no-]unsafe", "Use unsafe features") do |v|
35 MaRuKu::Globals[:unsafe_features] = v
36 end
37
38 opts.on("-b", "--[no-]break", "Break on error") do |v|
39 break_on_error = v
40 end
41
42
43 opts.on("-i", "--math-images ENGINE", "Use ENGINE to render TeX to PNG") do |s|
44 using_math = true
45 MaRuKu::Globals[:html_math_output_png] = true
46 MaRuKu::Globals[:html_math_output_mathml] = false
47 MaRuKu::Globals[:html_png_engine] = s
48 cli_puts "Using png engine #{s}."
49 end
50
51 opts.on("-m", "--math-engine ENGINE", "Use ENGINE to render MathML") do |s|
52 MaRuKu::Globals[:html_math_output_png] = false
53 MaRuKu::Globals[:html_math_output_mathml] = true
54 using_math = true
55 using_mathml = true
56 MaRuKu::Globals[:html_math_engine] = s
57 end
58
59 opts.on("-o", "--output FILE", "Output filename (`-o -' writes to stdout)") {|s| output_file = s}
60
61 opts.on_tail("--pdf", "Output PDF;", "first writes LaTeX, then calls pdflatex") {export = :pdf}
62 opts.on_tail("--s5", "Output S5 slideshow") {export = :s5}
63 opts.on_tail("--html", "Output HTML") {export = :html}
64 opts.on_tail("--html-frag", "Output the contents of the <body> tag") {export = :html_frag}
65 opts.on_tail("--tex", "Output LaTeX" ) {export = :tex}
66 opts.on_tail("--markdown", "Output Markdown" ) {export = :markdown}
67 opts.on_tail("--inspect", "Output the parse tree") {export = :inspect}
68
69 opts.on_tail("--ext EXTENSIONS", "Use Maruku extensions (comma separated)") do |s|
70 s.split(",").each {|e| require "maruku/ext/#{e}"}
71 end
72
73 opts.on_tail("-h", "--help", "Show this help message") do
74 puts opts
75 exit
76 end
77
78 opts.on_tail("--version", "Show version") do
79 puts "Maruku #{MaRuKu::Version}"
80 exit
81 end
82 end
83
84 begin
85 opt.parse!
86 rescue OptionParser::InvalidOption => e
87 $stderr.puts e
88 $stderr.puts opt
89 exit 1
90 end
91
92
93 if using_math
94 cli_puts "Using Math extensions."
95 require 'maruku/ext/math'
96 end
97
98
99 unless ARGV.empty?
100 if ARGV.size > 1 && output_file
101 $stderr.puts "Can't write #{ARGV.map {|f| f.inspect}.join(' and ')} to #{output_file.inspect}"
102 exit 1
103 end
104
105 # If we are given filenames, convert each file
106 ARGV.map do |f|
107 # read file content
108 cli_puts "Reading from file #{f.inspect}."
109 [f, File.read(f)]
110 end
111 else
112 export = :html_frag if export == :html
113 export = :tex_frag if export == :tex
114
115 cli_puts "Reading from standard input."
116 [[nil, $stdin.read]]
117 end.each do |filename, input|
118 doc = benchmark("Parsing") {Maruku.new(input, :on_error => (break_on_error ? :raise : :warning))}
119
120 out = ""
121 suffix = "?"
122 benchmark("Rendering") do
123 case export
124 when :html
125 suffix = using_mathml ? '.xhtml' : '.html'
126 out = doc.to_html_document
127 when :html_frag
128 suffix = '.html_frag'
129 out = doc.to_html
130 when :pdf, :tex
131 suffix = '.tex'
132 out = doc.to_latex_document
133 when :tex_frag
134 suffix = '.tex_frag'
135 out = doc.to_latex
136 when :inspect
137 suffix = '.txt'
138 out = doc.inspect
139 when :markdown
140 suffix = '.md'
141 out = doc.to_md
142 when :s5
143 suffix = '_s5slides.html'
144 out = doc.to_s5(:content_only => false, :print_slides => true)
145 end
146 end
147
148 if (output_file.nil? && filename.nil?) || output_file == "-"
149 if export == :pdf
150 $stderr.puts "Can't write PDF to stdout"
151 exit 1
152 end
153
154 cli_puts "Writing to standard output"
155 $stdout.puts out
156 next
157 end
158
159 if output_file
160 job = File.basename(output_file, File.extname(output_file))
161 else
162 dir = File.dirname(filename)
163 job = File.join(dir, File.basename(filename, File.extname(filename)))
164 output_file = job + suffix
165 end
166
167 if export == :pdf
168 cli_puts "Writing to #{job}.tex"
169 File.open("#{job}.tex", 'w') {|f| f.puts out}
170 cmd = ["pdflatex", "#{job}.tex", "-interaction=nonstopmode"]
171 cmd << "-output-directory=#{dir}" if dir
172 cli_puts "maruku: executing $ #{cmd.join(' ')}"
173
174 # run twice for cross references
175 system *cmd
176 system *cmd
177 next
178 end
179
180 cli_puts "Writing to #{output_file.inspect}"
181 File.open(output_file, 'w') {|f| f.puts out}
182 end