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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#!/usr/bin/env ruby
# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#
require 'optparse'
require 'rbconfig'
require 'yaml'
require 'kramdown'
def add_kramdown_options(opts, parsed_options, banner: [], ignore: [])
banner_shown = false
defined_options = []
Kramdown::Options.definitions.sort.each do |n, definition|
next if ignore.include?(n)
unless banner_shown
opts.separator("")
banner.each {|part| opts.separator(part) }
opts.separator("")
banner_shown = true
end
defined_options << n
no = n.to_s.tr('_', '-')
if definition.type == Kramdown::Options::Boolean
opts.on("--[no-]#{no}") {|v| parsed_options[n] = Kramdown::Options.parse(n, v) }
else
type = definition.type
type = String if type == Symbol || type == Object
opts.on("--#{no} ARG", type) {|v| parsed_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
defined_options
end
config_file = nil
begin
config_dir = case RbConfig::CONFIG['host_os']
when /bccwin|cygwin|djgpp|mingw|mswin|wince/i
File.expand_path((ENV['HOME'] || ENV['USERPROFILE'] || "~") + "/AppData/Local")
when /darwin|mac os/
File.expand_path("~/Library/Preferences/")
else
File.expand_path(ENV['XDG_CONFIG_HOME'] || '~/.config')
end
config_file = File.join(config_dir, "kramdownrc")
rescue StandardError
end
options = {}
format = ['html']
defined_options = []
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), " \
"html, or markdown") {|v| options[:input] = v }
opts.on("-o", "--output ARG", Array, "Specify one or more output formats separated by commas: " \
"html (default),", "kramdown, latex, man or remove_html_tags") {|v| format = v }
opts.on("-x", "--extension EXT", Array, "Load one or more extensions (without the 'kramdown-' " \
"prefix) separated", "by commas (e.g. parser-gfm,syntax-coderay)",
"Note: Use this option before other options!") do |exts|
exts.each do |ext|
require "kramdown-#{ext}"
new_options = add_kramdown_options(opts, options, banner: ["#{ext} options:"],
ignore: defined_options)
defined_options.concat(new_options)
rescue LoadError
$stderr.puts "Couldn't load extension #{ext}, ignoring"
end
end
opts.separator ""
opts.on("--no-config-file", "Do not read any configuration file. Default behavior is to check " \
"for a", "configuration file and read it if it exists.") { config_file = nil }
opts.on("--config-file FILE", "Specify the name of a configuration file with kramdown options " \
"in YAML", "format, e.g. \"auto_id_prefix: ARG\" instead of \"--auto-id-prefix ARG\"",
"and \"auto_ids: false\" instead of \"--no-auto-ids\".",
"Default: #{config_file}") {|v| config_file = v }
opts.separator ""
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
new_options = add_kramdown_options(opts, options, banner: ["kramdown options:"])
defined_options.concat(new_options)
end.parse!
begin
if config_file && File.exist?(config_file)
config_file_options =
if YAML.method(:safe_load).parameters.include?(%i[key permitted_classes])
YAML.safe_load(File.read(config_file), permitted_classes: [Symbol])
else # compatibility with Psych < 3.1.0
YAML.safe_load(File.read(config_file), [Symbol])
end
case config_file_options
when nil # empty configuration file except perhaps YAML header and comments
# Nothing to do
when Hash
options = config_file_options.merge(options)
else
raise Kramdown::Error, "No YAML map in configuration file \"#{config_file}\""
end
end
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
|