Sha256: c71c3238f76d33ea0fe43a077603eff690bc1cba9b152463c5b5420db0cc3bc3

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require "yaml"

module Metanorma
  module Cli
    class Collection
      def initialize(file, options)
        @file = file
        @options = Cli.with_indifferent_access(options)
        @output_dir = @options.delete(:output_dir)
        @compile_options = @options.delete(:compile)
      end

      def self.render(filename, options = {})
        new(filename, options).render
      end

      def render
        extract_options_from_file
        collection_file.render(collection_options.compact)
      end

      private

      attr_reader :file, :options

      def collection_file
        @collection_file ||= Metanorma::Collection.parse(file)
      end

      def source_folder
        @source_folder ||= File.dirname(File.expand_path(file))
      end

      def collection_options
        @collection_options ||= {
          compile: @compile_options,
          output_folder: build_output_folder,
          coverpage: @options.fetch(:coverpage, nil),
          format: collection_output_formats(@options.fetch(:format, "")),
        }
      end

      def build_output_folder
        output_folder = options.fetch(:output_folder, nil)

        if output_folder && @output_dir
          @output_dir.join(output_folder).to_s
        else
          output_folder || source_folder
        end
      end

      def collection_output_formats(formats)
        if formats.is_a?(String)
          formats = formats.split(",")
        end

        (formats || []).map { |extension| extension.strip.to_sym }
      end

      def extract_options_from_file
          yaml_file = YAML.safe_load(File.read(@file.to_s))

          @options = Cli.with_indifferent_access(
            yaml_file.slice("coverpage", "format", "output_folder"),
          )
          @options.merge!(options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metanorma-cli-1.7.1 lib/metanorma/cli/collection.rb
metanorma-cli-1.7.1.pre lib/metanorma/cli/collection.rb