module Yarrow::Format::Methods::FrontMatter::ClassMethods

Public Instance Methods

extract_yfm(text, options={}) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 75
def extract_yfm(text, options={})
  pattern = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
  if text =~ pattern
    content = text.sub(pattern, "")
  
    begin
      if options.key?(:symbolize_keys)
        meta = YAML.load($1, symbolize_names: true)
      else
        meta = YAML.load($1)
      end
      return [content, meta]
    rescue Psych::SyntaxError => error
      if defined? ::Logger
        # todo: application wide logger
        #logger = ::Logger.new(STDOUT)
        #logger.error "#{error.message}"
      end
      return [content, nil]
    end
  end
  
  [text, nil]
end
parse(text) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 30
def parse(text)
  result = Syntax::PATTERN.match(text)

  return [text, nil] if result.nil?

  start_chr = result[:start].chr
  stop_chr = result[:stop].chr
  input = result[:meta]

  meta = if start_chr == stop_chr
    case start_chr
    when Syntax::YAML then parse_yaml(input)
    when Syntax::TOML then parse_toml(input)
    when Syntax::MM_JSON then parse_json(input)
    end
  else
    if start_chr == Syntax::YAML && stop_chr == Syntax::YAML_DOC_END
      parse_yaml(input)
    elsif start_chr == Syntax::JSON_START && stop_chr == Syntax::JSON_END
      parse_json(input)
    else
      raise "Unsupported frontmatter delimiters"
    end
  end

  [result[:body], meta]
end
parse_json(raw) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 66
def parse_json(raw)
  JSON.parse("{#{raw}}", symbolize_names: true)
end
parse_toml(raw) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 62
def parse_toml(raw)
  TomlRB.parse(raw, symbolize_keys: true)
end
parse_yaml(raw) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 58
def parse_yaml(raw)
  YAML.load(raw, symbolize_names: true)
end
read(path) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 24
def read(path)
  text = File.read(path, :encoding => "utf-8")
  source, metadata = parse(text)
  Yarrow::Format::Contents.new(new(source), metadata)
end
read_yfm(path) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 70
def read_yfm(path)
  text = File.read(path, :encoding => 'utf-8')
  extract_yfm(text, symbolize_keys: true)
end
write_yfm(name, text, meta) click to toggle source
# File lib/yarrow/format/methods/front_matter.rb, line 100
def write_yfm(name, text, meta)
  # Symbolized keys are better to deal with when manipulating data in
  # Ruby but are an interop nightmare when serialized so here we do a
  # round-trip through JSON encoding to ensure all keys are string
  # encoded before dumping them to the front matter format.
  File.write(name, [YAML.dump(meta.to_json), "---", text].join("\n"))
end