Class: Pandocomatic::ConvertFileMultipleCommand

Inherits:
ConvertListCommand show all
Defined in:
lib/pandocomatic/command/convert_file_multiple_command.rb

Overview

Create a command to convert one file multiple times

Instance Attribute Summary collapse

Attributes inherited from Command

#errors, #index

Instance Method Summary collapse

Methods inherited from ConvertListCommand

#all_errors, #count, #multiple?, #push, #skip?

Methods inherited from Command

#all_errors, #count, #directory?, #dry_run?, #errors?, #file_modified?, #index_to_s, #make_quiet, #modified_only?, #multiple?, #quiet?, reset, #run, #runnable?, #skip?, #src_root, #uncount

Constructor Details

#initialize(config, src, dst) ⇒ ConvertFileMultipleCommand

Create a new ConvertFileMultipleCommand

Parameters:

  • config (Configuration)

    Pandocomatic’s configuration used to convert the source file

  • src (String)

    the file to convert

  • dst (String)

    the output file



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
# File 'lib/pandocomatic/command/convert_file_multiple_command.rb', line 47

def initialize(config, src, dst)
  super()
  @config = config
  @src = src

  subcommands = []

  if @config.use_templates?
    # Command-line specified template overrides all internal templates
    Pandocomatic::LOG.info 'Ignoring any internal pandocomatic configuration from input\'s YAML blocks'

    @config.selected_templates.each do |template|
      subcommands.push ConvertFileCommand.new(@config, @src, dst, template)
    end
  else
     = @config. @src

    if &.template?
      # There are templates in this document's metadata, try to use
      # those.
      .templates.each do |template_name|
        unless template_name.empty? || config.template?(template_name)
          raise ConfigurationError.new(:no_such_template, nil,
                                       template_name)
        end

        subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
      end
    else
      # Try to match any global templates using the glob patterns
      global_templates = @config.determine_templates(@src)

      if global_templates.empty?
        subcommands.push ConvertFileCommand.new(@config, @src, dst)
      else
        global_templates.each do |template_name|
          subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
        end
      end
    end
  end

  # Only run a command if the src file is modified, or if the modified
  # setting is ignored.
  subcommands.each do |subcommand|
    if (!modified_only? || file_modified?(@src, subcommand.dst)) && !(subcommand.nil? || subcommand.skip?)
      push subcommand
    end
  end
end

Instance Attribute Details

#subcommandsCommand[]

ConvertFileMultipleCommand

Returns:

  • (Command[])

    the subcommands to execute when running this



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
# File 'lib/pandocomatic/command/convert_file_multiple_command.rb', line 36

class ConvertFileMultipleCommand < ConvertListCommand
  attr_reader :subcommands

  # rubocop:disable Metrics

  # Create a new ConvertFileMultipleCommand
  #
  # @param config [Configuration] Pandocomatic's configuration used to
  #   convert the source file
  # @param src [String] the file to convert
  # @param dst [String] the output file
  def initialize(config, src, dst)
    super()
    @config = config
    @src = src

    subcommands = []

    if @config.use_templates?
      # Command-line specified template overrides all internal templates
      Pandocomatic::LOG.info 'Ignoring any internal pandocomatic configuration from input\'s YAML blocks'

      @config.selected_templates.each do |template|
        subcommands.push ConvertFileCommand.new(@config, @src, dst, template)
      end
    else
       = @config. @src

      if &.template?
        # There are templates in this document's metadata, try to use
        # those.
        .templates.each do |template_name|
          unless template_name.empty? || config.template?(template_name)
            raise ConfigurationError.new(:no_such_template, nil,
                                         template_name)
          end

          subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
        end
      else
        # Try to match any global templates using the glob patterns
        global_templates = @config.determine_templates(@src)

        if global_templates.empty?
          subcommands.push ConvertFileCommand.new(@config, @src, dst)
        else
          global_templates.each do |template_name|
            subcommands.push ConvertFileCommand.new(@config, @src, dst, template_name)
          end
        end
      end
    end

    # Only run a command if the src file is modified, or if the modified
    # setting is ignored.
    subcommands.each do |subcommand|
      if (!modified_only? || file_modified?(@src, subcommand.dst)) && !(subcommand.nil? || subcommand.skip?)
        push subcommand
      end
    end
  end

  # rubocop:enable Metrics

  # A string representation of this command
  #
  # @return [String]
  def to_s
    "converting #{@src} #{@subcommands.size} time#{'s' if @subcommands.size != 1}:"
  end

  # Execute this ConvertFileMultipleCommand
  def execute
    return if @subcommands.empty?

    description = CommandPrinter.new(self)
    Pandocomatic::LOG.info description

    run if !dry_run? && runnable?

    @subcommands.each(&:execute)
  end
end

Instance Method Details

#executeObject

Execute this ConvertFileMultipleCommand



108
109
110
111
112
113
114
115
116
117
# File 'lib/pandocomatic/command/convert_file_multiple_command.rb', line 108

def execute
  return if @subcommands.empty?

  description = CommandPrinter.new(self)
  Pandocomatic::LOG.info description

  run if !dry_run? && runnable?

  @subcommands.each(&:execute)
end

#to_sString

A string representation of this command

Returns:

  • (String)


103
104
105
# File 'lib/pandocomatic/command/convert_file_multiple_command.rb', line 103

def to_s
  "converting #{@src} #{@subcommands.size} time#{'s' if @subcommands.size != 1}:"
end