class Pandocomatic::MultipleFilesInput

A specific Input class to handle multiple input files

Public Class Methods

new(input, config) click to toggle source

Create a new MultipleFilesInput. As a side-effect a temporary file is created as well containing the content of all the files in input.

@param input [String a list with input files

Calls superclass method Pandocomatic::Input::new
# File lib/pandocomatic/multiple_files_input.rb, line 31
def initialize(input, config)
  super(input)
  @config = config
  create_temp_file
end

Public Instance Methods

destroy!() click to toggle source

Destroy the temporary file created for this MultipleFilesInput

# File lib/pandocomatic/multiple_files_input.rb, line 53
def destroy!
  return if @tmp_file.nil?

  @tmp_file.close
  @tmp_file.unlink
end
directory?() click to toggle source

Is this input a directory? A MultipleFilesInput cannot be a directory

@return Boolean

# File lib/pandocomatic/multiple_files_input.rb, line 48
def directory?
  false
end
name() click to toggle source

The name of this input

@return String

# File lib/pandocomatic/multiple_files_input.rb, line 40
def name
  @tmp_file.path
end
to_s() click to toggle source

A string representation of this Input

@return String

# File lib/pandocomatic/multiple_files_input.rb, line 63
def to_s
  input_string = @input_files.first
  previous_dir = File.dirname @input_files.first
  @input_files.slice(1..-1).each do |f|
    current_dir = File.dirname f
    if current_dir == previous_dir
      input_string += " + #{File.basename f}"
    else
      previous_dir = current_dir
      input_string += " + #{f}"
    end
  end

  input_string
end

Private Instance Methods

create_temp_file() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/pandocomatic/multiple_files_input.rb, line 83
def create_temp_file
  # Concatenate all input files into one (temporary) input file
  # created in the same directory as the first input file
  @tmp_file = Tempfile.new('pandocomatic_tmp_', File.dirname(absolute_path))

  contents = @input_files.map do |file|
    @errors.push IOError.new(:file_does_not_exist, nil, file) unless File.exist? file
    @errors.push IOError.new(:file_is_not_a_file, nil, file) unless File.file? file
    @errors.push IOError.new(:file_is_not_readable, nil, file) unless File.readable? file
    File.read File.absolute_path(file)
  end.join("\n\n")

  metadata = PandocMetadata.load contents

  unless metadata.unique?
    warn "\nWarning: Encountered the pandocomatic metadata property in " \
         'more than one YAML metadata block. Only the pandocomatic property ' \
         'from the first YAML metadata block is being used; the other ' \
         "pandocomatic properties have been discarded.\n\n"
  end

  @tmp_file.write contents
  @tmp_file.rewind
end