module Pandocomatic::Pandocomatic

The Pandocomatic class controlls the pandocomatic conversion process

Constants

ERROR_STATUS

Pandocomatic error status codes start from ERROR_STATUS

FEATURES

Feature toggles supported by pandocomatic

LOG

Global logger for pandocomatic

Public Class Methods

run(args) click to toggle source

Run pandocomatic given options

@param args [String list of options to configure pandocomatic

# File lib/pandocomatic/pandocomatic.rb, line 150
def self.run(args)
  LOG.pandocomatic_called_with args
  start_time = Time.now

  # Depending on given command-line arguments, CLI#parse! also
  # installs a file logger in LOG.
  configuration = CLI.parse! args

  if configuration.show_version?
    # The version option has precedence over all other options; if
    # given, the version is printed
    VersionPrinter.new(VERSION).print
  elsif configuration.show_help?
    # The help option has precedence over all other options except the
    # version option. If given, the help is printed.
    HelpPrinter.new.print
  else
    # When using multiple input files, errors reading these
    # files are already encountered at this point. If there
    # are any errors, there is no reason to continue.
    if configuration.input.errors?
      ConfigurationErrorsPrinter.new(configuration.input.all_errors).print
      exit ERROR_STATUS
    end

    if configuration.dry_run?
      LOG.debug 'Start dry-run conversion:'
    else
      LOG.debug 'Start conversion:'
    end

    # Run the pandocomatic converter configured according to the options
    # given.
    #
    # Pandocomatic has two modes: converting a directory tree or
    # converting a single file. The mode is selected by the input.
    if configuration.directory?
      command = ConvertDirCommand.new(configuration, configuration.input_file, configuration.output)
    else
      command = ConvertFileMultipleCommand.new(configuration, configuration.input_file,
                                               configuration.output)
      command.make_quiet unless command.subcommands.size > 1
    end

    # Notify the user about all configuration errors collected when
    # determining the commands to run to perform this pandocomatic
    # conversion.
    if command.all_errors.size.positive?
      ConfigurationErrorsPrinter.new(command.all_errors).print
      exit ERROR_STATUS
    end

    # Pandocomatic is successfully configured: running the
    # actual conversion now. But first a short summary of the
    # process to execute is printed.
    SummaryPrinter.new(command, configuration).print if !configuration.quiet? || command.directory?

    # Depending on the options dry-run and quiet, the command.execute
    # method will actually performing the commands (dry-run = false) and
    # print the command to STDOUT (quiet = false)
    command.execute

    FinishPrinter.new(command, configuration, start_time).print unless configuration.quiet?
  end
rescue PandocomaticError => e
  # Report the error and break off the conversion process.
  ErrorPrinter.new(e).print
  exit ERROR_STATUS + 1
rescue StandardError => e
  # An unexpected error has occurred; break off the program drastically
  # for now. This is likely a bug: ask the user to report it.
  UnknownErrorPrinter.new(e).print
  exit ERROR_STATUS + 2
ensure
  configuration&.clean_up!
  LOG.info "------------  END  ---------------\n"
end