pandocomatic 0.2.8 → 1.0.0

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pandocomatic/cli.rb +81 -64
  3. data/lib/pandocomatic/command/command.rb +37 -35
  4. data/lib/pandocomatic/command/convert_dir_command.rb +44 -46
  5. data/lib/pandocomatic/command/convert_file_command.rb +314 -290
  6. data/lib/pandocomatic/command/convert_file_multiple_command.rb +56 -53
  7. data/lib/pandocomatic/command/convert_list_command.rb +31 -34
  8. data/lib/pandocomatic/command/copy_file_command.rb +14 -15
  9. data/lib/pandocomatic/command/create_link_command.rb +24 -27
  10. data/lib/pandocomatic/command/skip_command.rb +12 -15
  11. data/lib/pandocomatic/configuration.rb +682 -867
  12. data/lib/pandocomatic/default_configuration.yaml +4 -0
  13. data/lib/pandocomatic/error/cli_error.rb +30 -26
  14. data/lib/pandocomatic/error/configuration_error.rb +10 -9
  15. data/lib/pandocomatic/error/io_error.rb +13 -13
  16. data/lib/pandocomatic/error/pandoc_error.rb +10 -9
  17. data/lib/pandocomatic/error/pandocomatic_error.rb +15 -14
  18. data/lib/pandocomatic/error/processor_error.rb +9 -9
  19. data/lib/pandocomatic/error/template_error.rb +50 -0
  20. data/lib/pandocomatic/input.rb +53 -54
  21. data/lib/pandocomatic/multiple_files_input.rb +79 -72
  22. data/lib/pandocomatic/output.rb +29 -0
  23. data/lib/pandocomatic/pandoc_metadata.rb +193 -181
  24. data/lib/pandocomatic/pandocomatic.rb +101 -97
  25. data/lib/pandocomatic/pandocomatic_yaml.rb +69 -0
  26. data/lib/pandocomatic/path.rb +171 -0
  27. data/lib/pandocomatic/printer/command_printer.rb +7 -5
  28. data/lib/pandocomatic/printer/configuration_errors_printer.rb +7 -6
  29. data/lib/pandocomatic/printer/error_printer.rb +12 -7
  30. data/lib/pandocomatic/printer/finish_printer.rb +11 -10
  31. data/lib/pandocomatic/printer/help_printer.rb +8 -6
  32. data/lib/pandocomatic/printer/printer.rb +34 -34
  33. data/lib/pandocomatic/printer/summary_printer.rb +39 -33
  34. data/lib/pandocomatic/printer/version_printer.rb +8 -8
  35. data/lib/pandocomatic/printer/views/cli_error.txt +5 -0
  36. data/lib/pandocomatic/printer/views/configuration_error.txt +2 -1
  37. data/lib/pandocomatic/printer/views/error.txt +1 -1
  38. data/lib/pandocomatic/printer/views/finish.txt +1 -1
  39. data/lib/pandocomatic/printer/views/help.txt +27 -15
  40. data/lib/pandocomatic/printer/views/summary.txt +7 -1
  41. data/lib/pandocomatic/printer/views/template_error.txt +1 -0
  42. data/lib/pandocomatic/printer/views/version.txt +3 -3
  43. data/lib/pandocomatic/printer/views/warning.txt +1 -1
  44. data/lib/pandocomatic/printer/warning_printer.rb +21 -19
  45. data/lib/pandocomatic/processor.rb +28 -28
  46. data/lib/pandocomatic/processors/fileinfo_preprocessor.rb +35 -30
  47. data/lib/pandocomatic/processors/metadata_preprocessor.rb +23 -22
  48. data/lib/pandocomatic/template.rb +244 -0
  49. data/lib/pandocomatic/warning.rb +24 -25
  50. metadata +32 -12
@@ -1,31 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
- # Copyright 2017, Huub de Beer <Huub@heerdebeer.org>
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
22
+ require_relative '../error/io_error'
20
23
 
21
- require_relative '../error/io_error.rb'
22
-
23
- require_relative 'command.rb'
24
- require_relative 'create_link_command.rb'
25
- require_relative 'convert_file_command.rb'
26
- require_relative 'convert_list_command.rb'
27
- require_relative 'copy_file_command.rb'
28
- 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 'copy_file_command'
29
+ require_relative 'skip_command'
29
30
 
30
31
  # Create a command to convert one file multiple times
31
32
  #
@@ -33,9 +34,10 @@ module Pandocomatic
33
34
  # @return [Command[]] the subcommands to execute when running this
34
35
  # ConvertFileMultipleCommand
35
36
  class ConvertFileMultipleCommand < ConvertListCommand
36
-
37
37
  attr_reader :subcommands
38
38
 
39
+ # rubocop:disable Metrics
40
+
39
41
  # Create a new ConvertFileMultipleCommand
40
42
  #
41
43
  # @param config [Configuration] Pandocomatic's configuration used to
@@ -43,63 +45,64 @@ module Pandocomatic
43
45
  # @param src [String] the file to convert
44
46
  # @param dst [String] the output file
45
47
  def initialize(config, src, dst)
46
- super()
47
- @config = config
48
- @src = src
48
+ super()
49
+ @config = config
50
+ @src = src
51
+
52
+ metadata = PandocMetadata.load_file @src
49
53
 
50
- metadata = PandocMetadata.load_file @src
54
+ subcommands = []
51
55
 
52
- subcommands = []
56
+ if metadata.template?
57
+ # There are templates in this document's metadata, try to use
58
+ # those.
59
+ metadata.templates.each do |template_name|
60
+ unless template_name.empty? || config.template?(template_name)
61
+ raise ConfigurationError.new(:no_such_template, nil,
62
+ template_name)
63
+ end
53
64
 
54
- if metadata.has_template?
55
- # There are templates in this document's metadata, try to use
56
- # those.
57
- metadata.templates.each do |template_name|
58
- raise ConfigurationError.new(:no_such_template, nil, template_name) unless template_name.empty? or config.has_template? template_name
65
+ subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
66
+ end
67
+ else
68
+ # Try to match any global templates using the glob patterns
69
+ global_templates = @config.determine_templates(@src)
59
70
 
60
- subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
61
- end
71
+ if global_templates.empty?
72
+ subcommands.push ConvertFileCommand.new(@config, @src, dst)
62
73
  else
63
- # Try to match any global templates using the glob patterns
64
- global_templates = @config.determine_templates(@src)
65
-
66
- if global_templates.empty?
67
- subcommands.push ConvertFileCommand.new(@config, @src, dst)
68
- else
69
- global_templates.each do |template_name|
70
- subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
71
- end
72
- end
74
+ global_templates.each do |template_name|
75
+ subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
76
+ end
73
77
  end
78
+ end
74
79
 
75
- # Only run a command if the src file is modified, or if the modified
76
- # setting is ignored.
77
- subcommands.each do |subcommand|
78
- if not modified_only? or file_modified? @src, subcommand.dst then
79
- push subcommand unless subcommand.nil? or subcommand.skip?
80
- end
80
+ # Only run a command if the src file is modified, or if the modified
81
+ # setting is ignored.
82
+ subcommands.each do |subcommand|
83
+ if (!modified_only? || file_modified?(@src, subcommand.dst)) && !(subcommand.nil? || subcommand.skip?)
84
+ push subcommand
81
85
  end
86
+ end
82
87
  end
83
88
 
89
+ # rubocop:enable Metrics
84
90
 
85
91
  # A string representation of this command
86
92
  #
87
93
  # @return [String]
88
- def to_s()
89
- "converting #{@src} #{@subcommands.size} time#{'s' if @subcommands.size != 1}:"
94
+ def to_s
95
+ "converting #{@src} #{@subcommands.size} time#{'s' if @subcommands.size != 1}:"
90
96
  end
91
97
 
92
98
  # Execute this ConvertFileMultipleCommand
93
- def execute()
94
- if not @subcommands.empty?
95
- CommandPrinter.new(self).print unless quiet? or @subcommands.size == 1
96
- run if not dry_run? and runnable?
99
+ def execute
100
+ return if @subcommands.empty?
97
101
 
98
- @subcommands.each do |subcommand|
99
- subcommand.execute
100
- end
101
- end
102
- end
102
+ CommandPrinter.new(self).print unless quiet? || (@subcommands.size == 1)
103
+ run if !dry_run? && runnable?
103
104
 
105
+ @subcommands.each(&:execute)
106
+ end
104
107
  end
105
108
  end
@@ -1,41 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
- # Copyright 2017, Huub de Beer <Huub@heerdebeer.org>
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
22
+ require_relative '../error/io_error'
20
23
 
21
- require_relative '../error/io_error.rb'
22
-
23
- require_relative 'command.rb'
24
- require_relative 'create_link_command.rb'
25
- require_relative 'convert_file_command.rb'
26
- require_relative 'copy_file_command.rb'
27
- require_relative 'skip_command.rb'
24
+ require_relative 'command'
25
+ require_relative 'create_link_command'
26
+ require_relative 'convert_file_command'
27
+ require_relative 'copy_file_command'
28
+ require_relative 'skip_command'
28
29
 
29
30
  # A Command with sub commands
30
31
  #
31
32
  # @!attribute subcommands
32
33
  # @return [Command[]] the subcommands of this ConvertListCommand
33
34
  class ConvertListCommand < Command
34
-
35
35
  attr_reader :subcommands
36
36
 
37
37
  # Create a new ConvertListCommand
38
- def initialize()
38
+ def initialize
39
39
  super()
40
40
  @subcommands = []
41
41
  end
@@ -44,58 +44,55 @@ module Pandocomatic
44
44
  #
45
45
  # @param command [Command] command to add
46
46
  def push(command)
47
- @subcommands.push command
47
+ @subcommands.push command
48
48
  end
49
49
 
50
50
  # Skip this ConvertListCommand when there are no sub commands
51
51
  #
52
52
  # @return [Boolean]
53
- def skip?()
53
+ def skip?
54
54
  @subcommands.empty?
55
55
  end
56
56
 
57
57
  # The number of commands to execute when this ConvertListCommand
58
58
  # is executed.
59
- def count()
60
- @subcommands.reduce(if skip? then 0 else 1 end) do |total, subcommand|
61
- total += subcommand.count
59
+ def count
60
+ @subcommands.reduce(0) do |total, subcommand|
61
+ total + subcommand.count
62
62
  end
63
63
  end
64
64
 
65
65
  # Get a list of all errors generated while running this command
66
66
  #
67
- # @return [Error[]]
68
- def all_errors()
67
+ # @return [Error[]]
68
+ def all_errors
69
69
  @subcommands.reduce(@errors) do |total, subcommand|
70
- total += subcommand.all_errors
70
+ total + subcommand.all_errors
71
71
  end
72
72
  end
73
73
 
74
74
  # A string representation of this ConvertListCommand
75
75
  #
76
76
  # @return [String]
77
- def to_s()
78
- "converting #{@subcommands.size} items:"
77
+ def to_s
78
+ "converting #{@subcommands.size} items:"
79
79
  end
80
80
 
81
81
  # Can this command have multiple commands?
82
82
  #
83
83
  # @return [Boolean] true
84
84
  def multiple?
85
- true
85
+ true
86
86
  end
87
87
 
88
88
  # Execute this ConvertListCommand
89
- def execute()
90
- if not @subcommands.empty?
91
- CommandPrinter.new(self).print unless quiet?
92
- run if not dry_run? and runnable?
89
+ def execute
90
+ return if @subcommands.empty?
93
91
 
94
- @subcommands.each do |subcommand|
95
- subcommand.execute
96
- end
97
- end
98
- end
92
+ CommandPrinter.new(self).print unless quiet?
93
+ run if !dry_run? && runnable?
99
94
 
95
+ @subcommands.each(&:execute)
96
+ end
100
97
  end
101
98
  end
@@ -1,26 +1,28 @@
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
22
  require 'fileutils'
21
23
 
22
- require_relative '../error/io_error.rb'
23
- require_relative 'command.rb'
24
+ require_relative '../error/io_error'
25
+ require_relative 'command'
24
26
 
25
27
  # A command to copy a file
26
28
  #
@@ -38,24 +40,21 @@ module Pandocomatic
38
40
  @src = src
39
41
  @dst = dst
40
42
  @errors.push IOError.new(:file_is_not_readable, nil, @src) unless File.readable? @src
41
- @errors.push IOError.new(:file_is_not_writable, nil, @dst) unless not File.exist?(@dst) or File.writable?(@dst)
43
+ @errors.push IOError.new(:file_is_not_writable, nil, @dst) unless !File.exist?(@dst) || File.writable?(@dst)
42
44
  end
43
45
 
44
46
  # Run this CopyFileCommand
45
- def run()
46
- begin
47
- FileUtils.cp(@src, @dst) if file_modified?(@src, @dst)
48
- rescue StandardError => e
49
- raise IOError.new(:unable_to_copy_file, e, [@src, @dst])
50
- end
47
+ def run
48
+ FileUtils.cp(@src, @dst) if file_modified?(@src, @dst)
49
+ rescue StandardError => e
50
+ raise IOError.new(:unable_to_copy_file, e, [@src, @dst])
51
51
  end
52
52
 
53
53
  # A string representation of this CopyFileCommand
54
54
  #
55
55
  # @return [String]
56
- def to_s()
56
+ def to_s
57
57
  "copy #{File.basename @src}"
58
58
  end
59
-
60
59
  end
61
60
  end
@@ -1,28 +1,29 @@
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 'command'
20
23
 
21
- require_relative 'command.rb'
22
-
23
- require_relative '../warning.rb'
24
- require_relative '../error/io_error.rb'
25
- require_relative '../printer/warning_printer.rb'
24
+ require_relative '../warning'
25
+ require_relative '../error/io_error'
26
+ require_relative '../printer/warning_printer'
26
27
 
27
28
  # A command to create a link to a file
28
29
  #
@@ -35,7 +36,6 @@ module Pandocomatic
35
36
  # @!attribute dst_target
36
37
  # @return [String] the link in the destination tree to link to
37
38
  class CreateLinkCommand < Command
38
-
39
39
  attr_reader :src, :dst, :dst_target
40
40
 
41
41
  # Create a new CreateLinkCommand
@@ -48,10 +48,10 @@ module Pandocomatic
48
48
  begin
49
49
  src_target = File.readlink @src
50
50
 
51
- if src_target.start_with? '.' then
51
+ if src_target.start_with? '.'
52
52
  full_src_target = File.expand_path src_target, File.dirname(src)
53
53
 
54
- if full_src_target.start_with? src_root()
54
+ if full_src_target.start_with? src_root
55
55
  @dst = dst
56
56
  @dst_target = src_target
57
57
  else
@@ -66,39 +66,37 @@ module Pandocomatic
66
66
  end
67
67
 
68
68
  # Run this CreateLinkCommand
69
- def run()
70
- begin
71
- File.symlink @dst_target, @dst unless File.exist? @dst
72
- rescue StandardError => e
73
- raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
74
- end
69
+ def run
70
+ File.symlink @dst_target, @dst unless File.exist? @dst
71
+ rescue StandardError => e
72
+ raise IOError.new(:unable_to_create_symbolic_link, e, [@src, @dst])
75
73
  end
76
74
 
77
75
  # Can this CreateLinkCommand be run?
78
76
  #
79
77
  # @return [Boolean] True if there are no errors and both source and
80
78
  # destination do exist
81
- def runnable?()
82
- not (has_errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
79
+ def runnable?
80
+ !(errors? or @dst.nil? or @dst_target.nil? or @src.nil?)
83
81
  end
84
82
 
85
83
  # Create a string representation of this CreateLinkCommand
86
- def to_s()
84
+ def to_s
87
85
  "link #{File.basename @dst} -> #{@dst_target}"
88
86
  end
89
87
 
90
88
  # Should this CreateLinkCommand be skipped?
91
89
  #
92
90
  # @return [Boolean]
93
- def skip?()
94
- not modified_only? or not modified?
91
+ def skip?
92
+ !modified_only? or !modified?
95
93
  end
96
-
94
+
97
95
  # Has the source file been modified?
98
96
  #
99
97
  # @return [Boolean]
100
- def modified?()
101
- if File.exist? @dst then
98
+ def modified?
99
+ if File.exist? @dst
102
100
  absolute_dst = File.realpath @dst
103
101
  target = File.expand_path(@dst_target, absolute_dst)
104
102
  absolute_dst != target
@@ -106,6 +104,5 @@ module Pandocomatic
106
104
  true
107
105
  end
108
106
  end
109
-
110
107
  end
111
108
  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 'command.rb'
22
+ require_relative 'command'
22
23
 
23
24
  # A command to skip a file or directory
24
25
  #
@@ -44,18 +45,17 @@ module Pandocomatic
44
45
  # Has this SkipCommand a message?
45
46
  #
46
47
  # @return [Boolean]
47
- def has_message?()
48
- not(@message.nil? or @message.empty?)
48
+ def message?
49
+ !((@message.nil? or @message.empty?))
49
50
  end
50
51
 
51
52
  # 'Run' this SkipCommand by doing nothing
52
- def run()
53
- end
53
+ def run; end
54
54
 
55
55
  # Skip this command
56
56
  #
57
57
  # @return [Boolean] true
58
- def skip?()
58
+ def skip?
59
59
  true
60
60
  end
61
61
 
@@ -63,10 +63,7 @@ module Pandocomatic
63
63
  #
64
64
  # @return [String]
65
65
  def to_s
66
- "skipping #{File.basename @src}" + if has_message?
67
- ": #{@message.to_s}"
68
- end
66
+ "skipping #{File.basename @src}" + (": #{@message}" if message?)
69
67
  end
70
-
71
68
  end
72
69
  end