ruby_front_matter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,30 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ <<<<<<< HEAD
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ =======
13
+ coverage
14
+ InstalledFiles
15
+ >>>>>>> 918d660549aa1dfc8b91d49dea346975144b2cad
16
+ lib/bundler/man
17
+ pkg
18
+ rdoc
19
+ spec/reports
20
+ test/tmp
21
+ test/version_tmp
22
+ tmp
23
+ <<<<<<< HEAD
24
+ =======
25
+
26
+ # YARD artifacts
27
+ .yardoc
28
+ _yardoc
29
+ doc/
30
+ >>>>>>> 918d660549aa1dfc8b91d49dea346975144b2cad
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ # Specify your gem's dependencies in ruby_front_matter.gemspec
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 F-3r
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # RubyFrontMatter
2
+
3
+ simple &amp; extensible front matter parsing in text documents for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby_front_matter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby_front_matter
18
+
19
+ ## Usage
20
+
21
+
22
+ ### configuring front matter format and delimiter
23
+
24
+ **RubyFrontMatter::Parser** allows to inject a custom parser for customizing the front matter format.
25
+
26
+ For example, if you want **JSON** format
27
+
28
+ ```ruby
29
+
30
+ str = '
31
+ ---
32
+ {
33
+ "front": "matters",
34
+ "matters": "front"
35
+ }
36
+ ---
37
+ '
38
+ RubyFrontMatter::Parser(parser: JSON).parse(str)
39
+ #=> [{'front' => 'matters', 'matters' => 'front'}, '']
40
+ ```
41
+
42
+ Any ruby object that responds to ```.load(string)``` can be used as parser.
43
+
44
+
45
+ ### configuring delimiter string
46
+
47
+ ```ruby
48
+ str = '
49
+ ====
50
+ {
51
+ "front": "matters",
52
+ "matters": "front"
53
+ }
54
+ ====
55
+ '
56
+
57
+ p = RubyFrontMatter::Parser.new(delimiter: '====', parser: JSON)
58
+ #=> [{'front' => 'matters', 'matters' => 'front'}, '']
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs = ["lib"]
8
+ t.warning = false
9
+ t.verbose = false
10
+ t.test_files = FileList['spec/*_spec.rb']
11
+ end
@@ -0,0 +1,3 @@
1
+ module RubyFrontMatter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require "ruby_front_matter/version"
2
+
3
+ module RubyFrontMatter
4
+ class Parser
5
+ attr_reader :parser
6
+
7
+ def initialize(options = {})
8
+ options = {delimiter: '---', parser: YAML}.merge(options)
9
+ @delimiter = options[:delimiter]
10
+ @parser = options[:parser]
11
+ end
12
+
13
+ def parse(string = '')
14
+ res = [{},'']
15
+ res[1] = string.lstrip.gsub(/#{@delimiter}(.*)#{@delimiter}/m) do |match|
16
+ res[0] = @parser.load($~.captures.first.strip)
17
+ ''
18
+ end.strip
19
+ res
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_front_matter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_front_matter"
8
+ spec.version = RubyFrontMatter::VERSION
9
+ spec.authors = ["F-3r"]
10
+ spec.email = ["f.fernando.martinez@gmail.com"]
11
+ spec.description = "simple & extensible front matter parsing for ruby"
12
+ spec.summary = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,76 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ require 'json'
4
+ require 'bundler'
5
+ Bundler.require
6
+
7
+ describe RubyFrontMatter::Parser do
8
+ describe ".parse" do
9
+ describe "when input string is empty" do
10
+ it "returns an [{}, '']" do
11
+ str = ''
12
+ RubyFrontMatter::Parser.new.parse(str).must_equal [{}, '']
13
+ end
14
+ end
15
+
16
+ describe "when input string has no front matter" do
17
+ it "returns an empty hash and the string" do
18
+ str = "i'm just a piece of text"
19
+ RubyFrontMatter::Parser.new.parse(str).must_equal [{}, str]
20
+ end
21
+ end
22
+
23
+ describe "when input has only a front matter" do
24
+ it "returns a hash with an empty string" do
25
+ str = "
26
+ ---
27
+ front:
28
+ matter: 'yes, matters'
29
+ ---
30
+ "
31
+ RubyFrontMatter::Parser.new.parse(str).must_equal [{'front' => {'matter' => 'yes, matters'}}, '']
32
+ end
33
+
34
+ describe "when input has both" do
35
+ it "returns parsed front matter and rest of the string contents" do
36
+ str = "
37
+ ---
38
+ a:
39
+ b: 123
40
+ ---
41
+
42
+ some text
43
+ "
44
+ RubyFrontMatter::Parser.new.parse(str).must_equal [{'a' => {'b' => 123}}, "some text"]
45
+ end
46
+ end
47
+
48
+ it "allows to inject the parser" do
49
+ #thus changing the front matter format
50
+ p = RubyFrontMatter::Parser.new(parser: JSON)
51
+ str = '
52
+ ---
53
+ {
54
+ "front": "matters",
55
+ "matters": "front"
56
+ }
57
+ ---
58
+ '
59
+ p.parse(str).must_equal [{'front' => 'matters', 'matters' => 'front'}, '']
60
+ end
61
+
62
+ it "allows to change the delimiter string" do
63
+ p = RubyFrontMatter::Parser.new(delimiter: '====', parser: JSON)
64
+ str = '
65
+ ====
66
+ {
67
+ "front": "matters",
68
+ "matters": "front"
69
+ }
70
+ ====
71
+ '
72
+ p.parse(str).must_equal [{'front' => 'matters', 'matters' => 'front'}, '']
73
+ end
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_front_matter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - F-3r
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: simple & extensible front matter parsing for ruby
47
+ email:
48
+ - f.fernando.martinez@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/ruby_front_matter.rb
59
+ - lib/ruby_front_matter/version.rb
60
+ - ruby_front_matter.gemspec
61
+ - spec/ruby_front_matter_spec.rb
62
+ homepage: ''
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: ''
87
+ test_files:
88
+ - spec/ruby_front_matter_spec.rb