module Bridgetown::Validatable

TODO: to be retired once the Resource engine is made official

Public Instance Methods

read_yaml(base, name, opts = {}) click to toggle source

Read the YAML frontmatter.

base - The String path to the dir containing the file. name - The String filename of the file. opts - optional parameter to File.read, default at site configs

Returns nothing. rubocop:disable Metrics/AbcSize

# File lib/bridgetown-core/concerns/validatable.rb, line 14
def read_yaml(base, name, opts = {})
  filename = File.join(base, name)

  begin
    self.content = File.read(@path || site.in_source_dir(base, name),
                             **Utils.merged_file_read_opts(site, opts))
    if content =~ Document::YAML_FRONT_MATTER_REGEXP
      self.content = $POSTMATCH
      self.data = YAMLParser.load(Regexp.last_match(1))&.with_dot_access
    end
  rescue Psych::SyntaxError => e
    Bridgetown.logger.warn "YAML Exception reading #{filename}: #{e.message}"
    raise e if site.config["strict_front_matter"]
  rescue StandardError => e
    Bridgetown.logger.warn "Error reading file #{filename}: #{e.message}"
    raise e if site.config["strict_front_matter"]
  end

  self.data ||= HashWithDotAccess::Hash.new

  validate_data! filename
  validate_permalink! filename

  self.data
end
validate_data!(filename) click to toggle source

FIXME: why doesn't Document validate data too?

# File lib/bridgetown-core/concerns/validatable.rb, line 42
def validate_data!(filename)
  unless self.data.is_a?(Hash)
    raise Errors::InvalidYAMLFrontMatterError,
          "Invalid YAML front matter in #{filename}"
  end
end