preamble 0.0.2 → 0.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/.rspec +2 -0
- data/README.markdown +19 -2
- data/lib/preamble.rb +61 -35
- data/lib/preamble/version.rb +2 -2
- data/preamble.gemspec +2 -1
- data/spec/preamble/preamble_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 +58 -33
data/.rspec
ADDED
data/README.markdown
CHANGED
@@ -21,16 +21,33 @@ Example
|
|
21
21
|
Usage
|
22
22
|
-----
|
23
23
|
|
24
|
+
# load file with metadata and content
|
24
25
|
Preamble.load("./file.xyz")
|
26
|
+
|
27
|
+
# load multiple files with metadata and content
|
25
28
|
Preamble.load_multiple("./file.xyz", "./file.abc")
|
26
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')
|
27
38
|
|
28
39
|
Output
|
29
40
|
------
|
30
41
|
|
31
|
-
The Preamble.load function returns
|
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`
|
32
49
|
|
33
|
-
|
50
|
+
"\nYour body content goes here"
|
34
51
|
|
35
52
|
|
36
53
|
Notes
|
data/lib/preamble.rb
CHANGED
@@ -1,59 +1,85 @@
|
|
1
|
-
require
|
1
|
+
require 'preamble/version'
|
2
2
|
require 'yaml'
|
3
3
|
|
4
|
-
|
4
|
+
class Preamble
|
5
|
+
|
6
|
+
DEFAULTS = {
|
7
|
+
:external_encoding => Encoding.default_external
|
8
|
+
}
|
5
9
|
|
6
|
-
|
10
|
+
attr_accessor :metadata, :content
|
7
11
|
|
8
|
-
|
9
|
-
|
12
|
+
def initialize(metadata, content)
|
13
|
+
@metadata = metadata
|
14
|
+
@content = content
|
15
|
+
end
|
10
16
|
|
11
|
-
|
17
|
+
def metadata_with_content
|
18
|
+
@metadata.to_yaml + "---\n" + @content
|
19
|
+
end
|
12
20
|
|
13
|
-
|
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|
|
14
38
|
|
15
|
-
|
39
|
+
stripped = line.strip
|
16
40
|
|
17
|
-
|
41
|
+
case state
|
18
42
|
|
19
|
-
|
43
|
+
when :before_preamble
|
20
44
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
53
|
|
30
|
-
|
54
|
+
when :preamble
|
31
55
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
56
|
+
new_state = case stripped
|
57
|
+
when "---"
|
58
|
+
:after_preamble
|
59
|
+
else
|
60
|
+
preamble_lines << line
|
61
|
+
:preamble
|
62
|
+
end
|
39
63
|
|
40
|
-
|
41
|
-
|
42
|
-
|
64
|
+
when :after_preamble
|
65
|
+
new_state = :after_preamble
|
66
|
+
content_lines << line
|
43
67
|
|
44
|
-
|
45
|
-
|
68
|
+
else
|
69
|
+
raise "Invalid State: #{ state }"
|
46
70
|
|
47
|
-
|
71
|
+
end
|
48
72
|
|
73
|
+
state = new_state
|
74
|
+
end
|
49
75
|
end
|
50
76
|
|
51
|
-
return
|
52
|
-
|
77
|
+
return new(YAML::load(preamble_lines), content_lines)
|
53
78
|
end
|
54
79
|
|
55
80
|
def self.load_multiple(*paths)
|
56
|
-
|
81
|
+
options = paths.last.is_a?(Hash) ? paths.pop : {}
|
82
|
+
paths.map{ |path| Preamble.load(path, options) }
|
57
83
|
end
|
58
84
|
|
59
85
|
end
|
data/lib/preamble/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0.
|
1
|
+
class Preamble
|
2
|
+
VERSION = "0.0.3"
|
3
3
|
end
|
data/preamble.gemspec
CHANGED
@@ -14,7 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.rubyforge_project = "preamble"
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files -- {test,spec,features}
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/**`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
+
s.add_dependency 'rspec'
|
20
21
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Preamble do
|
5
|
+
|
6
|
+
describe "#load" do
|
7
|
+
|
8
|
+
it "should parse the preamble of a single file" do
|
9
|
+
result = Preamble.load(standard)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return the result as a Preamble object" do
|
13
|
+
result = Preamble.load(standard)
|
14
|
+
expect(result).to be_a Preamble
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return a Preamble object with metadata as a Hash" do
|
18
|
+
result = Preamble.load(standard)
|
19
|
+
expect(result.metadata).to be_a Hash
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a Preamble object with content as a String" do
|
23
|
+
result = Preamble.load(standard)
|
24
|
+
expect(result.content).to be_a String
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should accept leading whitespace" do
|
28
|
+
Preamble.load(leading_whitespace)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should accept an options hash" do
|
32
|
+
Preamble.load(standard, {})
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should accept an encoding option" do
|
36
|
+
Preamble.load(standard, {:external_encoding => 'UTF-8'})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should read a UTF-8 character" do
|
40
|
+
result = Preamble.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 = Preamble.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 = Preamble.load(standard)
|
56
|
+
output_file = Tempfile.new('preamble').path
|
57
|
+
result.metadata["key1"] += "!"
|
58
|
+
result.save(output_file)
|
59
|
+
|
60
|
+
result = Preamble.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
|
+
Preamble.load_multiple(standard, leading_whitespace, unicode)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should accept options as tail arguments" do
|
72
|
+
Preamble.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
CHANGED
@@ -1,63 +1,88 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: preamble
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Starr Horne
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Allows you to add YAML front matter to your templates. Useful for adding
|
31
|
+
metadata to static pages
|
32
|
+
email:
|
19
33
|
- starr@chromahq.com
|
20
34
|
executables: []
|
21
|
-
|
22
35
|
extensions: []
|
23
|
-
|
24
36
|
extra_rdoc_files: []
|
25
|
-
|
26
|
-
files:
|
37
|
+
files:
|
27
38
|
- .gitignore
|
39
|
+
- .rspec
|
28
40
|
- Gemfile
|
29
41
|
- README.markdown
|
30
42
|
- Rakefile
|
31
43
|
- lib/preamble.rb
|
32
44
|
- lib/preamble/version.rb
|
33
45
|
- preamble.gemspec
|
34
|
-
|
46
|
+
- spec/preamble/preamble_spec.rb
|
47
|
+
- spec/sample_files.rb
|
48
|
+
- spec/sample_files/leading_whitespace.txt
|
49
|
+
- spec/sample_files/standard.txt
|
50
|
+
- spec/sample_files/unicode.txt
|
51
|
+
- spec/spec_helper.rb
|
35
52
|
homepage: https://github.com/starrhorne/preamble
|
36
53
|
licenses: []
|
37
|
-
|
38
54
|
post_install_message:
|
39
55
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
56
|
+
require_paths:
|
42
57
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
59
|
none: false
|
45
|
-
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version:
|
49
|
-
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
hash: 3440432532881689500
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
68
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version:
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: 3440432532881689500
|
55
76
|
requirements: []
|
56
|
-
|
57
77
|
rubyforge_project: preamble
|
58
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.8.23
|
59
79
|
signing_key:
|
60
80
|
specification_version: 3
|
61
81
|
summary: Use yaml preambles in your documents & templates
|
62
|
-
test_files:
|
63
|
-
|
82
|
+
test_files:
|
83
|
+
- spec/preamble/preamble_spec.rb
|
84
|
+
- spec/sample_files.rb
|
85
|
+
- spec/sample_files/leading_whitespace.txt
|
86
|
+
- spec/sample_files/standard.txt
|
87
|
+
- spec/sample_files/unicode.txt
|
88
|
+
- spec/spec_helper.rb
|