class Bookwatch::Preprocessing::DitaPDFPreprocessor

Constants

DitaToPDFLibraryFailure

Attributes

command_creator[R]
fs[R]
sheller[R]

Public Class Methods

new(fs, command_creator, sheller) click to toggle source
# File lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb, line 6
def initialize(fs, command_creator, sheller)
  @fs = fs
  @command_creator = command_creator
  @sheller = sheller
end

Public Instance Methods

applicable_to?(section) click to toggle source
# File lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb, line 12
def applicable_to?(section)
  !!section.pdf_output_filename
end
most_recent_pdf(dir_path) click to toggle source
# File lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb, line 38
def most_recent_pdf(dir_path)
  pdfs_by_modified_date = Dir.glob(dir_path + '**/*.pdf').sort_by{ |f| File.mtime(f) }
  pdfs_by_modified_date.last
end
preprocess(sections, output_locations, options: {}, output_streams: nil, **_) click to toggle source
# File lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb, line 16
def preprocess(sections, output_locations, options: {}, output_streams: nil, **_)
  sections.each do |section|
    command = command_creator.convert_to_pdf_command(
      section,
      dita_flags: options[:dita_flags],
      write_to: output_locations.pdf_from_preprocessing_dir
    )
    status = sheller.run_command(command, output_streams.to_h)

    if status.success?
      pdf_path = most_recent_pdf(output_locations.pdf_from_preprocessing_dir)
      pdf_destination = output_locations.pdf_artifact_dir.join(section.pdf_output_filename)
      fs.copy_and_rename(pdf_path, pdf_destination)
    else
      raise DitaToPDFLibraryFailure.new 'The DITA-to-PDF conversion failed. ' +
        'Please check that you have specified the path to your DITA-OT library in the ENV, ' +
        'that your DITA-specific keys/values in config.yml are set, ' +
        'and that your DITA toolkit is correctly configured.'
    end
  end
end