/
kramdown
80 lines (70 loc) · 2.39 KB
/
kramdown
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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2012 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown.
#
# kramdown is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
#
require 'optparse'
require 'kramdown'
options = {}
format = ['html']
OptionParser.new do |opts|
opts.banner = "Usage: kramdown [options] [FILE FILE ...]"
opts.summary_indent = ' '*4
opts.separator ""
opts.separator "Command line options:"
opts.separator ""
opts.on("-i", "--input ARG", "Specify the input format: kramdown (default) or html") {|v| options[:input] = v}
opts.on("-o", "--output ARG", Array, "Specify one or more output formats separated by commas: html (default), kramdown, latex or remove_html_tags") {|v| format = v}
opts.on("-v", "--version", "Show the version of kramdown") do
puts Kramdown::VERSION
exit
end
opts.on("-h", "--help", "Show the help") do
puts opts.summarize('', 5, 72)
exit
end
opts.separator ""
opts.separator "kramdown options:"
opts.separator ""
Kramdown::Options.definitions.each do |n, definition|
no = n.to_s.tr('_', '-')
if definition.type == Kramdown::Options::Boolean
opts.on("--[no-]#{no}") {|v| options[n] = Kramdown::Options.parse(n, v)}
else
type = definition.type
type = String if type == Symbol || type == Object
opts.on("--#{no} ARG", type) {|v| options[n] = Kramdown::Options.parse(n, v)}
end
definition.desc.split(/\n/).each do |line|
opts.separator opts.summary_indent + ' '*6 + line
end
opts.separator ''
end
end.parse!
begin
doc = Kramdown::Document.new(ARGF.read, options)
result = ''
format.each {|f| result = doc.send("to_#{f}")}
puts result
doc.warnings.each {|warn| $stderr.puts "Warning: #{warn}"}
rescue Kramdown::Error => e
$stderr.puts "Error: #{e.message}"
exit(1)
end