Class: Bookwatch::Preprocessing::DitaPDFPreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb

Constant Summary collapse

DitaToPDFLibraryFailure =
Class.new(RuntimeError)

Instance Method Summary collapse

Constructor Details

#initialize(fs, command_creator, sheller) ⇒ DitaPDFPreprocessor

Returns a new instance of DitaPDFPreprocessor.



6
7
8
9
10
# 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

Instance Method Details

#applicable_to?(section) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/bookwatch/preprocessing/dita_pdf_preprocessor.rb', line 12

def applicable_to?(section)
  !!section.pdf_output_filename
end

#most_recent_pdf(dir_path) ⇒ Object



38
39
40
41
# 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, **_) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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