Module: Yarrow::Format::Methods::FrontMatter::ClassMethods

Defined in:
lib/yarrow/format/methods/front_matter.rb

Instance Method Summary collapse

Instance Method Details

#extract_yfm(text, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



66
67
68
# File 'lib/yarrow/format/methods/front_matter.rb', line 66

def parse_json(raw)
  JSON.parse("{#{raw}}", symbolize_names: true)
end

#parse_toml(raw) ⇒ Object



62
63
64
# File 'lib/yarrow/format/methods/front_matter.rb', line 62

def parse_toml(raw)
  TomlRB.parse(raw, symbolize_keys: true)
end

#parse_yaml(raw) ⇒ Object



58
59
60
# File 'lib/yarrow/format/methods/front_matter.rb', line 58

def parse_yaml(raw)
  YAML.load(raw, symbolize_names: true)
end

#read(path) ⇒ Object



24
25
26
27
28
# File 'lib/yarrow/format/methods/front_matter.rb', line 24

def read(path)
  text = File.read(path, :encoding => "utf-8")
  source,  = parse(text)
  Yarrow::Format::Contents.new(new(source), )
end

#read_yfm(path) ⇒ Object



70
71
72
73
# 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) ⇒ Object



100
101
102
103
104
105
106
# 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