Class: Bookbinder::Preprocessing::DitaPDFPreprocessor

Inherits:
DitaPreprocessor show all
Defined in:
lib/bookbinder/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.



8
9
10
11
12
# File 'lib/bookbinder/preprocessing/dita_pdf_preprocessor.rb', line 8

def initialize(fs, command_creator, sheller)
  @fs = fs
  @command_creator = command_creator
  @sheller = sheller
end

Instance Method Details

#applicable_to?(section) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/bookbinder/preprocessing/dita_pdf_preprocessor.rb', line 14

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

#most_recent_pdf(dir_path) ⇒ Object



40
41
42
43
# File 'lib/bookbinder/preprocessing/dita_pdf_preprocessor.rb', line 40

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bookbinder/preprocessing/dita_pdf_preprocessor.rb', line 18

def preprocess(sections, output_locations, options: [], output_streams: nil, **_)
  sections.each do |section|
    command = command_creator.convert_to_pdf_command(
      section,
      dita_flags: dita_flags(options),
      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