class MdMetadata::Extractor

Attributes

mode[RW]

Public Class Methods

new(mode = :hugo) click to toggle source
# File lib/md_metadata_extractor.rb, line 5
def initialize(mode = :hugo)
  # Define input data
  @input = ""
  @result = { :metadata => {} , :content => ""}
  @mode = mode
end

Public Instance Methods

content_regex() click to toggle source
# File lib/md_metadata_extractor.rb, line 14
def content_regex
    /#{Regexp.quote(delimiter)}.*#{Regexp.quote(delimiter)}(.*)\z/m
end
delimiter() click to toggle source
# File lib/md_metadata_extractor.rb, line 17
def delimiter
  case @mode
  when :hugo
    "+++"
  when :jekyll
    "---"
  end
end
extract(input) click to toggle source

Loads an input string for processing, returns a hash of values based on said metadata

# File lib/md_metadata_extractor.rb, line 38
def extract(input)
  raise "Malformed input file" unless metadata_regex.match?(input)
  @metadata = metadata_regex.match(input).to_s.delete(delimiter)
  @metadata.each_line do |line|
    meta = line.split(separator)
    unless meta[1].nil?
      meta[0].strip!
      meta[1].gsub!( '"', ' ').strip!
      @result[:metadata][meta[0]] = meta[1]
    end
  end
  @result[:content] = content_regex.match(input)[-1].to_s.strip!
  @result
end
metadata_regex() click to toggle source
# File lib/md_metadata_extractor.rb, line 11
def metadata_regex
    /#{Regexp.quote(delimiter)}(.*)#{Regexp.quote(delimiter)}/m
end
separator() click to toggle source
# File lib/md_metadata_extractor.rb, line 26
def separator
  case @mode
  when :hugo
    "="
  when :jekyll
    ":"
  end
end