pandocomatic 0.1.4.7 → 0.1.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pandocomatic/command/convert_file_command.rb +208 -204
- data/lib/pandocomatic/pandocomatic.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0734cbbb6eb522f731ca8327ab9fcf5ae511a98
|
4
|
+
data.tar.gz: e8f266ce4bece89cd72b6d9a01bde7b0a87685d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbfc1ed17b98565d55972b95827438153e9d7b9d06a81432f69275c86e9f91ab10c848fe468f683596eae82a79e09830589aa33e18b647bac0906eb9ddb8af32
|
7
|
+
data.tar.gz: afde83d1a2639beb117f86349c241289df49c66e5654da6e9a8aac4efb9db513fe3cf891e30b8d9af22a7ff03ae4434a75ebc080e840c3fb067bb86943d2445e
|
@@ -18,231 +18,235 @@
|
|
18
18
|
#++
|
19
19
|
module Pandocomatic
|
20
20
|
|
21
|
-
|
21
|
+
require 'paru'
|
22
|
+
require 'shellwords'
|
22
23
|
|
23
|
-
|
24
|
+
require_relative '../pandoc_metadata.rb'
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
require_relative '../processor.rb'
|
27
|
+
require_relative '../processors/fileinfo_preprocessor'
|
28
|
+
require_relative '../processors/metadata_preprocessor'
|
28
29
|
|
29
|
-
|
30
|
+
require_relative '../configuration.rb'
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
require_relative '../error/io_error.rb'
|
33
|
+
require_relative '../error/configuration_error.rb'
|
34
|
+
require_relative '../error/processor_error.rb'
|
34
35
|
|
35
|
-
|
36
|
+
require_relative 'command.rb'
|
36
37
|
|
37
|
-
|
38
|
+
OUTPUT_FORMATS = ["docx", "odt", "pdf", "beamer"]
|
38
39
|
|
39
|
-
|
40
|
+
class ConvertFileCommand < Command
|
40
41
|
|
41
|
-
|
42
|
+
attr_reader :config, :src, :dst
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
44
|
+
# Create a new ConvertFileCommand
|
45
|
+
#
|
46
|
+
# @param config [Configuration] pandocomatic's configuration
|
47
|
+
# @param src [String] the path to the file to convert
|
48
|
+
# @param dst [String] the path to save the output of the conversion
|
49
|
+
# @param template_name [String = nil] the template to use while converting
|
50
|
+
# this file
|
51
|
+
def initialize(config, src, dst, template_name = nil)
|
52
|
+
super()
|
53
|
+
@config = config
|
54
|
+
@src = src
|
55
|
+
@dst = dst
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
if template_name.nil? or template_name.empty?
|
58
|
+
@template_name = @config.determine_template @src
|
59
|
+
else
|
60
|
+
@template_name = template_name
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
# Execute this ConvertFileCommand
|
68
|
-
def run
|
69
|
-
convert_file
|
70
|
-
end
|
71
|
-
|
72
|
-
# Create a string representation of this ConvertFileCommand
|
73
|
-
#
|
74
|
-
# @return [String]
|
75
|
-
def to_s
|
76
|
-
"convert #{File.basename @src} #{"-> #{File.basename @dst}" unless @dst.nil?}"
|
77
|
-
end
|
78
|
-
|
79
|
-
private
|
80
|
-
|
81
|
-
def convert_file
|
82
|
-
metadata = PandocMetadata.load_file @src
|
83
|
-
pandoc_options = metadata.pandoc_options || {}
|
84
|
-
template = {}
|
85
|
-
|
86
|
-
# Determine the actual options and settings to use when converting this
|
87
|
-
# file.
|
88
|
-
if not @template_name.nil? and not @template_name.empty?
|
89
|
-
raise ConfigurationError.new(:no_such_template, nil, @template_name) unless @config.has_template? @template_name
|
90
|
-
template = @config.get_template @template_name
|
91
|
-
|
92
|
-
pandoc_options = (template['pandoc'] || {}).merge(pandoc_options) do |key, oldval, newval|
|
93
|
-
# Options that can occur more than once, such as 'filter' or
|
94
|
-
# 'metadata' are merged, not replaced like options that can occur
|
95
|
-
# only once, such as 'toc' or 'from'
|
96
|
-
if oldval.is_a? Array
|
97
|
-
oldval + newval
|
98
|
-
else
|
99
|
-
newval
|
100
|
-
end
|
63
|
+
@errors.push IOError.new(:file_does_not_exist, nil, @src) unless File.exist? @src
|
64
|
+
@errors.push IOError.new(:file_is_not_a_file, nil, @src) unless File.file? @src
|
65
|
+
@errors.push IOError.new(:file_is_not_readable, nil, @src) unless File.readable? @src
|
101
66
|
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Run setup scripts
|
105
|
-
setup template
|
106
|
-
|
107
|
-
# Read in the file to convert
|
108
|
-
input = File.read @src
|
109
|
-
|
110
|
-
# Run the default preprocessors to mix-in information about the file
|
111
|
-
# that is being converted and mix-in the template's metadata section as
|
112
|
-
# well
|
113
|
-
input = FileInfoPreprocessor.run input, @src, pandoc_options
|
114
|
-
input = MetadataPreprocessor.run input, template['metadata'] if template.has_key? 'metadata' and not template['metadata'].empty?
|
115
|
-
|
116
|
-
# Convert the file by preprocessing it, run pandoc on it, and
|
117
|
-
# postprocessing the output
|
118
|
-
input = preprocess input, template
|
119
|
-
input = pandoc input, pandoc_options, File.dirname(@src)
|
120
|
-
output = postprocess input, template
|
121
|
-
|
122
|
-
# Write out the results of the conversion process to file.
|
123
|
-
if @dst.to_s.empty? and metadata.pandoc_options.has_key? 'output'
|
124
|
-
@dst = metadata.pandoc_options['output']
|
125
|
-
end
|
126
|
-
|
127
|
-
begin
|
128
|
-
unless OUTPUT_FORMATS.include? pandoc_options["to"] then
|
129
|
-
File.open(@dst, 'w') do |file|
|
130
|
-
raise IOError.new(:file_is_not_a_file, nil, @dst) unless File.file? @dst
|
131
|
-
raise IOError.new(:file_is_not_writable, nil, @dst) unless File.writable? @dst
|
132
|
-
file << output
|
133
|
-
end
|
134
|
-
end
|
135
|
-
rescue StandardError => e
|
136
|
-
raise IOError.new(:error_writing_file, e, @dst)
|
137
|
-
end
|
138
|
-
|
139
|
-
# run cleanup scripts
|
140
|
-
cleanup template
|
141
|
-
end
|
142
|
-
|
143
|
-
# TODO: update this list
|
144
|
-
PANDOC_OPTIONS_WITH_PATH = [
|
145
|
-
'filter',
|
146
|
-
'template',
|
147
|
-
'css',
|
148
|
-
'include-in-header',
|
149
|
-
'include-before-body',
|
150
|
-
'include-after-body',
|
151
|
-
'reference-odt',
|
152
|
-
'reference-docx',
|
153
|
-
'epub-stylesheet',
|
154
|
-
'epub-cover-image',
|
155
|
-
'epub-metadata',
|
156
|
-
'epub-embed-font',
|
157
|
-
'epub-subdirectory',
|
158
|
-
'bibliography',
|
159
|
-
'csl',
|
160
|
-
'syntax-definition',
|
161
|
-
'reference-doc',
|
162
|
-
'lua-filter',
|
163
|
-
'extract-media',
|
164
|
-
'resource-path',
|
165
|
-
'citation-abbreviations',
|
166
|
-
'abbreviations',
|
167
|
-
'log'
|
168
|
-
]
|
169
|
-
|
170
|
-
def pandoc(input, options, src_dir)
|
171
|
-
converter = Paru::Pandoc.new
|
172
|
-
options.each do |option, value|
|
173
|
-
# Pandoc multi-word options can have the multiple words separated by
|
174
|
-
# both underscore (_) and dash (-).
|
175
|
-
option= option.gsub "-", "_"
|
176
|
-
|
177
|
-
if PANDOC_OPTIONS_WITH_PATH.include? option
|
178
|
-
if value.is_a? Array
|
179
|
-
value = value.map {|v| @config.update_path(v, src_dir, option == "filter")}
|
180
|
-
else
|
181
|
-
value = @config.update_path(value, src_dir, option == "filter")
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
# There is no "pdf" output format; change it to latex but keep the
|
186
|
-
# extension.
|
187
|
-
value = "latex" if option == "to" and value == "pdf"
|
188
|
-
|
189
|
-
converter.send option, value unless option == 'output'
|
190
|
-
# don't let pandoc write the output to enable postprocessing
|
191
|
-
end
|
192
|
-
|
193
|
-
converter.send "output", @dst if OUTPUT_FORMATS.include? options["to"]
|
194
|
-
|
195
|
-
begin
|
196
|
-
puts converter.to_command if debug?
|
197
|
-
converter << input
|
198
|
-
rescue Paru::Error => e
|
199
|
-
raise PandocError.new(:error_running_pandoc, e, input_document)
|
200
|
-
end
|
201
|
-
end
|
202
67
|
|
203
|
-
|
204
|
-
|
205
|
-
|
68
|
+
# Execute this ConvertFileCommand
|
69
|
+
def run
|
70
|
+
convert_file
|
71
|
+
end
|
206
72
|
|
207
|
-
|
208
|
-
|
209
|
-
|
73
|
+
# Create a string representation of this ConvertFileCommand
|
74
|
+
#
|
75
|
+
# @return [String]
|
76
|
+
def to_s
|
77
|
+
"convert #{File.basename @src} #{"-> #{File.basename @dst}" unless @dst.nil?}"
|
78
|
+
end
|
210
79
|
|
211
|
-
|
212
|
-
|
213
|
-
|
80
|
+
private
|
81
|
+
|
82
|
+
def convert_file
|
83
|
+
metadata = PandocMetadata.load_file @src
|
84
|
+
pandoc_options = metadata.pandoc_options || {}
|
85
|
+
template = {}
|
86
|
+
|
87
|
+
# Determine the actual options and settings to use when converting this
|
88
|
+
# file.
|
89
|
+
if not @template_name.nil? and not @template_name.empty?
|
90
|
+
raise ConfigurationError.new(:no_such_template, nil, @template_name) unless @config.has_template? @template_name
|
91
|
+
template = @config.get_template @template_name
|
92
|
+
|
93
|
+
pandoc_options = (template['pandoc'] || {}).merge(pandoc_options) do |key, oldval, newval|
|
94
|
+
# Options that can occur more than once, such as 'filter' or
|
95
|
+
# 'metadata' are merged, not replaced like options that can occur
|
96
|
+
# only once, such as 'toc' or 'from'
|
97
|
+
if oldval.is_a? Array
|
98
|
+
oldval + newval
|
99
|
+
else
|
100
|
+
newval
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Run setup scripts
|
106
|
+
setup template
|
107
|
+
|
108
|
+
# Read in the file to convert
|
109
|
+
input = File.read @src
|
110
|
+
|
111
|
+
# Run the default preprocessors to mix-in information about the file
|
112
|
+
# that is being converted and mix-in the template's metadata section as
|
113
|
+
# well
|
114
|
+
input = FileInfoPreprocessor.run input, @src, pandoc_options
|
115
|
+
input = MetadataPreprocessor.run input, template['metadata'] if template.has_key? 'metadata' and not template['metadata'].empty?
|
116
|
+
|
117
|
+
# Convert the file by preprocessing it, run pandoc on it, and
|
118
|
+
# postprocessing the output
|
119
|
+
input = preprocess input, template
|
120
|
+
input = pandoc input, pandoc_options, File.dirname(@src)
|
121
|
+
output = postprocess input, template
|
122
|
+
|
123
|
+
# Write out the results of the conversion process to file.
|
124
|
+
if @dst.to_s.empty? and metadata.pandoc_options.has_key? 'output'
|
125
|
+
@dst = metadata.pandoc_options['output']
|
126
|
+
end
|
127
|
+
|
128
|
+
begin
|
129
|
+
unless OUTPUT_FORMATS.include? pandoc_options["to"] then
|
130
|
+
File.open(@dst, 'w') do |file|
|
131
|
+
raise IOError.new(:file_is_not_a_file, nil, @dst) unless File.file? @dst
|
132
|
+
raise IOError.new(:file_is_not_writable, nil, @dst) unless File.writable? @dst
|
133
|
+
file << output
|
134
|
+
end
|
135
|
+
end
|
136
|
+
rescue StandardError => e
|
137
|
+
raise IOError.new(:error_writing_file, e, @dst)
|
138
|
+
end
|
139
|
+
|
140
|
+
# run cleanup scripts
|
141
|
+
cleanup template
|
142
|
+
end
|
214
143
|
|
215
|
-
|
216
|
-
|
217
|
-
|
144
|
+
# TODO: update this list
|
145
|
+
PANDOC_OPTIONS_WITH_PATH = [
|
146
|
+
'filter',
|
147
|
+
'template',
|
148
|
+
'css',
|
149
|
+
'include-in-header',
|
150
|
+
'include-before-body',
|
151
|
+
'include-after-body',
|
152
|
+
'reference-odt',
|
153
|
+
'reference-docx',
|
154
|
+
'epub-stylesheet',
|
155
|
+
'epub-cover-image',
|
156
|
+
'epub-metadata',
|
157
|
+
'epub-embed-font',
|
158
|
+
'epub-subdirectory',
|
159
|
+
'bibliography',
|
160
|
+
'csl',
|
161
|
+
'syntax-definition',
|
162
|
+
'reference-doc',
|
163
|
+
'lua-filter',
|
164
|
+
'extract-media',
|
165
|
+
'resource-path',
|
166
|
+
'citation-abbreviations',
|
167
|
+
'abbreviations',
|
168
|
+
'log'
|
169
|
+
]
|
170
|
+
|
171
|
+
def pandoc(input, options, src_dir)
|
172
|
+
converter = Paru::Pandoc.new
|
173
|
+
options.each do |option, value|
|
174
|
+
# Pandoc multi-word options can have the multiple words separated by
|
175
|
+
# both underscore (_) and dash (-).
|
176
|
+
option= option.gsub "-", "_"
|
177
|
+
|
178
|
+
if PANDOC_OPTIONS_WITH_PATH.include? option
|
179
|
+
if value.is_a? Array
|
180
|
+
value = value.map {|v| @config.update_path(v, src_dir, option == "filter")}
|
181
|
+
else
|
182
|
+
value = @config.update_path(value, src_dir, option == "filter")
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# There is no "pdf" output format; change it to latex but keep the
|
187
|
+
# extension.
|
188
|
+
value = "latex" if option == "to" and value == "pdf"
|
189
|
+
|
190
|
+
converter.send option, value unless option == 'output'
|
191
|
+
# don't let pandoc write the output to enable postprocessing
|
192
|
+
end
|
193
|
+
|
194
|
+
converter.send "output", @dst if OUTPUT_FORMATS.include? options["to"]
|
195
|
+
|
196
|
+
begin
|
197
|
+
puts converter.to_command if debug?
|
198
|
+
converter << input
|
199
|
+
rescue Paru::Error => e
|
200
|
+
raise PandocError.new(:error_running_pandoc, e, input_document)
|
201
|
+
end
|
202
|
+
end
|
218
203
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
if config.has_key? type then
|
223
|
-
processors = config[type]
|
224
|
-
output = input
|
225
|
-
processors.each do |processor|
|
226
|
-
script = @config.update_path(processor, File.dirname(@src))
|
204
|
+
def preprocess(input, config = {})
|
205
|
+
process input, 'preprocessors', config
|
206
|
+
end
|
227
207
|
|
228
|
-
|
229
|
-
|
208
|
+
def postprocess(input, config = {})
|
209
|
+
process input, 'postprocessors', config
|
210
|
+
end
|
230
211
|
|
231
|
-
|
232
|
-
|
212
|
+
def setup(config = {})
|
213
|
+
process "", 'setup', config
|
214
|
+
end
|
233
215
|
|
234
|
-
|
216
|
+
def cleanup(config = {})
|
217
|
+
process "", 'cleanup', config
|
218
|
+
end
|
235
219
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
220
|
+
# Run the input string through a list of filters called processors. There
|
221
|
+
# are to types: preprocessors and postprocessors
|
222
|
+
def process(input, type, config = {})
|
223
|
+
if config.has_key? type then
|
224
|
+
processors = config[type]
|
225
|
+
output = input
|
226
|
+
processors.each do |processor|
|
227
|
+
script = @config.update_path(processor, File.dirname(@src))
|
228
|
+
|
229
|
+
command, *parameters = script.shellsplit # split on spaces unless it is preceded by a backslash
|
230
|
+
|
231
|
+
if not File.exists? command
|
232
|
+
command = Configuration.which(command)
|
233
|
+
script = "#{command} #{parameters.join(' ')}"
|
234
|
+
|
235
|
+
raise ProcessorError.new(:script_does_not_exist, nil, command) if command.nil?
|
236
|
+
end
|
237
|
+
|
238
|
+
raise ProcessorError.new(:script_is_not_executable, nil, command) unless File.executable? command
|
239
|
+
|
240
|
+
begin
|
241
|
+
output = Processor.run(script, output)
|
242
|
+
rescue StandardError => e
|
243
|
+
ProcessorError.new(:error_processing_script, e, [script, @src])
|
244
|
+
end
|
245
|
+
end
|
246
|
+
output
|
247
|
+
else
|
248
|
+
input
|
249
|
+
end
|
241
250
|
end
|
242
|
-
output
|
243
|
-
else
|
244
|
-
input
|
245
|
-
end
|
246
251
|
end
|
247
|
-
end
|
248
252
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pandocomatic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.4.
|
4
|
+
version: 0.1.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Huub de Beer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: paru
|