Class: Kitchen::Oven

Inherits:
Object show all
Defined in:
lib/kitchen/oven.rb

Overview

A class for baking documents according to the instructions in recipes

Defined Under Namespace

Classes: BakeProfile

Class Method Summary collapse

Class Method Details

.bake(input_file:, recipes:, output_file:, config_file: nil, resource_dir: nil) ⇒ Object

Bakes an input file using a recipe to produce an output file

Parameters:

  • input_file (String)

    the path to the input file

  • config_file (String) (defaults to: nil)

    the path to the configuration file

  • recipes (Array<Recipe>)

    an array of recipes with which to bake the document

  • output_file (String)

    the path to the output file



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kitchen/oven.rb', line 15

def self.bake(input_file:, recipes:, output_file:, config_file: nil, resource_dir: nil)
  profile = BakeProfile.new
  profile.started!

  nokogiri_doc = File.open(input_file) do |f|
    profile.opened!
    Nokogiri::XML(f).tap { profile.parsed! }
  end

  config = config_file.nil? ? nil : Config.new_from_file(File.open(config_file))

  doc = Kitchen::Document.new(
    nokogiri_document: nokogiri_doc,
    config: config
  )

  # Add image metadata to resource hash
  resources = {}
  if resource_dir
    Dir.foreach(resource_dir) do |filename|
      next unless filename.match(/.json/)

      file = File.read("#{resource_dir}/#{filename}")
      img_hash = JSON.parse(file).with_indifferent_access
      img_src = filename.gsub('.json', '').to_sym
      resources[img_src] = img_hash
    end
  end

  I18n.locale = doc.locale

  [recipes].flatten.each do |recipe|
    recipe.document = doc
    recipe.resources = resources
    recipe.bake
  end
  profile.baked!

  File.open(output_file, 'w') do |f|
    f.write doc.to_xhtml(indent: 2, encoding: doc.encoding || 'utf-8')
  end
  profile.written!

  Nokogiri::XML.print_profile_data if ENV['PROFILE'] && !ENV['TESTING']

  profile
end