-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanrun
executable file
·188 lines (168 loc) · 4.98 KB
/
panrun
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env ruby
# https://github.com/mb21/panrun
require 'yaml'
# guess output_format from ARGV
def get_output_format()
output_file = nil
ARGV.each_with_index do |a, i|
if a == "-o" || a == "--output"
output_file = ARGV[i+1]
elsif a.start_with? "--output="
output_file = a[9..-1]
end
end
output_format = nil
ARGV.each_with_index do |a, i|
if a == "-t" || a == "--to"
output_format = ARGV[i+1]
elsif a.start_with? "--to="
output_format = a[5..-1]
end
end
if output_format.nil?
if !output_file.nil? && output_file.include?(".")
output_format = output_file.split(".").last
if ["pdf", "tex"].include? output_format
output_format = "latex"
end
end
end
return output_format
end
def get_pandoc_opts()
opts = %x(pandoc --bash-completion).scan(/opts\=\"([^"]*)/).first.first
# strip the two preceding dashes and ignore one-letter variants
opts.split(' ').map do |o|
if o[0..1] == "--"
o[2..-1]
end
end
end
# convert a meta-hash to an arguments-array
def get_args(meta)
pandoc_opts = get_pandoc_opts()
args = []
meta.each do |key, val|
# check whether `key` is an option that can be
# used with the installed pandoc version
if pandoc_opts.include? key
opt = key
else
# since RMarkdown YAML uses e.g. `toc_depth` instead of `toc-depth`
# try that as well:
key = key.gsub('_', '-')
if pandoc_opts.include? key
opt = key
end
end
if opt && val != false
if val.is_a? Hash
val.each do |k, v|
args.push "--" ++ opt
args.push k ++ "=" ++ v
end
elsif val.is_a? Array
val.each do |v|
args.push "--" ++ opt
args.push v
end
else
args.push "--" ++ opt
if not (val.nil? || val.is_a?(TrueClass))
# try to only include a value for an option that takes one
args.push val.to_s
end
end
end
end
if more_args = meta['pandoc_args']
args.concat more_args
end
return args
end
def output_key_name()
"output"
end
def data_dir_name()
if ENV['APPDATA']
# Windows
File.join ENV['APPDATA'], "panrun"
else
# POSIX
File.join Dir.home, ".panrun"
end
end
# try to load default YAML from other files and merge it with local YAML
def get_meta_from_other_file(meta, type=nil)
if not type.is_a?(String)
type = "default"
end
data_dir = data_dir_name()
file_name = if [".", "..", "/", "\\"].include? type[0]
if File.file? type
type
else
abort "Could not find file #{type}"
end
else
# look in ~/.panrun/
name = File.join(data_dir, type + ".yaml")
if File.file? name
name
else
nil
end
end
file_meta, args = if file_name && m = YAML.load_file(file_name)
[ m[output_key_name] || {}, ["--metadata-file", file_name] ]
else
[{}, []]
end
file_meta.each do |format, _|
meta[format] = file_meta[format].merge( meta[format] || {} )
end
return [meta, args]
end
# determine input file
input_file = ARGV[0]
if input_file.nil? || input_file[0] == "-"
abort "Usage: panrun input.md [pandoc-options]\n\n"\
"Looking for default.yaml etc. in #{data_dir_name}\n"\
"For more info, see https://github.com/mb21/panrun"
end
# load and merge various metadata
yaml = YAML.load_file(input_file)
doc_meta = yaml[output_key_name] || {}
meta, file_arg = get_meta_from_other_file doc_meta, yaml["type"]
# determine output format
output_format = get_output_format()
if output_format.nil?
if meta.is_a?(Hash) && meta.first
# fallback to the first output key (hashes are ordered in ruby)
output_format = meta.first[0]
meta_val = meta.first[1]
if not meta_val["to"] || meta_val["output"]
STDERR.puts "panrun: [WARNING] defaulting to the YAML for output format '#{output_format}',\n"\
" but pandoc may not default to the same format.\n"\
" It is recommended to add a `to:` or `output:` field to your YAML."
end
else
abort "Could not find any output format in YAML."
end
end
# lookup format in meta, else try various rmarkdown formats
meta_out = if meta[output_format]
meta[output_format]
elsif meta[output_format + "_document"]
meta[output_format + "_document"]
elsif output_format == "latex" && meta["pdf_document"]
meta["pdf_document"]
elsif meta[output_format + "_presentation"]
meta[output_format + "_presentation"]
else
abort "Could not find YAML key for detected output format '#{output_format}'."
end
args = get_args(meta_out)
args = ["pandoc", input_file] + args + file_arg + ARGV[1..-1]
STDERR.puts "panrun calling: " + args.join(' ')
exec *args