simple & extensible front matter parsing in text documents for ruby
Add this line to your application's Gemfile:
gem 'ruby_front_matter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby_front_matter
By default ruby_front_matter uses YAML format front matters, delimited by ---
Given a text such as:
str = '
---
front:
matter: "matters"
---
some text that would be left unparsed
'
in order to parse the front matter and get the text without it:
RubyFrontMatter::Parser.new().parse(str)
#=> [{'front' => { 'matter': 'matters' } }, 'some text that would be left unparsed']
RubyFrontMatter::Parser allows to inject a custom parser for customizing the front matter format.
For example, if you want JSON format
str = '
---
{
"front": "matters",
"matters": "front"
}
---
'
RubyFrontMatter::Parser(parser: JSON).parse(str)
#=> [{'front' => 'matters', 'matters' => 'front'}, '']
Any ruby object that responds to .load(string)
can be used as parser.
str = '
====
{
"front": "matters",
"matters": "front"
}
====
'
p = RubyFrontMatter::Parser.new(delimiter: '====', parser: JSON)
#=> [{'front' => 'matters', 'matters' => 'front'}, '']
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request