class Metatext

MetaText a lightweight jekyll-style metadata parser

what it do

@author jguest

Public Class Methods

configure(dir: nil, ext: nil, processor: nil) click to toggle source

@param dir - path to metatext files @param ext - the file extention (e.g. ‘txt’, ‘md’, ‘txt.erb’) @param processor - any object that responds to ‘#render`

# File lib/metatext.rb, line 24
def configure(dir: nil, ext: nil, processor: nil)
  @dir = dir
  @ext = ext
  @pro = processor
end
parse(to_parse, locals={}) { |metadata(raw), render(raw)| ... } click to toggle source

main driver method for metatext parsing @return self

# File lib/metatext.rb, line 33
def parse(to_parse, locals={})
  raw = read(to_parse) || to_parse
  raw = erbify raw, with: locals if parse_as_erb? raw
  yield metadata(raw), render(raw)
end

Private Class Methods

data_regex() click to toggle source

a regex for “—- some metadata — the real content” @return regular expression

# File lib/metatext.rb, line 85
def data_regex
  /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
end
erbify(raw, with: {}) click to toggle source

run the file contents through erb @param with - the variables you want available in the file

# File lib/metatext.rb, line 60
def erbify(raw, with: {})
  namespace = OpenStruct.new with
  ERB.new(raw).result namespace.instance_eval { binding }
end
metadata(raw) click to toggle source

get metadata with yaml in-between “—” @param raw

# File lib/metatext.rb, line 68
def metadata(raw)
  raw.match data_regex
  OpenStruct.new YAML.load($1) if $1
end
parse_as_erb?(raw) click to toggle source

run raw content through erb? @param raw

# File lib/metatext.rb, line 53
def parse_as_erb?(raw)
  (@ext && @ext.include?("erb")) || (@ext.nil? && raw.include?("<%"))
end
read(to_parse) click to toggle source

opens and reads the metatext file @param file

# File lib/metatext.rb, line 44
def read(to_parse)
  if to_parse.is_a? Symbol
    File.read "#{@dir}/#{to_parse.to_s}.#{@ext}"
  end
end
render(raw) click to toggle source

get content with everything but yaml section @param raw

# File lib/metatext.rb, line 76
def render(raw)
  content = raw.gsub data_regex, "" || raw
  return @pro.render content if @pro
  content
end