pandocomatic 0.1.4.10 → 0.1.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pandocomatic/command/command.rb +0 -1
- data/lib/pandocomatic/command/convert_dir_command.rb +26 -0
- data/lib/pandocomatic/command/convert_file_command.rb +12 -0
- data/lib/pandocomatic/command/convert_file_multiple_command.rb +15 -19
- data/lib/pandocomatic/command/convert_list_command.rb +23 -0
- data/lib/pandocomatic/command/copy_file_command.rb +12 -0
- data/lib/pandocomatic/command/create_link_command.rb +26 -0
- data/lib/pandocomatic/command/skip_command.rb +22 -0
- data/lib/pandocomatic/configuration.rb +11 -3
- data/lib/pandocomatic/error/cli_error.rb +2 -0
- data/lib/pandocomatic/error/configuration_error.rb +2 -0
- data/lib/pandocomatic/error/io_error.rb +2 -0
- data/lib/pandocomatic/error/pandoc_error.rb +3 -1
- data/lib/pandocomatic/error/pandocomatic_error.rb +27 -0
- data/lib/pandocomatic/error/processor_error.rb +2 -0
- data/lib/pandocomatic/pandocomatic.rb +160 -152
- data/lib/pandocomatic/printer/command_printer.rb +2 -0
- data/lib/pandocomatic/printer/configuration_errors_printer.rb +2 -0
- data/lib/pandocomatic/printer/error_printer.rb +3 -0
- data/lib/pandocomatic/printer/finish_printer.rb +6 -1
- data/lib/pandocomatic/printer/help_printer.rb +2 -0
- data/lib/pandocomatic/printer/printer.rb +29 -17
- data/lib/pandocomatic/printer/summary_printer.rb +29 -15
- data/lib/pandocomatic/printer/version_printer.rb +2 -0
- data/lib/pandocomatic/printer/warning_printer.rb +15 -10
- data/lib/pandocomatic/processor.rb +19 -7
- data/lib/pandocomatic/processors/fileinfo_preprocessor.rb +26 -24
- data/lib/pandocomatic/processors/metadata_preprocessor.rb +17 -16
- data/lib/pandocomatic/warning.rb +19 -12
- metadata +3 -3
@@ -18,9 +18,11 @@
|
|
18
18
|
#++
|
19
19
|
module Pandocomatic
|
20
20
|
require_relative './pandocomatic_error.rb'
|
21
|
-
|
21
|
+
|
22
|
+
# An error when running pandoc
|
22
23
|
class PandocError < PandocomaticError
|
23
24
|
|
25
|
+
# The template to use to print this PandocError
|
24
26
|
def template()
|
25
27
|
'pandoc_error.txt'
|
26
28
|
end
|
@@ -19,8 +19,25 @@
|
|
19
19
|
module Pandocomatic
|
20
20
|
require_relative '../printer/error_printer.rb'
|
21
21
|
|
22
|
+
# General pandocomatic error
|
23
|
+
#
|
24
|
+
# @!attribute type
|
25
|
+
# @return [Symbol] type of error
|
26
|
+
#
|
27
|
+
# @!attribute error
|
28
|
+
# @return [Error] the underlying error, if any
|
29
|
+
#
|
30
|
+
# @!attribute data
|
31
|
+
# @return [Object] attached data, if any
|
22
32
|
class PandocomaticError < StandardError
|
23
33
|
attr_reader :type, :error, :data
|
34
|
+
|
35
|
+
# Create a new PandocomaticError
|
36
|
+
#
|
37
|
+
# @param type [Symbol = :unknown] the type of error, defaults to :unknown
|
38
|
+
# @param error [Error = nil] the underlying error, optional
|
39
|
+
# @param data [Object = nil] extra information attached to this
|
40
|
+
# PandocomaticError, if any; optional
|
24
41
|
def initialize(type = :unknown, error = nil, data = nil)
|
25
42
|
super type.to_s.gsub("_", " ").capitalize
|
26
43
|
@type = type
|
@@ -28,18 +45,28 @@ module Pandocomatic
|
|
28
45
|
@data = data
|
29
46
|
end
|
30
47
|
|
48
|
+
# Has this PandocomaticError an underlying error?
|
49
|
+
#
|
50
|
+
# @return [Boolean]
|
31
51
|
def has_error?()
|
32
52
|
not @error.nil?
|
33
53
|
end
|
34
54
|
|
55
|
+
# Has this PandocomaticError extra information associated to it?
|
56
|
+
#
|
57
|
+
# @return [Boolean]
|
35
58
|
def has_data?()
|
36
59
|
not @data.nil?
|
37
60
|
end
|
38
61
|
|
62
|
+
# Print this error.
|
39
63
|
def print()
|
40
64
|
ErrorPrinter.new(self).print
|
41
65
|
end
|
42
66
|
|
67
|
+
# Show this error
|
68
|
+
#
|
69
|
+
# @return [String] a string representation of this PandocomaticError
|
43
70
|
def show()
|
44
71
|
ErrorPrinter.new(self).to_s
|
45
72
|
end
|
@@ -19,8 +19,10 @@
|
|
19
19
|
module Pandocomatic
|
20
20
|
require_relative './pandocomatic_error.rb'
|
21
21
|
|
22
|
+
# An error while running a processor
|
22
23
|
class ProcessorError < PandocomaticError
|
23
24
|
|
25
|
+
# The template to use when printing this ProcessorError
|
24
26
|
def template()
|
25
27
|
'processor_error.txt'
|
26
28
|
end
|
@@ -17,168 +17,176 @@
|
|
17
17
|
# with pandocomatic. If not, see <http://www.gnu.org/licenses/>.
|
18
18
|
#++
|
19
19
|
module Pandocomatic
|
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
|
-
|
20
|
+
Encoding.default_external = Encoding::UTF_8 #ensure unicode encoding
|
21
|
+
Encoding.default_internal = Encoding::UTF_8
|
22
|
+
|
23
|
+
require 'paru'
|
24
|
+
|
25
|
+
require_relative './error/pandocomatic_error.rb'
|
26
|
+
require_relative './error/pandoc_error.rb'
|
27
|
+
require_relative './error/configuration_error.rb'
|
28
|
+
|
29
|
+
require_relative './cli.rb'
|
30
|
+
|
31
|
+
require_relative './configuration.rb'
|
32
|
+
|
33
|
+
require_relative './printer/help_printer.rb'
|
34
|
+
require_relative './printer/version_printer.rb'
|
35
|
+
require_relative './printer/error_printer.rb'
|
36
|
+
require_relative './printer/configuration_errors_printer.rb'
|
37
|
+
require_relative './printer/finish_printer.rb'
|
38
|
+
require_relative './printer/summary_printer.rb'
|
39
|
+
|
40
|
+
require_relative './command/command.rb'
|
41
|
+
require_relative './command/convert_dir_command.rb'
|
42
|
+
require_relative './command/convert_list_command.rb'
|
43
|
+
require_relative './command/convert_file_command.rb'
|
44
|
+
require_relative './command/convert_file_multiple_command.rb'
|
45
|
+
|
46
|
+
# The Pandocomatic class controlls the pandocomatic conversion process
|
47
|
+
class Pandocomatic
|
48
|
+
|
49
|
+
# Pandocomatic's current version
|
50
|
+
VERSION = [0, 1, 4, 11]
|
51
|
+
|
52
|
+
# Pandocomatic's default configuration file
|
53
|
+
CONFIG_FILE = 'pandocomatic.yaml'
|
54
|
+
|
55
|
+
# Run pandocomatic given options
|
56
|
+
#
|
57
|
+
# @param args [String[]] list of options to configure pandocomatic
|
58
|
+
def self.run(args)
|
59
|
+
begin
|
60
|
+
start_time = Time.now
|
61
|
+
options = CLI.parse args
|
62
|
+
|
63
|
+
if options[:version_given]
|
64
|
+
# The version option has precedence over all other options; if
|
65
|
+
# given, the version is printed
|
66
|
+
VersionPrinter.new(VERSION).print
|
67
|
+
elsif options[:help_given]
|
68
|
+
# The help option has precedence over all other options except the
|
69
|
+
# version option. If given, the help is printed.
|
70
|
+
HelpPrinter.new().print
|
71
|
+
else
|
72
|
+
# Run the pandocomatic converter configured according to the options
|
73
|
+
# given.
|
74
|
+
input = options[:input]
|
75
|
+
output = options[:output]
|
76
|
+
configuration = configure options
|
77
|
+
|
78
|
+
# Extend the command classes by setting the source tree root
|
79
|
+
# directory, and the options quiet and dry-run, which are used when
|
80
|
+
# executing a command: if dry-run the command is not actually
|
81
|
+
# executed and if quiet the command is not printed to STDOUT
|
82
|
+
src_root = File.absolute_path input
|
83
|
+
dry_run = if options[:dry_run_given] then options[:dry_run] else false end
|
84
|
+
quiet = if options[:quiet_given] then options[:quiet] else false end
|
85
|
+
debug = if options[:debug_given] and not quiet then options[:debug] else false end
|
86
|
+
modified_only = if options[:modified_only_given] then options[:modified_only_given] else false end
|
87
|
+
|
88
|
+
Command.reset(src_root, dry_run, quiet, debug, modified_only)
|
89
|
+
|
90
|
+
# Pandocomatic has two modes: converting a directory tree or
|
91
|
+
# converting a single file. The mode is selected by the input.
|
92
|
+
if File.directory? input
|
93
|
+
command = ConvertDirCommand.new(configuration, input, output)
|
94
|
+
else
|
95
|
+
destination = if output.nil? or output.empty? then File.basename input else output end
|
96
|
+
|
97
|
+
command = ConvertFileMultipleCommand.new(configuration, input, destination)
|
98
|
+
command.make_quiet unless command.subcommands.size > 1
|
99
|
+
end
|
100
|
+
|
101
|
+
# Notify the user about all configuration errors collected when
|
102
|
+
# determining the commands to run to perform this pandocomatic
|
103
|
+
# conversion.
|
104
|
+
if command.all_errors.size > 0
|
105
|
+
ConfigurationErrorsPrinter.new(command.all_errors).print
|
106
|
+
exit
|
107
|
+
end
|
108
|
+
|
109
|
+
# Pandocomatic is successfully configured: running the
|
110
|
+
# actual conversion now.
|
111
|
+
SummaryPrinter.new(command, input, output).print unless quiet or not command.directory?
|
112
|
+
|
113
|
+
# Depending on the options dry-run and quiet, the command.execute
|
114
|
+
# method will actually performing the commands (dry-run = false) and
|
115
|
+
# print the command to STDOUT (quiet = false)
|
116
|
+
command.execute()
|
117
|
+
|
118
|
+
FinishPrinter.new(command, input, output, start_time).print unless quiet
|
119
|
+
end
|
120
|
+
rescue PandocomaticError => e
|
121
|
+
# Report the error and break off the conversion process.
|
122
|
+
ErrorPrinter.new(e).print
|
123
|
+
rescue StandardError => e
|
124
|
+
# An unexpected error has occurred; break off the program drastically
|
125
|
+
# for now
|
126
|
+
raise e
|
127
|
+
end
|
111
128
|
end
|
112
|
-
rescue PandocomaticError => e
|
113
|
-
# Report the error and break off the conversion process.
|
114
|
-
ErrorPrinter.new(e).print
|
115
|
-
rescue StandardError => e
|
116
|
-
# An unexpected error has occurred; break off the program drastically
|
117
|
-
# for now
|
118
|
-
raise e
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
private
|
123
129
|
|
124
|
-
|
125
|
-
config_file = ''
|
130
|
+
private
|
126
131
|
|
127
|
-
|
128
|
-
|
129
|
-
elsif Dir.entries(data_dir).include? CONFIG_FILE
|
130
|
-
config_file = File.join(data_dir, CONFIG_FILE)
|
131
|
-
elsif Dir.entries(Dir.pwd()).include? CONFIG_FILE
|
132
|
-
config_file = File.join(Dir.pwd(), CONFIG_FILE)
|
133
|
-
else
|
134
|
-
# Fall back to default configuration file distributed with
|
135
|
-
# pandocomatic
|
136
|
-
config_file = File.join(__dir__, 'default_configuration.yaml')
|
137
|
-
end
|
132
|
+
def self.determine_config_file(options, data_dir = Dir.pwd)
|
133
|
+
config_file = ''
|
138
134
|
|
139
|
-
|
135
|
+
if options[:config_given]
|
136
|
+
config_file = options[:config]
|
137
|
+
elsif Dir.entries(data_dir).include? CONFIG_FILE
|
138
|
+
config_file = File.join(data_dir, CONFIG_FILE)
|
139
|
+
elsif Dir.entries(Dir.pwd()).include? CONFIG_FILE
|
140
|
+
config_file = File.join(Dir.pwd(), CONFIG_FILE)
|
141
|
+
else
|
142
|
+
# Fall back to default configuration file distributed with
|
143
|
+
# pandocomatic
|
144
|
+
config_file = File.join(__dir__, 'default_configuration.yaml')
|
145
|
+
end
|
140
146
|
|
141
|
-
|
142
|
-
raise ConfigurationError.new(:config_file_is_not_a_file, nil, path) unless File.file? path
|
143
|
-
raise ConfigurationError.new(:config_file_is_not_readable, nil, path) unless File.readable? path
|
147
|
+
path = File.absolute_path config_file
|
144
148
|
|
145
|
-
|
146
|
-
|
149
|
+
raise ConfigurationError.new(:config_file_does_not_exist, nil, path) unless File.exist? path
|
150
|
+
raise ConfigurationError.new(:config_file_is_not_a_file, nil, path) unless File.file? path
|
151
|
+
raise ConfigurationError.new(:config_file_is_not_readable, nil, path) unless File.readable? path
|
147
152
|
|
148
|
-
|
149
|
-
data_dir = ''
|
150
|
-
|
151
|
-
if options[:data_dir_given]
|
152
|
-
data_dir = options[:data_dir]
|
153
|
-
else
|
154
|
-
# No data-dir option given: try to find the default one from pandoc
|
155
|
-
begin
|
156
|
-
data_dir = Paru::Pandoc.info()[:data_dir]
|
157
|
-
rescue Paru::Error => e
|
158
|
-
# If pandoc cannot be run, continuing probably does not work out
|
159
|
-
# anyway, so raise pandoc error
|
160
|
-
raise PandocError.new(:error_running_pandoc, e, data_dir)
|
161
|
-
rescue StandardError => e
|
162
|
-
# Ignore error and use the current working directory as default working directory
|
163
|
-
data_dir = Dir.pwd
|
153
|
+
path
|
164
154
|
end
|
165
|
-
end
|
166
|
-
|
167
|
-
# check if data directory does exist and is readable
|
168
|
-
path = File.absolute_path data_dir
|
169
155
|
|
170
|
-
|
171
|
-
|
172
|
-
|
156
|
+
def self.determine_data_dir(options)
|
157
|
+
data_dir = ''
|
158
|
+
|
159
|
+
if options[:data_dir_given]
|
160
|
+
data_dir = options[:data_dir]
|
161
|
+
else
|
162
|
+
# No data-dir option given: try to find the default one from pandoc
|
163
|
+
begin
|
164
|
+
data_dir = Paru::Pandoc.info()[:data_dir]
|
165
|
+
rescue Paru::Error => e
|
166
|
+
# If pandoc cannot be run, continuing probably does not work out
|
167
|
+
# anyway, so raise pandoc error
|
168
|
+
raise PandocError.new(:error_running_pandoc, e, data_dir)
|
169
|
+
rescue StandardError => e
|
170
|
+
# Ignore error and use the current working directory as default working directory
|
171
|
+
data_dir = Dir.pwd
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# check if data directory does exist and is readable
|
176
|
+
path = File.absolute_path data_dir
|
177
|
+
|
178
|
+
raise ConfigurationError.new(:data_dir_does_not_exist, nil, path) unless File.exist? path
|
179
|
+
raise ConfigurationError.new(:data_dir_is_not_a_directory, nil, path) unless File.directory? path
|
180
|
+
raise ConfigurationError.new(:data_dir_is_not_readable, nil, path) unless File.readable? path
|
181
|
+
|
182
|
+
path
|
183
|
+
end
|
173
184
|
|
174
|
-
|
175
|
-
|
185
|
+
def self.configure(options)
|
186
|
+
data_dir = determine_data_dir options
|
187
|
+
config_file = determine_config_file options, data_dir
|
188
|
+
Configuration.new options, data_dir, config_file
|
189
|
+
end
|
176
190
|
|
177
|
-
def self.configure(options)
|
178
|
-
data_dir = determine_data_dir options
|
179
|
-
config_file = determine_config_file options, data_dir
|
180
|
-
Configuration.new options, data_dir, config_file
|
181
191
|
end
|
182
|
-
|
183
|
-
end
|
184
192
|
end
|
@@ -19,7 +19,9 @@
|
|
19
19
|
module Pandocomatic
|
20
20
|
require_relative './printer.rb'
|
21
21
|
|
22
|
+
# Printer for ConfigurationErrors in non-quiet mode
|
22
23
|
class ConfigurationErrorsPrinter < Printer
|
24
|
+
# Create a new ConfigurationErrorsPrinter
|
23
25
|
def initialize(errors)
|
24
26
|
super 'configuration_errors.txt'
|
25
27
|
@errors = errors
|
@@ -19,13 +19,16 @@
|
|
19
19
|
module Pandocomatic
|
20
20
|
require_relative './printer.rb'
|
21
21
|
|
22
|
+
# Printer for Errors in non-quiet mode
|
22
23
|
class ErrorPrinter < Printer
|
24
|
+
# Create a new ErrorPrinter
|
23
25
|
def initialize(error)
|
24
26
|
template = if error.respond_to? :template then error.template else 'error.txt' end
|
25
27
|
super template
|
26
28
|
@error = error
|
27
29
|
end
|
28
30
|
|
31
|
+
# Print an Error to STDERR rather than STDOUT
|
29
32
|
def print
|
30
33
|
warn to_s
|
31
34
|
end
|