class FrontMatter
Constants
- VERSION
Attributes
setting[RW]
Public Class Methods
new( opts={} )
click to toggle source
# File lib/front_matter.rb, line 20 def initialize( opts={} ) comment_marker = %r{(?<comment> ^\s* \W{1,2} )}x @setting = { :patterns => { :header => { :filetype => %r{.*}, :pattern => %r{ (?<start> #{comment_marker} \s* [-=#*]{3,} .* )[\r\n] (?<content> (?: \k<comment> .* [\r\n])+) \k<start> }x }, :yaml => { :filetype => %r{.*}, :pattern => %r{ (?<start> #{comment_marker} \s* -{3} )[\r\n] (?<content> (?: \k<comment> .* [\r\n])+) (?<empty>^ \s* $) }x }, }, :unindent => false , :as_yaml => false , } @setting.merge!(opts) end
Public Instance Methods
extract(contents, filetype=[])
click to toggle source
# File lib/front_matter.rb, line 47 def extract(contents, filetype=[]) unless filetype.empty? patterns = @setting[:patterns].select { |kind, pattern| filetype.any { |ft| pattern[:filetype].match(ft)} } else patterns = @setting[:patterns] end union_patterns = Regexp.union(patterns.collect{ |k, p| p[:pattern] }) results = [] contents.mscan(union_patterns).each { |m| results << m[:content].gsub(/^#{Regexp.escape(m[:comment])}/, "") } results.map! { |r| r.unindent } if @setting[:unindent] results.map! { |r| "---\n#{r}" } if @setting[:as_yaml] return results end
extract_file(path, opts={})
click to toggle source
# File lib/front_matter.rb, line 68 def extract_file(path, opts={}) filetype = opts[:filetype] ? opts[:filetype] : [] if opts[:firstline] || opts[:lastline] firstline = opts[:firstline] ? opts[:firstline] - 1 : 0 lastline = opts[:lastline] ? opts[:lastline] -1 : -1 return extract(File.readlines(path)[firstline..lastline].join, filetype) else return extract(File.read(path), filetype) end end