pandocomatic 0.2.7.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pandocomatic/cli.rb +81 -64
- data/lib/pandocomatic/command/command.rb +37 -35
- data/lib/pandocomatic/command/convert_dir_command.rb +44 -46
- data/lib/pandocomatic/command/convert_file_command.rb +314 -286
- data/lib/pandocomatic/command/convert_file_multiple_command.rb +56 -53
- data/lib/pandocomatic/command/convert_list_command.rb +31 -34
- data/lib/pandocomatic/command/copy_file_command.rb +14 -15
- data/lib/pandocomatic/command/create_link_command.rb +24 -27
- data/lib/pandocomatic/command/skip_command.rb +12 -15
- data/lib/pandocomatic/configuration.rb +682 -867
- data/lib/pandocomatic/default_configuration.yaml +4 -0
- data/lib/pandocomatic/error/cli_error.rb +30 -26
- data/lib/pandocomatic/error/configuration_error.rb +10 -9
- data/lib/pandocomatic/error/io_error.rb +13 -13
- data/lib/pandocomatic/error/pandoc_error.rb +10 -9
- data/lib/pandocomatic/error/pandocomatic_error.rb +15 -14
- data/lib/pandocomatic/error/processor_error.rb +9 -9
- data/lib/pandocomatic/error/template_error.rb +50 -0
- data/lib/pandocomatic/input.rb +53 -54
- data/lib/pandocomatic/multiple_files_input.rb +79 -72
- data/lib/pandocomatic/output.rb +29 -0
- data/lib/pandocomatic/pandoc_metadata.rb +193 -181
- data/lib/pandocomatic/pandocomatic.rb +101 -97
- data/lib/pandocomatic/pandocomatic_yaml.rb +69 -0
- data/lib/pandocomatic/path.rb +171 -0
- data/lib/pandocomatic/printer/command_printer.rb +7 -5
- data/lib/pandocomatic/printer/configuration_errors_printer.rb +7 -6
- data/lib/pandocomatic/printer/error_printer.rb +12 -7
- data/lib/pandocomatic/printer/finish_printer.rb +11 -10
- data/lib/pandocomatic/printer/help_printer.rb +8 -6
- data/lib/pandocomatic/printer/printer.rb +34 -34
- data/lib/pandocomatic/printer/summary_printer.rb +39 -33
- data/lib/pandocomatic/printer/version_printer.rb +8 -8
- data/lib/pandocomatic/printer/views/cli_error.txt +5 -0
- data/lib/pandocomatic/printer/views/configuration_error.txt +2 -1
- data/lib/pandocomatic/printer/views/error.txt +1 -1
- data/lib/pandocomatic/printer/views/finish.txt +1 -1
- data/lib/pandocomatic/printer/views/help.txt +27 -15
- data/lib/pandocomatic/printer/views/summary.txt +7 -1
- data/lib/pandocomatic/printer/views/template_error.txt +1 -0
- data/lib/pandocomatic/printer/views/version.txt +3 -3
- data/lib/pandocomatic/printer/views/warning.txt +1 -1
- data/lib/pandocomatic/printer/warning_printer.rb +21 -19
- data/lib/pandocomatic/processor.rb +28 -28
- data/lib/pandocomatic/processors/fileinfo_preprocessor.rb +35 -30
- data/lib/pandocomatic/processors/metadata_preprocessor.rb +23 -22
- data/lib/pandocomatic/template.rb +244 -0
- data/lib/pandocomatic/warning.rb +24 -25
- metadata +32 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8185106783d727335ad8c571251e9352c99ae9c135c073db975d42bed3b567f
|
4
|
+
data.tar.gz: 99b41c4033cb7e39c060aa4f3affae90f4c50c9ac65d0bd2bf0fb823cd897345
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: feed8b1cc30bf78341c357dcf44ada7f9f88ef4f74b4ab847a4e592d1ddfddcb3754d0a38b050e8c6b282a0ee3122dc0a5a3bf5d5157f58c9331121dc6cc0d21
|
7
|
+
data.tar.gz: 9ea7f3801f2bd93a3fadf5399d0597a046e6fecfb46f57da8f8eccc3e6ba2a5bddc7c706a6a192f1b21f10ada40882c331e68a3bdfa475f83595b79c251cee32
|
data/lib/pandocomatic/cli.rb
CHANGED
@@ -1,37 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#--
|
2
|
-
# Copyright 2017-
|
3
|
-
#
|
4
|
+
# Copyright 2017-2022, Huub de Beer <huub@heerdebeer.org>
|
5
|
+
#
|
4
6
|
# This file is part of pandocomatic.
|
5
|
-
#
|
7
|
+
#
|
6
8
|
# Pandocomatic is free software: you can redistribute it and/or modify
|
7
9
|
# it under the terms of the GNU General Public License as published by the
|
8
10
|
# Free Software Foundation, either version 3 of the License, or (at your
|
9
11
|
# option) any later version.
|
10
|
-
#
|
12
|
+
#
|
11
13
|
# Pandocomatic is distributed in the hope that it will be useful, but
|
12
14
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
13
15
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
14
16
|
# for more details.
|
15
|
-
#
|
17
|
+
#
|
16
18
|
# You should have received a copy of the GNU General Public License along
|
17
19
|
# with pandocomatic. If not, see <http://www.gnu.org/licenses/>.
|
18
20
|
#++
|
19
21
|
module Pandocomatic
|
20
22
|
require 'optimist'
|
21
23
|
|
22
|
-
require_relative './error/cli_error
|
23
|
-
require_relative './configuration
|
24
|
+
require_relative './error/cli_error'
|
25
|
+
require_relative './configuration'
|
24
26
|
|
25
27
|
##
|
26
28
|
# Command line options parser for pandocomatic using optimist.
|
27
29
|
#
|
28
30
|
class CLI
|
29
|
-
|
30
31
|
##
|
31
32
|
# Parse the arguments, returns a triplet with the global options, an
|
32
33
|
# optional subcommand, and the (optional) options for that subcommand.
|
33
34
|
#
|
34
|
-
# @param args [String, Array] A command
|
35
|
+
# @param args [String, Array] A command-line invocation string or a list of strings like ARGV
|
35
36
|
#
|
36
37
|
# @return [Configuration] The configuration for running pandocomatic given
|
37
38
|
# the command-line options.
|
@@ -39,39 +40,38 @@ module Pandocomatic
|
|
39
40
|
args = args.split if args.is_a? String
|
40
41
|
|
41
42
|
begin
|
42
|
-
parse_options args || Configuration.new({:
|
43
|
+
parse_options args || Configuration.new({ help: true, help_given: true })
|
43
44
|
rescue Optimist::CommandlineError => e
|
44
45
|
raise CLIError.new(:problematic_invocation, e, args)
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
|
49
|
+
# rubocop:disable Metrics
|
49
50
|
|
50
51
|
# Parse pandocomatic's global options.
|
51
52
|
#
|
52
53
|
# @return [Configuration]
|
53
|
-
def self.parse_options(args)
|
54
|
+
private_class_method def self.parse_options(args)
|
54
55
|
parser = Optimist::Parser.new do
|
55
|
-
# General options
|
56
|
-
opt :dry_run, 'Do a dry run', :
|
57
|
-
opt :
|
58
|
-
opt :debug, 'Debug mode, shows pandoc invocations', :
|
59
|
-
opt :modified_only, 'Modified files only', :
|
56
|
+
# General options
|
57
|
+
opt :dry_run, 'Do a dry run', short: '-y'
|
58
|
+
opt :verbose, 'Run verbosely', short: '-V'
|
59
|
+
opt :debug, 'Debug mode, shows pandoc invocations', short: '-b'
|
60
|
+
opt :modified_only, 'Modified files only', short: '-m'
|
60
61
|
|
61
62
|
# Configuration of the converter
|
62
|
-
opt :data_dir, 'Data dir', :
|
63
|
-
opt :config, 'Configuration file', :
|
63
|
+
opt :data_dir, 'Data dir', short: '-d', type: String
|
64
|
+
opt :config, 'Configuration file', short: '-c', type: String
|
65
|
+
opt :root_path, 'Root path', short: '-r', type: String
|
64
66
|
|
65
67
|
# What to convert and where to put it
|
66
|
-
opt :output, 'Output', :
|
67
|
-
opt :input, 'Input', :
|
68
|
-
|
69
|
-
# Experimental
|
70
|
-
opt :root_path, 'Root path', :short => '-r', :type => String
|
68
|
+
opt :output, 'Output', short: '-o', type: String
|
69
|
+
opt :input, 'Input', short: '-i', type: String, multi: true
|
70
|
+
opt :stdout, 'Output to standard out', short: '-s'
|
71
71
|
|
72
72
|
# Common
|
73
|
-
opt :show_version, 'Version', :
|
74
|
-
opt :show_help, 'Help', :
|
73
|
+
opt :show_version, 'Version', short: '-v', long: 'version'
|
74
|
+
opt :show_help, 'Help', short: '-h', long: 'help'
|
75
75
|
end
|
76
76
|
|
77
77
|
# All options should be parsed according to the specification given in the parser
|
@@ -80,55 +80,71 @@ module Pandocomatic
|
|
80
80
|
rescue Optimist::CommandlineError => e
|
81
81
|
raise CLIError.new(:problematic_invocation, e, args)
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
options = use_custom_version options
|
85
85
|
options = use_custom_help options
|
86
86
|
|
87
87
|
if options_need_to_be_validated? options
|
88
88
|
# if no input option is specified, all items following the last option
|
89
89
|
# are treated as input files.
|
90
|
-
if
|
90
|
+
if !(options[:input_given])
|
91
91
|
options[:input] = args
|
92
92
|
options[:input_given] = true
|
93
|
-
elsif
|
94
|
-
raise CLIError
|
93
|
+
elsif !args.empty?
|
94
|
+
raise CLIError, :no_mixed_inputs
|
95
95
|
end
|
96
96
|
|
97
97
|
# There should be an input specified
|
98
|
-
raise CLIError
|
98
|
+
raise CLIError, :no_input_given if options[:input].nil? || options[:input].empty?
|
99
99
|
|
100
100
|
# Support multiple input files for conversion
|
101
|
-
multiple_inputs =
|
101
|
+
multiple_inputs = options[:input].size > 1
|
102
102
|
|
103
103
|
# The input files or directories should exist
|
104
104
|
input = options[:input].map do |input_file|
|
105
105
|
raise CLIError.new(:input_does_not_exist, nil, input_file) unless File.exist? input_file
|
106
106
|
raise CLIError.new(:input_is_not_readable, nil, input_file) unless File.readable? input_file
|
107
|
-
|
107
|
+
|
108
108
|
# If there are multiple input files, these files cannot be directories
|
109
|
-
|
110
|
-
|
109
|
+
if multiple_inputs && File.directory?(input_file)
|
110
|
+
raise CLIError.new(:multiple_input_files_only, nil,
|
111
|
+
input_file)
|
112
|
+
end
|
113
|
+
|
111
114
|
File.absolute_path input_file
|
112
115
|
end
|
113
116
|
|
117
|
+
# You cannot use the --stdout option while converting directories
|
118
|
+
if options[:stdout_given] && File.directory?(input.first)
|
119
|
+
options[:stdout] = false
|
120
|
+
raise CLIError, :cannot_use_stdout_with_directory
|
121
|
+
end
|
122
|
+
|
114
123
|
if options[:output_given]
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
124
|
+
# You cannot use --stdout with --output
|
125
|
+
if options[:stdout_given]
|
126
|
+
options[:stdout] = false
|
127
|
+
raise CLIError, :cannot_use_both_output_and_stdout
|
128
|
+
else
|
129
|
+
output = File.absolute_path options[:output]
|
130
|
+
# Input and output should be both files or directories
|
131
|
+
match_file_types input.first, output
|
132
|
+
|
133
|
+
# The output, if it already exist, should be writable
|
134
|
+
unless (!File.exist? output) || File.writable?(output)
|
135
|
+
raise CLIError.new(:output_is_not_writable, nil,
|
136
|
+
output)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
elsif !multiple_inputs && File.directory?(input.first)
|
140
|
+
raise CLIError, :no_output_given
|
126
141
|
end
|
142
|
+
# If the input is a directory, an output directory should be
|
143
|
+
# specified as well. If the input is a file, the output could be
|
144
|
+
# specified in the input file, or STDOUT could be used.
|
127
145
|
|
128
146
|
# No check for root_path: a user can supply one that does not exists
|
129
147
|
# at this location and still work on the output location.
|
130
|
-
if options[:root_path_given]
|
131
|
-
end
|
132
148
|
|
133
149
|
# Data dir, if specified, should be an existing and readable directory
|
134
150
|
if options[:data_dir_given]
|
@@ -148,13 +164,15 @@ module Pandocomatic
|
|
148
164
|
raise CLIError.new(:config_file_is_not_a_file, nil, config) unless File.file? config
|
149
165
|
end
|
150
166
|
|
151
|
-
end
|
167
|
+
end
|
152
168
|
|
153
169
|
Configuration.new options, input
|
154
170
|
end
|
155
171
|
|
156
|
-
|
157
|
-
|
172
|
+
# rubocop:enable Metrics
|
173
|
+
|
174
|
+
private_class_method def self.options_need_to_be_validated?(options)
|
175
|
+
!(options[:version_given]) and !(options[:help_given])
|
158
176
|
end
|
159
177
|
|
160
178
|
#--
|
@@ -163,8 +181,8 @@ module Pandocomatic
|
|
163
181
|
# set, these are put in the options as "version" and "help"
|
164
182
|
# respectively.
|
165
183
|
#++
|
166
|
-
|
167
|
-
def self.use_custom_version
|
184
|
+
|
185
|
+
private_class_method def self.use_custom_version(options)
|
168
186
|
if options[:show_version]
|
169
187
|
options.delete :show_version
|
170
188
|
options.delete :show_version_given
|
@@ -174,29 +192,28 @@ module Pandocomatic
|
|
174
192
|
options
|
175
193
|
end
|
176
194
|
|
177
|
-
def self.use_custom_help
|
195
|
+
private_class_method def self.use_custom_help(options)
|
178
196
|
if options[:show_help]
|
179
197
|
options.delete :show_help
|
180
198
|
options.delete :show_help_given
|
181
199
|
options[:help] = true
|
182
200
|
options[:help_given] = true
|
183
201
|
end
|
184
|
-
options
|
202
|
+
options
|
185
203
|
end
|
186
204
|
|
187
205
|
# If output does not exist, the output can be
|
188
206
|
# created with the same type. If output does exist, however, it should
|
189
207
|
# have the same type as the input.
|
190
|
-
def self.matching_file_types?(input, output)
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
def self.match_file_types(input, output)
|
195
|
-
if not matching_file_types? input, output
|
196
|
-
raise CLIError.new(:output_is_not_a_file, nil, input) if File.file? input
|
197
|
-
raise CLIError.new(:output_is_not_a_directory, nil, input) if File.directory? input
|
198
|
-
end
|
208
|
+
private_class_method def self.matching_file_types?(input, output)
|
209
|
+
!File.exist?(output) or File.ftype(input) == File.ftype(output)
|
199
210
|
end
|
200
211
|
|
212
|
+
private_class_method def self.match_file_types(input, output)
|
213
|
+
return if matching_file_types? input, output
|
214
|
+
|
215
|
+
raise CLIError.new(:output_is_not_a_file, nil, input) if File.file? input
|
216
|
+
raise CLIError.new(:output_is_not_a_directory, nil, input) if File.directory? input
|
217
|
+
end
|
201
218
|
end
|
202
219
|
end
|
@@ -1,24 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#--
|
2
4
|
# Copyright 2017, Huub de Beer <Huub@heerdebeer.org>
|
3
|
-
#
|
5
|
+
#
|
4
6
|
# This file is part of pandocomatic.
|
5
|
-
#
|
7
|
+
#
|
6
8
|
# Pandocomatic is free software: you can redistribute it and/or modify
|
7
9
|
# it under the terms of the GNU General Public License as published by the
|
8
10
|
# Free Software Foundation, either version 3 of the License, or (at your
|
9
11
|
# option) any later version.
|
10
|
-
#
|
12
|
+
#
|
11
13
|
# Pandocomatic is distributed in the hope that it will be useful, but
|
12
14
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
13
15
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
14
16
|
# for more details.
|
15
|
-
#
|
17
|
+
#
|
16
18
|
# You should have received a copy of the GNU General Public License along
|
17
19
|
# with pandocomatic. If not, see <http://www.gnu.org/licenses/>.
|
18
20
|
#++
|
19
21
|
module Pandocomatic
|
20
|
-
|
21
|
-
require_relative '../printer/command_printer.rb'
|
22
|
+
require_relative '../printer/command_printer'
|
22
23
|
|
23
24
|
# Command is a base class of all actions pandocomatic executes while
|
24
25
|
# converting a file or a directory of files.
|
@@ -31,18 +32,19 @@ module Pandocomatic
|
|
31
32
|
# @return [Number] the index of this Command in the list with all commands
|
32
33
|
# to run when running pandocomatic.
|
33
34
|
class Command
|
34
|
-
|
35
35
|
attr_reader :errors, :index
|
36
36
|
|
37
|
+
# rubocop:disable Style/ClassVars
|
38
|
+
|
37
39
|
@@total = 0
|
38
40
|
@@dry_run = false
|
39
41
|
@@quiet = false
|
40
42
|
@@debug = false
|
41
|
-
@@src_root =
|
43
|
+
@@src_root = '.'
|
42
44
|
@@modified_only = false
|
43
45
|
|
44
46
|
# Create a new Command
|
45
|
-
def initialize
|
47
|
+
def initialize
|
46
48
|
@errors = []
|
47
49
|
@@total += 1
|
48
50
|
@index = @@total
|
@@ -63,35 +65,35 @@ module Pandocomatic
|
|
63
65
|
# Get the root directory of this Command's conversion process
|
64
66
|
#
|
65
67
|
# @return [String]
|
66
|
-
def src_root
|
68
|
+
def src_root
|
67
69
|
@@src_root
|
68
70
|
end
|
69
71
|
|
70
72
|
# Does this Command not actually execute?
|
71
73
|
#
|
72
74
|
# @return [Boolean]
|
73
|
-
def dry_run?
|
75
|
+
def dry_run?
|
74
76
|
@@dry_run
|
75
77
|
end
|
76
78
|
|
77
79
|
# Is this Command executed silently?
|
78
80
|
#
|
79
81
|
# @return [Boolean]
|
80
|
-
def quiet?
|
82
|
+
def quiet?
|
81
83
|
@@quiet
|
82
84
|
end
|
83
85
|
|
84
86
|
# Is this Command executed in debug mode?
|
85
87
|
#
|
86
88
|
# @return [Boolean]
|
87
|
-
def debug?
|
89
|
+
def debug?
|
88
90
|
@@debug
|
89
91
|
end
|
90
92
|
|
91
93
|
# Is this Command only executed on modified files?
|
92
94
|
#
|
93
95
|
# @return [Boolean]
|
94
|
-
def modified_only?
|
96
|
+
def modified_only?
|
95
97
|
@@modified_only
|
96
98
|
end
|
97
99
|
|
@@ -99,86 +101,87 @@ module Pandocomatic
|
|
99
101
|
# commands as well.
|
100
102
|
#
|
101
103
|
# @return [Number]
|
102
|
-
def count
|
104
|
+
def count
|
103
105
|
1
|
104
106
|
end
|
105
107
|
|
106
108
|
# Get all the errors generated while executing this Command
|
107
109
|
#
|
108
110
|
# @return [Error[]]
|
109
|
-
def all_errors
|
111
|
+
def all_errors
|
110
112
|
@errors
|
111
113
|
end
|
112
114
|
|
113
115
|
# Make this Command run quietly
|
114
|
-
def make_quiet
|
116
|
+
def make_quiet
|
115
117
|
@@quiet = true
|
116
118
|
end
|
117
119
|
|
118
120
|
# Convert this Command's index to a string representation
|
119
121
|
#
|
120
122
|
# @return [String]
|
121
|
-
def index_to_s
|
122
|
-
|
123
|
+
def index_to_s
|
124
|
+
(@@total - @index + 1).to_s.rjust(@@total.to_s.size)
|
123
125
|
end
|
124
126
|
|
125
127
|
# Execute this Command. A Command can be dry-run as well, in which it is
|
126
128
|
# not actually run.
|
127
|
-
def execute
|
129
|
+
def execute
|
128
130
|
CommandPrinter.new(self).print unless quiet?
|
129
|
-
run if
|
131
|
+
run if !dry_run? && runnable?
|
130
132
|
end
|
131
133
|
|
132
134
|
# Actually run this Command
|
133
|
-
def run
|
134
|
-
end
|
135
|
+
def run; end
|
135
136
|
|
136
137
|
# Are there any errors while configuring this Command? If not, this
|
137
138
|
# Command is runnable.
|
138
139
|
#
|
139
140
|
# @return [Boolean]
|
140
|
-
def runnable?
|
141
|
-
|
141
|
+
def runnable?
|
142
|
+
!errors?
|
142
143
|
end
|
143
144
|
|
144
145
|
# Create a String representation of this Command
|
145
146
|
#
|
146
147
|
# @return [String]
|
147
|
-
def to_s
|
148
|
+
def to_s
|
148
149
|
'command'
|
149
150
|
end
|
150
|
-
|
151
|
+
|
151
152
|
# Is this Command converting a directory?
|
152
153
|
#
|
153
154
|
# @return [Boolean] false
|
154
|
-
def directory?
|
155
|
+
def directory?
|
155
156
|
false
|
156
157
|
end
|
157
158
|
|
158
159
|
# Does this Command convert a file multiple times?
|
159
160
|
#
|
160
161
|
# @return [Boolean] false
|
161
|
-
def multiple?
|
162
|
+
def multiple?
|
162
163
|
false
|
163
164
|
end
|
164
165
|
|
165
166
|
# Will this Command be skipped, thus not executed?
|
166
167
|
#
|
167
168
|
# @return [Boolean] false
|
168
|
-
def skip?
|
169
|
+
def skip?
|
169
170
|
false
|
170
171
|
end
|
171
172
|
|
172
173
|
# Decrement the total number of conversion commands by 1
|
173
|
-
def uncount
|
174
|
+
def uncount
|
174
175
|
@@total -= 1
|
175
176
|
end
|
176
177
|
|
178
|
+
# rubocop:enable Style/ClassVars
|
179
|
+
|
177
180
|
# Has this Command run in any errors?
|
178
181
|
#
|
179
182
|
# @return [Error[]]
|
180
|
-
def
|
181
|
-
|
183
|
+
def errors?
|
184
|
+
!@errors.empty?
|
182
185
|
end
|
183
186
|
|
184
187
|
# Is the source file newer than the destination file?
|
@@ -188,8 +191,7 @@ module Pandocomatic
|
|
188
191
|
#
|
189
192
|
# @return [Boolean] True if src has been modified after dst has been last
|
190
193
|
def file_modified?(src, dst)
|
191
|
-
|
194
|
+
!File.exist? dst or File.mtime(src) > File.mtime(dst)
|
192
195
|
end
|
193
|
-
|
194
196
|
end
|
195
197
|
end
|
@@ -1,32 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#--
|
2
4
|
# Copyright 2017, Huub de Beer <Huub@heerdebeer.org>
|
3
|
-
#
|
5
|
+
#
|
4
6
|
# This file is part of pandocomatic.
|
5
|
-
#
|
7
|
+
#
|
6
8
|
# Pandocomatic is free software: you can redistribute it and/or modify
|
7
9
|
# it under the terms of the GNU General Public License as published by the
|
8
10
|
# Free Software Foundation, either version 3 of the License, or (at your
|
9
11
|
# option) any later version.
|
10
|
-
#
|
12
|
+
#
|
11
13
|
# Pandocomatic is distributed in the hope that it will be useful, but
|
12
14
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
13
15
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
14
16
|
# for more details.
|
15
|
-
#
|
17
|
+
#
|
16
18
|
# You should have received a copy of the GNU General Public License along
|
17
19
|
# with pandocomatic. If not, see <http://www.gnu.org/licenses/>.
|
18
20
|
#++
|
19
21
|
module Pandocomatic
|
22
|
+
require_relative '../error/io_error'
|
20
23
|
|
21
|
-
require_relative '
|
22
|
-
|
23
|
-
require_relative '
|
24
|
-
require_relative '
|
25
|
-
require_relative '
|
26
|
-
require_relative '
|
27
|
-
require_relative '
|
28
|
-
require_relative 'copy_file_command.rb'
|
29
|
-
require_relative 'skip_command.rb'
|
24
|
+
require_relative 'command'
|
25
|
+
require_relative 'create_link_command'
|
26
|
+
require_relative 'convert_file_command'
|
27
|
+
require_relative 'convert_list_command'
|
28
|
+
require_relative 'convert_file_multiple_command'
|
29
|
+
require_relative 'copy_file_command'
|
30
|
+
require_relative 'skip_command'
|
30
31
|
|
31
32
|
# Commmand to convert a directory
|
32
33
|
#
|
@@ -39,9 +40,10 @@ module Pandocomatic
|
|
39
40
|
# @!attribute dst_dir
|
40
41
|
# @return [String] the destination directory to convert to
|
41
42
|
class ConvertDirCommand < ConvertListCommand
|
42
|
-
|
43
43
|
attr_reader :config, :src_dir, :dst_dir
|
44
44
|
|
45
|
+
# rubocop:disable Metrics
|
46
|
+
|
45
47
|
# Create a new ConvertDirCommand
|
46
48
|
#
|
47
49
|
# @param current_config [Configuration] The configuration of pandocomatic
|
@@ -76,80 +78,76 @@ module Pandocomatic
|
|
76
78
|
|
77
79
|
dst = File.join @dst_dir, filename
|
78
80
|
|
79
|
-
if File.symlink?
|
81
|
+
if File.symlink?(src) && !config.follow_links?
|
80
82
|
subcommand = CreateLinkCommand.new(src, dst)
|
81
|
-
elsif File.directory? src
|
82
|
-
if config.recursive?
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
elsif File.file? src
|
88
|
-
if config.convert? src
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
subcommand = CopyFileCommand.new(src, dst)
|
93
|
-
end
|
83
|
+
elsif File.directory? src
|
84
|
+
subcommand = if config.recursive?
|
85
|
+
ConvertDirCommand.new(config, src, dst)
|
86
|
+
else
|
87
|
+
SkipCommand.new(src, :skipping_directory)
|
88
|
+
end
|
89
|
+
elsif File.file? src
|
90
|
+
if config.convert? src
|
91
|
+
subcommand = ConvertFileMultipleCommand.new(config, src, dst)
|
92
|
+
elsif !modified_only? || file_modified?(src, dst)
|
93
|
+
subcommand = CopyFileCommand.new(src, dst)
|
94
94
|
end
|
95
95
|
else
|
96
96
|
subcommand = SkipCommand.new(src, :unclear_what_to_do)
|
97
97
|
end
|
98
98
|
|
99
|
-
push subcommand unless subcommand.nil?
|
99
|
+
push subcommand unless subcommand.nil? || subcommand.skip?
|
100
100
|
end
|
101
101
|
|
102
102
|
# Empty commands do not count to the total amount of commands to execute
|
103
103
|
uncount if skip?
|
104
104
|
end
|
105
105
|
|
106
|
+
# rubocop:enable Metrics
|
107
|
+
|
106
108
|
# Should this command be skipped?
|
107
109
|
#
|
108
110
|
# @return [Boolean] True if this command has no sub commands
|
109
|
-
def skip?
|
111
|
+
def skip?
|
110
112
|
@subcommands.empty?
|
111
113
|
end
|
112
114
|
|
113
115
|
# Converts this command a directory?
|
114
116
|
#
|
115
117
|
# @return [Boolean] true
|
116
|
-
def directory?
|
118
|
+
def directory?
|
117
119
|
true
|
118
120
|
end
|
119
121
|
|
120
122
|
# Convert this command to a String representation for a Printer
|
121
123
|
#
|
122
124
|
# @return [String]
|
123
|
-
def to_s
|
124
|
-
"convert #{@src_dir}
|
125
|
+
def to_s
|
126
|
+
"convert #{@src_dir}; #{create_directory? ? 'create and ' : ''}enter #{@dst_dir}"
|
125
127
|
end
|
126
128
|
|
127
129
|
# Run this command
|
128
|
-
def run
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
raise IOError.new(:error_creating_directory, e, @dst_dir)
|
133
|
-
end
|
130
|
+
def run
|
131
|
+
Dir.mkdir @dst_dir if create_directory?
|
132
|
+
rescue SystemError => e
|
133
|
+
raise IOError.new(:error_creating_directory, e, @dst_dir)
|
134
134
|
end
|
135
135
|
|
136
136
|
private
|
137
137
|
|
138
|
-
def create_directory?
|
139
|
-
|
138
|
+
def create_directory?
|
139
|
+
!File.exist? @dst_dir or !File.directory? @dst_dir
|
140
140
|
end
|
141
141
|
|
142
142
|
# If the source directory contains a configuration file, use it to
|
143
143
|
# reconfigure the converter. Otherwise, use the current configuration
|
144
144
|
def reconfigure(current_config, src_dir)
|
145
145
|
config_file = File.join src_dir, Configuration::CONFIG_FILE
|
146
|
-
if File.exist? config_file
|
147
|
-
|
146
|
+
if File.exist? config_file
|
147
|
+
current_config.reconfigure config_file
|
148
148
|
else
|
149
|
-
|
149
|
+
current_config
|
150
150
|
end
|
151
|
-
config
|
152
151
|
end
|
153
|
-
|
154
152
|
end
|
155
153
|
end
|