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) ⇒ 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
# File 'lib/kitchen/oven.rb', line 15

def self.bake(input_file:, recipes:, output_file:, config_file: 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
  )

  I18n.locale = doc.locale

  [recipes].flatten.each do |recipe|
    recipe.document = doc
    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