module Locomotive::Steam::Adapters::Filesystem::YAMLLoader

Attributes

env[R]
site_path[R]

Public Class Methods

new(site_path, env = :local) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loader.rb, line 13
def initialize(site_path, env = :local)
  @site_path, @env = site_path, env
end

Public Instance Methods

_load(path, frontmatter = false, strict = false, &block) click to toggle source
# 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
load(scope = nil) click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loader.rb, line 17
def load(scope = nil)
  @scope = scope
end
safe_json_file_load(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_extensions() click to toggle source
# File lib/locomotive/steam/adapters/filesystem/yaml_loader.rb, line 82
def template_extensions
  @extensions ||= %w(liquid haml)
end