preambular 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.markdown +60 -0
- data/Rakefile +1 -0
- data/lib/preambular.rb +85 -0
- data/lib/preambular/version.rb +3 -0
- data/preambular.gemspec +21 -0
- data/spec/preambular/preambular_spec.rb +77 -0
- data/spec/sample_files.rb +17 -0
- data/spec/sample_files/leading_whitespace.txt +6 -0
- data/spec/sample_files/standard.txt +5 -0
- data/spec/sample_files/unicode.txt +5 -0
- data/spec/spec_helper.rb +6 -0
- metadata +85 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Preamble
|
2
|
+
========
|
3
|
+
|
4
|
+
Introduction
|
5
|
+
------------
|
6
|
+
|
7
|
+
The preamble gem lets you add a yaml preamble to your files, much like the Jekyll static site generator
|
8
|
+
|
9
|
+
|
10
|
+
Example
|
11
|
+
-------
|
12
|
+
|
13
|
+
---
|
14
|
+
key1: value1
|
15
|
+
key2: [1, 2, 3]
|
16
|
+
---
|
17
|
+
|
18
|
+
Your body content goes here.
|
19
|
+
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-----
|
23
|
+
|
24
|
+
# load file with metadata and content
|
25
|
+
Preamble.load("./file.xyz")
|
26
|
+
|
27
|
+
# load multiple files with metadata and content
|
28
|
+
Preamble.load_multiple("./file.xyz", "./file.abc")
|
29
|
+
|
30
|
+
# save metadata and content
|
31
|
+
file = Preamble.new({"author" => "Lucky", "year" => 2014}, "My lucky diary.")
|
32
|
+
file.save("diary.txt")
|
33
|
+
|
34
|
+
# load, modify metadata, then save
|
35
|
+
file = Preamble.load('./file.xyz')
|
36
|
+
file.metadata["new_key"] = "factoid"
|
37
|
+
file.save('./file.xyz')
|
38
|
+
|
39
|
+
Output
|
40
|
+
------
|
41
|
+
|
42
|
+
The Preamble.load function returns a Preamble object. Your data will be in preamble.metadata, and the rest of the content will be in preamble.content.
|
43
|
+
|
44
|
+
`preamble.metadata`
|
45
|
+
|
46
|
+
{ "key1" => "value1", "key2" => [1, 2, 3] }
|
47
|
+
|
48
|
+
`preamble.content`
|
49
|
+
|
50
|
+
"\nYour body content goes here"
|
51
|
+
|
52
|
+
|
53
|
+
Notes
|
54
|
+
-----
|
55
|
+
|
56
|
+
1. The preamble must begin and end with three dashes '---'
|
57
|
+
2. Whitespace can go above the preamble, but no content
|
58
|
+
3. This gem is template-agnostic. It doesn't care what your body content is. It just gives it to you as a string.
|
59
|
+
|
60
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/preambular.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'preambular/version'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class Preambular
|
5
|
+
|
6
|
+
DEFAULTS = {
|
7
|
+
:external_encoding => Encoding.default_external
|
8
|
+
}
|
9
|
+
|
10
|
+
attr_accessor :metadata, :content
|
11
|
+
|
12
|
+
def initialize(metadata, content)
|
13
|
+
@metadata = metadata
|
14
|
+
@content = content
|
15
|
+
end
|
16
|
+
|
17
|
+
def metadata_with_content
|
18
|
+
@metadata.to_yaml + "---\n" + @content
|
19
|
+
end
|
20
|
+
|
21
|
+
def save(path, options = {})
|
22
|
+
options = DEFAULTS.merge(options)
|
23
|
+
|
24
|
+
open(path, "w:#{options[:external_encoding]}") do |f|
|
25
|
+
f.write metadata_with_content
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.load(path, options = {})
|
30
|
+
preamble_lines = String.new
|
31
|
+
content_lines = String.new
|
32
|
+
options = DEFAULTS.merge(options)
|
33
|
+
|
34
|
+
state = :before_preamble
|
35
|
+
|
36
|
+
open(path, "r:#{options[:external_encoding]}") do |f|
|
37
|
+
f.each do |line|
|
38
|
+
|
39
|
+
stripped = line.strip
|
40
|
+
|
41
|
+
case state
|
42
|
+
|
43
|
+
when :before_preamble
|
44
|
+
|
45
|
+
new_state = case stripped
|
46
|
+
when "---"
|
47
|
+
:preamble
|
48
|
+
when ""
|
49
|
+
:before_preamble
|
50
|
+
else
|
51
|
+
raise "First line must begin with ---"
|
52
|
+
end
|
53
|
+
|
54
|
+
when :preamble
|
55
|
+
|
56
|
+
new_state = case stripped
|
57
|
+
when "---"
|
58
|
+
:after_preamble
|
59
|
+
else
|
60
|
+
preamble_lines << line
|
61
|
+
:preamble
|
62
|
+
end
|
63
|
+
|
64
|
+
when :after_preamble
|
65
|
+
new_state = :after_preamble
|
66
|
+
content_lines << line
|
67
|
+
|
68
|
+
else
|
69
|
+
raise "Invalid State: #{ state }"
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
state = new_state
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
return new(YAML::load(preamble_lines), content_lines)
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.load_multiple(*paths)
|
81
|
+
options = paths.last.is_a?(Hash) ? paths.pop : {}
|
82
|
+
paths.map{ |path| Preambular.load(path, options) }
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/preambular.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "preambular/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "preambular"
|
7
|
+
s.version = Preambular::VERSION
|
8
|
+
s.authors = ["Starr Horne", "Duane Johnson"]
|
9
|
+
s.email = ["starr@chromahq.com", "duane.johnson@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/wordtreefoundation/preambular"
|
11
|
+
s.summary = %q{Use and save yaml preambles in your documents & templates}
|
12
|
+
s.description = %q{Allows you to add YAML front matter to your templates. Useful for adding metadata to static pages}
|
13
|
+
|
14
|
+
s.rubyforge_project = "preambular"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/**`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.add_dependency 'rspec'
|
21
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Preambular do
|
5
|
+
|
6
|
+
describe "#load" do
|
7
|
+
|
8
|
+
it "should parse the preamble of a single file" do
|
9
|
+
result = Preambular.load(standard)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return the result as a Preambular object" do
|
13
|
+
result = Preambular.load(standard)
|
14
|
+
expect(result).to be_a Preambular
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return a Preambular object with metadata as a Hash" do
|
18
|
+
result = Preambular.load(standard)
|
19
|
+
expect(result.metadata).to be_a Hash
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a Preambular object with content as a String" do
|
23
|
+
result = Preambular.load(standard)
|
24
|
+
expect(result.content).to be_a String
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should accept leading whitespace" do
|
28
|
+
Preambular.load(leading_whitespace)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should accept an options hash" do
|
32
|
+
Preambular.load(standard, {})
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should accept an encoding option" do
|
36
|
+
Preambular.load(standard, {:external_encoding => 'UTF-8'})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should read a UTF-8 character" do
|
40
|
+
result = Preambular.load(unicode, {:external_encoding => 'UTF-8'})
|
41
|
+
expect(result.metadata['unicode_tm']).to eq "\u2122" # "TM"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#metadata_with_content" do
|
47
|
+
it "should be the same as the source file" do
|
48
|
+
result = Preambular.load(standard)
|
49
|
+
expect(File.read(standard)).to eq(result.metadata_with_content)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#save" do
|
54
|
+
it "should save a modified file" do
|
55
|
+
result = Preambular.load(standard)
|
56
|
+
output_file = Tempfile.new('preamble').path
|
57
|
+
result.metadata["key1"] += "!"
|
58
|
+
result.save(output_file)
|
59
|
+
|
60
|
+
result = Preambular.load(output_file)
|
61
|
+
expect(result.metadata["key1"]).to eq("value1!")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#load_multiple" do
|
66
|
+
|
67
|
+
it "should load multiple files" do
|
68
|
+
Preambular.load_multiple(standard, leading_whitespace, unicode)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should accept options as tail arguments" do
|
72
|
+
Preambular.load_multiple(standard, leading_whitespace, unicode, external_encoding: 'UTF-8')
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SampleFiles
|
2
|
+
|
3
|
+
SAMPLE_FILES = {
|
4
|
+
standard: "standard.txt",
|
5
|
+
leading_whitespace: "leading_whitespace.txt",
|
6
|
+
unicode: "unicode.txt"
|
7
|
+
}
|
8
|
+
|
9
|
+
SAMPLE_FILES.each do |method, filename|
|
10
|
+
define_method(method) { full_path filename }
|
11
|
+
end
|
12
|
+
|
13
|
+
def full_path filename
|
14
|
+
"#{File.dirname(__FILE__)}/sample_files/#{filename}"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: preambular
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.3'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Starr Horne
|
9
|
+
- Duane Johnson
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
description: Allows you to add YAML front matter to your templates. Useful for adding
|
32
|
+
metadata to static pages
|
33
|
+
email:
|
34
|
+
- starr@chromahq.com
|
35
|
+
- duane.johnson@gmail.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- .gitignore
|
41
|
+
- .rspec
|
42
|
+
- Gemfile
|
43
|
+
- README.markdown
|
44
|
+
- Rakefile
|
45
|
+
- lib/preambular.rb
|
46
|
+
- lib/preambular/version.rb
|
47
|
+
- preambular.gemspec
|
48
|
+
- spec/preambular/preambular_spec.rb
|
49
|
+
- spec/sample_files.rb
|
50
|
+
- spec/sample_files/leading_whitespace.txt
|
51
|
+
- spec/sample_files/standard.txt
|
52
|
+
- spec/sample_files/unicode.txt
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
homepage: https://github.com/wordtreefoundation/preambular
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project: preambular
|
74
|
+
rubygems_version: 1.8.23
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Use and save yaml preambles in your documents & templates
|
78
|
+
test_files:
|
79
|
+
- spec/preambular/preambular_spec.rb
|
80
|
+
- spec/sample_files.rb
|
81
|
+
- spec/sample_files/leading_whitespace.txt
|
82
|
+
- spec/sample_files/standard.txt
|
83
|
+
- spec/sample_files/unicode.txt
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
has_rdoc:
|