Module: Locomotive::Steam::Adapters::Filesystem::YAMLLoader

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 11

def env
  @env
end

#site_pathObject (readonly)

Returns the value of attribute site_path.



11
12
13
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 11

def site_path
  @site_path
end

Instance Method Details

#_load(path, frontmatter = false, strict = false, &block) ⇒ Object



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
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 21

def _load(path, frontmatter = false, strict = false, &block)
  if File.exists?(path)
    yaml      = File.open(path).read.force_encoding('utf-8')
    template  = nil

    # JSON header?
    if frontmatter && match = yaml.match(JSON_FRONTMATTER_REGEXP)
      json, template = match[:json], match[:template]
      safe_json_load(json, template, path, &block)

    # YAML header?
    elsif frontmatter && match = yaml.match(strict ? YAML_FRONTMATTER_REGEXP : FRONTMATTER_REGEXP)
      yaml, template = match[:yaml], match[:template]
      safe_yaml_load(yaml, template, path, &block)

    elsif frontmatter
      message = 'Your file requires a valid YAML or JSON header'
      raise Locomotive::Steam::TemplateError.new(message, path, yaml, 0, nil)

    # YAML by default
    else
      safe_yaml_load(yaml, template, path, &block)
    end
  else
    Locomotive::Common::Logger.error "No #{path} file found"
    {}
  end
end

#initialize(site_path, env = :local) ⇒ Object



13
14
15
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 13

def initialize(site_path, env = :local)
  @site_path, @env = site_path, env
end

#load(scope = nil) ⇒ Object



17
18
19
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 17

def load(scope = nil)
  @scope = scope
end

#safe_json_file_load(path) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 74

def safe_json_file_load(path)
  return {} unless File.exists?(path)

  json = File.read(path)

  safe_json_load(json, nil, path)
end

#safe_json_load(json, template, path, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 62

def safe_json_load(json, template, path, &block)
  return {} if  json.blank?

  begin
    MultiJson.load(json).tap do |attributes|
      block.call(attributes, template) if block_given?
    end
  rescue MultiJson::ParseError => e
    raise Locomotive::Steam::JsonParsingError.new(e, path, json)
  end
end

#safe_yaml_load(yaml, template, path, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 50

def safe_yaml_load(yaml, template, path, &block)
  return {} if yaml.blank?

  begin
    HashConverter.to_sym(YAML.load(yaml)).tap do |attributes|
      block.call(attributes, template) if block_given?
    end
  rescue Exception => e
    raise "Malformed YAML in this file #{path}, error: #{e.message}"
  end
end

#template_extensionsObject



82
83
84
# File 'lib/locomotive/steam/adapters/filesystem/yaml_loader.rb', line 82

def template_extensions
  @extensions ||= %w(liquid haml)
end