preamble 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -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 an array. The first part is your data, the second is the body conent.
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
- [ { "key1" => "value1", "key2" => [1, 2, 3] }, "\nYour body content goes here"]
50
+ "\nYour body content goes here"
34
51
 
35
52
 
36
53
  Notes
@@ -1,59 +1,85 @@
1
- require "preamble/version"
1
+ require 'preamble/version'
2
2
  require 'yaml'
3
3
 
4
- module Preamble
4
+ class Preamble
5
+
6
+ DEFAULTS = {
7
+ :external_encoding => Encoding.default_external
8
+ }
5
9
 
6
- def self.load(path)
10
+ attr_accessor :metadata, :content
7
11
 
8
- preamble_lines = []
9
- body_lines = []
12
+ def initialize(metadata, content)
13
+ @metadata = metadata
14
+ @content = content
15
+ end
10
16
 
11
- state = :before_preamble
17
+ def metadata_with_content
18
+ @metadata.to_yaml + "---\n" + @content
19
+ end
12
20
 
13
- open(path).each do |line|
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
- stripped = line.strip
39
+ stripped = line.strip
16
40
 
17
- case state
41
+ case state
18
42
 
19
- when :before_preamble
43
+ when :before_preamble
20
44
 
21
- new_state = case stripped
22
- when "---"
23
- :preamble
24
- when ""
25
- :before_preamble
26
- else
27
- raise "First line must begin with ---"
28
- end
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
- when :preamble
54
+ when :preamble
31
55
 
32
- new_state = case stripped
33
- when "---"
34
- :after_preamble
35
- else
36
- preamble_lines << line
37
- :preamble
38
- end
56
+ new_state = case stripped
57
+ when "---"
58
+ :after_preamble
59
+ else
60
+ preamble_lines << line
61
+ :preamble
62
+ end
39
63
 
40
- when :after_preamble
41
- new_state = :after_preamble
42
- body_lines << line
64
+ when :after_preamble
65
+ new_state = :after_preamble
66
+ content_lines << line
43
67
 
44
- else raise "Invalid State: #{ state }"
45
- end
68
+ else
69
+ raise "Invalid State: #{ state }"
46
70
 
47
- state = new_state
71
+ end
48
72
 
73
+ state = new_state
74
+ end
49
75
  end
50
76
 
51
- return [YAML::load(preamble_lines.join), body_lines.join]
52
-
77
+ return new(YAML::load(preamble_lines), content_lines)
53
78
  end
54
79
 
55
80
  def self.load_multiple(*paths)
56
- paths.map{ |path| Preamble.load(path) }
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
@@ -1,3 +1,3 @@
1
- module Preamble
2
- VERSION = "0.0.2"
1
+ class Preamble
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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}/*`.split("\n")
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
@@ -0,0 +1,6 @@
1
+
2
+ ---
3
+ key1: value1
4
+ ---
5
+
6
+ Your body content goes here.
@@ -0,0 +1,5 @@
1
+ ---
2
+ key1: value1
3
+ ---
4
+
5
+ Your body content goes here.
@@ -0,0 +1,5 @@
1
+ ---
2
+ unicode_tm: ™
3
+ ---
4
+
5
+ Your body content goes here.
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/preamble'
2
+ require_relative 'sample_files'
3
+
4
+ RSpec.configure do |c|
5
+ c.include SampleFiles
6
+ end
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
- date: 2011-06-26 00:00:00 -05:00
14
- default_executable:
15
- dependencies: []
16
-
17
- description: Allows you to add YAML front matter to your templates. Useful for adding metadata to static pages
18
- email:
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
- has_rdoc: true
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: "0"
49
- required_rubygems_version: !ruby/object:Gem::Requirement
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: "0"
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.6.2
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