yampla 0.0.1
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +149 -0
- data/Rakefile +1 -0
- data/bin/yampla +55 -0
- data/lib/yampla/build.rb +74 -0
- data/lib/yampla/system_extension.rb +7 -0
- data/lib/yampla/version.rb +3 -0
- data/lib/yampla.rb +4 -0
- data/samples/book_build.rb +10 -0
- data/samples/books.yaml +21 -0
- data/samples/index_template.html +14 -0
- data/samples/item_template.html +17 -0
- data/spec/build_spec.rb +243 -0
- data/spec/spec_helper.rb +3 -0
- data/yampla.gemspec +25 -0
- metadata +145 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 kyoendo
|
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,149 @@
|
|
1
|
+
# Yampla
|
2
|
+
|
3
|
+
Build index and each item pages from YAML data with a template engine. Liquid is used for the engine.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'yampla'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yampla
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
A simple example to create a book index page and each book pages.
|
22
|
+
|
23
|
+
###Step1. Provide book data with YAML format.
|
24
|
+
|
25
|
+
(book.yaml)
|
26
|
+
|
27
|
+
b1:
|
28
|
+
title: book1
|
29
|
+
price: 1000JPY
|
30
|
+
date: 2013-1-1
|
31
|
+
keywords:
|
32
|
+
- ruby
|
33
|
+
- beginner
|
34
|
+
b2:
|
35
|
+
title: book2
|
36
|
+
price: 1500JPY
|
37
|
+
date: 2013-2-7
|
38
|
+
keywords:
|
39
|
+
- rails
|
40
|
+
b3:
|
41
|
+
title: book3
|
42
|
+
price: 2400JPY
|
43
|
+
date: 2013-3-15
|
44
|
+
keywords:
|
45
|
+
- sinatra
|
46
|
+
- rack
|
47
|
+
|
48
|
+
###Step2. Provide an index template and a book template.
|
49
|
+
|
50
|
+
(index\_template.html)
|
51
|
+
|
52
|
+
<!DOCTYPE html>
|
53
|
+
<html>
|
54
|
+
<head>
|
55
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
56
|
+
<title>Book List</title>
|
57
|
+
</head>
|
58
|
+
<body>
|
59
|
+
<ol>
|
60
|
+
{% for item in items %}
|
61
|
+
<li id="{{ item.id }}"><a href="{{ item.id }}.html">{{ item.title }}</a></li>
|
62
|
+
{% endfor %}
|
63
|
+
</ol>
|
64
|
+
</body>
|
65
|
+
</html>
|
66
|
+
|
67
|
+
You can access books array data via **items** variable(by default) in Liquid tags. Each book properties can be accessed by method call style(ex. item.title).
|
68
|
+
|
69
|
+
(book\_template.html)
|
70
|
+
|
71
|
+
<!DOCTYPE html>
|
72
|
+
<html>
|
73
|
+
<head>
|
74
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
75
|
+
<title>{{ item.title }}</title>
|
76
|
+
</head>
|
77
|
+
<body>
|
78
|
+
<h2>{{ item.title }}</h2>
|
79
|
+
<p>{{ item.price }}</p>
|
80
|
+
<p>{{ item.date }}</p>
|
81
|
+
<div>
|
82
|
+
{% for key in item.keywords %}
|
83
|
+
<small>{{ key }}</small>
|
84
|
+
{% endfor %}
|
85
|
+
</div>
|
86
|
+
</body>
|
87
|
+
</html>
|
88
|
+
|
89
|
+
You can access each book data via **item** variable(by default) in Liquid tags.
|
90
|
+
|
91
|
+
###Step3. Write ruby code using yampla gem and run it.
|
92
|
+
|
93
|
+
(book\_build.rb)
|
94
|
+
|
95
|
+
require "yampla"
|
96
|
+
|
97
|
+
ya = Yampla::Build.new('books.yaml')
|
98
|
+
ya.set_template(:index, 'index_template.html')
|
99
|
+
ya.set_template(:items, 'book_template.html')
|
100
|
+
|
101
|
+
puts ya.run(:index)
|
102
|
+
puts ya.run(:items)
|
103
|
+
|
104
|
+
As a result, you will get index output as follows;
|
105
|
+
|
106
|
+
<!DOCTYPE html>
|
107
|
+
<html>
|
108
|
+
<head>
|
109
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
110
|
+
<title>Book List</title>
|
111
|
+
</head>
|
112
|
+
<body>
|
113
|
+
<ol>
|
114
|
+
|
115
|
+
<li id="b1"><a href="b1.html">book1</a></li>
|
116
|
+
|
117
|
+
<li id="b2"><a href="b2.html">book2</a></li>
|
118
|
+
|
119
|
+
<li id="b3"><a href="b3.html">book3</a></li>
|
120
|
+
|
121
|
+
</ol>
|
122
|
+
</body>
|
123
|
+
</html>
|
124
|
+
|
125
|
+
And get items output as hash like follows;
|
126
|
+
|
127
|
+
{"b1"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n <title>book1</title>\n </head>\n <body>\n <h2>book1</h2>\n <p>1000JPY</p>\n <p>2013-01-01</p>\n <div>\n \n <small>ruby</small>\n \n <small>beginner</small>\n \n </div>\n </body>\n</html>\n",
|
128
|
+
"b2"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n <title>book2</title>\n </head>\n <body>\n <h2>book2</h2>\n <p>1500JPY</p>\n <p>2013-02-07</p>\n <div>\n \n <small>rails</small>\n \n </div>\n </body>\n</html>\n",
|
129
|
+
"b3"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n <title>book3</title>\n </head>\n <body>\n <h2>book3</h2>\n <p>2400JPY</p>\n <p>2013-03-15</p>\n <div>\n \n <small>sinatra</small>\n \n <small>rack</small>\n \n </div>\n </body>\n</html>\n"}
|
130
|
+
|
131
|
+
|
132
|
+
To get these results as files, try #save.
|
133
|
+
|
134
|
+
ya.save(:index)
|
135
|
+
ya.save(:items)
|
136
|
+
|
137
|
+
'index.html', 'b1.html', 'b2.html' and 'b3.html' with above contents will be created at 'out' subdirectory.
|
138
|
+
|
139
|
+
## yampla command
|
140
|
+
|
141
|
+
try `yampla -t book_template.html` at the file directory. Same result will be obtained. More details, see `yampla --help`.
|
142
|
+
|
143
|
+
## Contributing
|
144
|
+
|
145
|
+
1. Fork it
|
146
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
147
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
148
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
149
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/yampla
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "yampla"
|
3
|
+
require "trollop"
|
4
|
+
|
5
|
+
class OptionParser
|
6
|
+
def self.parse!
|
7
|
+
Trollop::options do
|
8
|
+
version "yampla #{Yampla::VERSION} (c) 2013 kyoendo"
|
9
|
+
banner ~<<-EOS
|
10
|
+
Yampla is a builder to build index and each item pages from YAML data with a template engine. Liquid is used for the engine.
|
11
|
+
|
12
|
+
Prerequisite:
|
13
|
+
|
14
|
+
1. Provide item data with YAML format.
|
15
|
+
|
16
|
+
2. Provide an index template and a item template.
|
17
|
+
|
18
|
+
3. Write ruby code using yampla gem and run it.
|
19
|
+
|
20
|
+
Usage:
|
21
|
+
|
22
|
+
yampla [options]
|
23
|
+
|
24
|
+
where [options] are:
|
25
|
+
EOS
|
26
|
+
|
27
|
+
opt :yaml, "Set data YAML file", :type => :string
|
28
|
+
opt :index, "Set index template file", :default => "index_template.html"
|
29
|
+
opt :item, "Set item template file", :default => "item_template.html"
|
30
|
+
opt :out, "Output files", :default => 'all' # :index or :items
|
31
|
+
opt :dir, "Output directory", :default => 'out'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
opts = OptionParser.parse!
|
37
|
+
files = Dir['*']
|
38
|
+
|
39
|
+
yaml = opts[:yaml] || files.detect { |f| f.match /\w+\.(yaml|yml)$/ }
|
40
|
+
|
41
|
+
ya = Yampla::Build.new(yaml)
|
42
|
+
ya.set_template(:index, opts[:index])
|
43
|
+
ya.set_template(:items, opts[:item])
|
44
|
+
|
45
|
+
case opts[:out]
|
46
|
+
when 'all'
|
47
|
+
ya.save(:index, dir:opts[:dir])
|
48
|
+
ya.save(:items, dir:opts[:dir])
|
49
|
+
when 'index'
|
50
|
+
ya.save(:index, dir:opts[:dir])
|
51
|
+
when 'items'
|
52
|
+
ya.save(:items, dir:opts[:dir])
|
53
|
+
else
|
54
|
+
STDERR.puts "'out' option is wrong. 'all', 'index', 'items' are acceptable."
|
55
|
+
end
|
data/lib/yampla/build.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "hashie"
|
3
|
+
require "liquid"
|
4
|
+
|
5
|
+
class Yampla::Build
|
6
|
+
attr_reader :data
|
7
|
+
def initialize(yaml)
|
8
|
+
@template = {}
|
9
|
+
@yaml = yaml_parse(yaml)
|
10
|
+
@data = yaml2object(@yaml)
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_template(type, template)
|
14
|
+
unless [:index, :items].include?(type.intern)
|
15
|
+
raise ArgumentError, "First argument must :index or :items"
|
16
|
+
end
|
17
|
+
@template[type] = parse_template(template)
|
18
|
+
end
|
19
|
+
|
20
|
+
def run(type, opt={})
|
21
|
+
opt = {template:@template[type], name:nil}.merge(opt)
|
22
|
+
case type
|
23
|
+
when :index
|
24
|
+
build_index(opt[:template], opt[:name])
|
25
|
+
when :items
|
26
|
+
build_items(opt[:template], opt[:name])
|
27
|
+
else
|
28
|
+
raise ArgumentError, "First argument must be :index or :items."
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def build_index(template, name=nil)
|
33
|
+
Liquid::Template.parse(template).render("#{name || 'items'}" => @data)
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_items(template, name=nil)
|
37
|
+
@data.each_with_object({}) do |item, h|
|
38
|
+
h[item.id] = Liquid::Template.parse(template).render("#{name || 'item'}" => item)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def save(type, opt={})
|
43
|
+
opt = {ext:@extname, name:nil, dir:'out'}.merge(opt)
|
44
|
+
content = run(type, name:opt[:name])
|
45
|
+
ext = ".#{opt[:ext]}" if opt[:ext]
|
46
|
+
dir = opt[:dir]
|
47
|
+
Dir.mkdir(dir) unless Dir.exists?(dir)
|
48
|
+
case type
|
49
|
+
when :index
|
50
|
+
path = File.join(dir, "index#{ext}")
|
51
|
+
File.open(path, 'w') { |f| f.puts content }
|
52
|
+
when :items
|
53
|
+
content.each do |id, data|
|
54
|
+
path = File.join(dir, "#{id}#{ext}")
|
55
|
+
File.open(path, 'w') { |f| f.puts data }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def yaml_parse(yaml)
|
62
|
+
yaml.match(/\w+\.(yaml|yml)$/) ? YAML.load_file(yaml) : YAML.load(yaml)
|
63
|
+
end
|
64
|
+
|
65
|
+
def yaml2object(yaml)
|
66
|
+
yaml.map { |id, data| Hashie::Mash.new( data.dup.update(id: id) ) }
|
67
|
+
end
|
68
|
+
|
69
|
+
def parse_template(template)
|
70
|
+
File.read(template).tap { @extname = File.extname(template)[1..-1] }
|
71
|
+
rescue Errno::ENOENT
|
72
|
+
return template if template.match(/.+?\.[^.]*$/)
|
73
|
+
end
|
74
|
+
end
|
data/lib/yampla.rb
ADDED
data/samples/books.yaml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
b1:
|
2
|
+
title: book1
|
3
|
+
price: 1000JPY
|
4
|
+
date: 2013-1-1
|
5
|
+
keywords:
|
6
|
+
- ruby
|
7
|
+
- beginner
|
8
|
+
b2:
|
9
|
+
title: book2
|
10
|
+
price: 1500JPY
|
11
|
+
date: 2013-2-7
|
12
|
+
keywords:
|
13
|
+
- rails
|
14
|
+
b3:
|
15
|
+
title: book3
|
16
|
+
price: 2400JPY
|
17
|
+
date: 2013-3-15
|
18
|
+
keywords:
|
19
|
+
- sinatra
|
20
|
+
- rack
|
21
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<title>Book List</title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<ol>
|
9
|
+
{% for item in items %}
|
10
|
+
<li id="{{ item.id }}"><a href="{{ item.id }}.html">{{ item.title }}</a></li>
|
11
|
+
{% endfor %}
|
12
|
+
</ol>
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<title>{{ item.title }}</title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<h2>{{ item.title }}</h2>
|
9
|
+
<p>{{ item.price }}</p>
|
10
|
+
<p>{{ item.date }}</p>
|
11
|
+
<div>
|
12
|
+
{% for key in item.keywords %}
|
13
|
+
<small>{{ key }}</small>
|
14
|
+
{% endfor %}
|
15
|
+
</div>
|
16
|
+
</body>
|
17
|
+
</html>
|
data/spec/build_spec.rb
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Yampla::Build do
|
4
|
+
before do
|
5
|
+
@yml = ~<<-EOS
|
6
|
+
item1:
|
7
|
+
title: book1
|
8
|
+
price: 100
|
9
|
+
item2:
|
10
|
+
title: book2
|
11
|
+
price: 200
|
12
|
+
EOS
|
13
|
+
end
|
14
|
+
describe ".new" do
|
15
|
+
context "w/o an argument" do
|
16
|
+
it "raise argument error" do
|
17
|
+
expect { should }.to raise_error(ArgumentError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context "w/ yaml string" do
|
21
|
+
subject { Yampla::Build.new @yml }
|
22
|
+
it { subject.instance_variable_get(:@yaml).should ==
|
23
|
+
{'item1' => {'title' => 'book1', 'price' => 100},
|
24
|
+
'item2' => {'title' => 'book2', 'price' => 200}}
|
25
|
+
}
|
26
|
+
end
|
27
|
+
context "w/ yaml file but not exist" do
|
28
|
+
subject { Yampla::Build.new 'item.yaml' }
|
29
|
+
it "raise file not found error" do
|
30
|
+
expect { should }.to raise_error(Errno::ENOENT)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context "w/ yaml file" do
|
34
|
+
include FakeFS::SpecHelpers
|
35
|
+
FakeFS do
|
36
|
+
File.open('item.yaml', 'w') do |f|
|
37
|
+
f.puts ~<<-EOS
|
38
|
+
item1:
|
39
|
+
title: book1
|
40
|
+
EOS
|
41
|
+
end
|
42
|
+
subject { Yampla::Build.new 'item.yaml' }
|
43
|
+
it { subject.instance_variable_get(:@yaml).should ==
|
44
|
+
{'item1' => {'title' => 'book1'}} }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#data" do
|
50
|
+
subject { Yampla::Build.new(@yml).data }
|
51
|
+
it { should be_instance_of(Array) }
|
52
|
+
it { should have(2).items }
|
53
|
+
its(:first) { should be_instance_of(Hashie::Mash) }
|
54
|
+
its('first.title') { should eq 'book1' }
|
55
|
+
its('first.price') { should be 100 }
|
56
|
+
its('last.title') { should eq 'book2' }
|
57
|
+
its('last.price') { should be 200 }
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#run" do
|
61
|
+
context "for index" do
|
62
|
+
context "w/o template" do
|
63
|
+
subject { Yampla::Build.new(@yml).run(:index) }
|
64
|
+
it { should eq "" }
|
65
|
+
end
|
66
|
+
context "w/ simple template" do
|
67
|
+
subject { Yampla::Build.new(@yml).run(:index, template:~<<-EOS) }
|
68
|
+
items size: {{ items.size }}
|
69
|
+
EOS
|
70
|
+
it { should eq "items size: 2\n" }
|
71
|
+
end
|
72
|
+
context "w/ simple template using alter name for items" do
|
73
|
+
subject { Yampla::Build.new(@yml).run(:index, template:~<<-EOS, name:'books') }
|
74
|
+
items size: {{ books.size }}
|
75
|
+
EOS
|
76
|
+
it { should eq "items size: 2\n" }
|
77
|
+
end
|
78
|
+
context "w/ list template" do
|
79
|
+
subject { Yampla::Build.new(@yml).run(:index, template:~<<-EOS) }
|
80
|
+
{% for item in items %}
|
81
|
+
{{ item.id }}:{{ item.title }}({{ item.price }})
|
82
|
+
{% endfor %}
|
83
|
+
EOS
|
84
|
+
it { should eq ~<<-EOS }
|
85
|
+
|
86
|
+
item1:book1(100)
|
87
|
+
|
88
|
+
item2:book2(200)
|
89
|
+
|
90
|
+
EOS
|
91
|
+
end
|
92
|
+
end
|
93
|
+
context "for each item" do
|
94
|
+
context "w/o template" do
|
95
|
+
subject { Yampla::Build.new(@yml).run(:items) }
|
96
|
+
it { should == {'item1' => "", 'item2' => ""} }
|
97
|
+
end
|
98
|
+
context "w/ simple template" do
|
99
|
+
subject { Yampla::Build.new(@yml).run(:items, template:~<<-EOS) }
|
100
|
+
id:{{ item.id }}/title:{{ item.title }}/price:{{ item.price }}
|
101
|
+
EOS
|
102
|
+
it { should == {'item1' => "id:item1/title:book1/price:100\n",
|
103
|
+
'item2' => "id:item2/title:book2/price:200\n"} }
|
104
|
+
end
|
105
|
+
context "w/ simple template using alter name for item" do
|
106
|
+
subject { Yampla::Build.new(@yml).run(:items, template:~<<-EOS, name:'book') }
|
107
|
+
id:{{ book.id }}/title:{{ book.title }}/price:{{ book.price }}
|
108
|
+
EOS
|
109
|
+
it { should == {'item1' => "id:item1/title:book1/price:100\n",
|
110
|
+
'item2' => "id:item2/title:book2/price:200\n"} }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#set_template" do
|
116
|
+
context "with string template" do
|
117
|
+
context "for index" do
|
118
|
+
before do
|
119
|
+
@ya = Yampla::Build.new(@yml)
|
120
|
+
@ya.set_template(:index, ~<<-EOS)
|
121
|
+
items size: {{ items.size }}
|
122
|
+
EOS
|
123
|
+
end
|
124
|
+
subject { @ya.run(:index) }
|
125
|
+
it { should eq "items size: 2\n" }
|
126
|
+
end
|
127
|
+
context "for items" do
|
128
|
+
before do
|
129
|
+
@ya = Yampla::Build.new(@yml)
|
130
|
+
@ya.set_template(:items, ~<<-EOS)
|
131
|
+
id:{{ item.id }}/title:{{ item.title }}/price:{{ item.price }}
|
132
|
+
EOS
|
133
|
+
end
|
134
|
+
subject { @ya.run(:items) }
|
135
|
+
it { should == {'item1' => "id:item1/title:book1/price:100\n",
|
136
|
+
'item2' => "id:item2/title:book2/price:200\n"} }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
context "with file template" do
|
140
|
+
include FakeFS::SpecHelpers
|
141
|
+
before do
|
142
|
+
FakeFS.activate!
|
143
|
+
File.open('index_template', 'w') { |f| f.puts ~<<-EOS }
|
144
|
+
items size: {{ items.size }}
|
145
|
+
EOS
|
146
|
+
File.open('item_template', 'w') { |f| f.puts ~<<-EOS }
|
147
|
+
id:{{ item.id }}/title:{{ item.title }}/price:{{ item.price }}
|
148
|
+
EOS
|
149
|
+
end
|
150
|
+
context "for index" do
|
151
|
+
before do
|
152
|
+
@ya = Yampla::Build.new(@yml)
|
153
|
+
@ya.set_template(:index, 'index_template')
|
154
|
+
end
|
155
|
+
subject { @ya.run(:index) }
|
156
|
+
it { should eq "items size: 2\n" }
|
157
|
+
end
|
158
|
+
context "for items" do
|
159
|
+
before do
|
160
|
+
@ya = Yampla::Build.new(@yml)
|
161
|
+
@ya.set_template(:items, 'item_template')
|
162
|
+
end
|
163
|
+
subject { @ya.run(:items) }
|
164
|
+
it { should == {'item1' => "id:item1/title:book1/price:100\n",
|
165
|
+
'item2' => "id:item2/title:book2/price:200\n"} }
|
166
|
+
end
|
167
|
+
after { FakeFS.deactivate! }
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#save" do
|
172
|
+
include FakeFS::SpecHelpers
|
173
|
+
before(:all) { FakeFS.activate! }
|
174
|
+
context "for index" do
|
175
|
+
context "w/o extension" do
|
176
|
+
before do
|
177
|
+
@ya = Yampla::Build.new(@yml)
|
178
|
+
@ya.set_template(:index, ~<<-EOS)
|
179
|
+
items size: {{ items.size }}
|
180
|
+
EOS
|
181
|
+
@ya.save(:index)
|
182
|
+
end
|
183
|
+
it "save a file" do
|
184
|
+
File.open('out/index').read.should eq "items size: 2\n"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
context "w/ extension" do
|
188
|
+
before do
|
189
|
+
@ya = Yampla::Build.new(@yml)
|
190
|
+
@ya.set_template(:index, ~<<-EOS)
|
191
|
+
items size: {{ items.size }}
|
192
|
+
EOS
|
193
|
+
@ya.save(:index, ext:'txt')
|
194
|
+
end
|
195
|
+
it "save a file" do
|
196
|
+
File.open('out/index.txt').read.should eq "items size: 2\n"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
context "specify file destination" do
|
200
|
+
before do
|
201
|
+
@ya = Yampla::Build.new(@yml)
|
202
|
+
@ya.set_template(:index, ~<<-EOS)
|
203
|
+
items size: {{ items.size }}
|
204
|
+
EOS
|
205
|
+
@ya.save(:index, dir:'result')
|
206
|
+
end
|
207
|
+
it "save a file" do
|
208
|
+
File.open('result/index').read.should eq "items size: 2\n"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
context "for items" do
|
214
|
+
context "w/o extension" do
|
215
|
+
before do
|
216
|
+
@ya = Yampla::Build.new(@yml)
|
217
|
+
@ya.set_template(:items, ~<<-EOS)
|
218
|
+
id:{{ item.id }}/title:{{ item.title }}/price:{{ item.price }}
|
219
|
+
EOS
|
220
|
+
@ya.save(:items)
|
221
|
+
end
|
222
|
+
it "save a files" do
|
223
|
+
File.open('out/item1').read.should eq "id:item1/title:book1/price:100\n"
|
224
|
+
File.open('out/item2').read.should eq "id:item2/title:book2/price:200\n"
|
225
|
+
end
|
226
|
+
end
|
227
|
+
context "w extension" do
|
228
|
+
before do
|
229
|
+
@ya = Yampla::Build.new(@yml)
|
230
|
+
@ya.set_template(:items, ~<<-EOS)
|
231
|
+
id:{{ item.id }}/title:{{ item.title }}/price:{{ item.price }}
|
232
|
+
EOS
|
233
|
+
@ya.save(:items, ext:'txt')
|
234
|
+
end
|
235
|
+
it "save a files" do
|
236
|
+
File.open('out/item1.txt').read.should eq "id:item1/title:book1/price:100\n"
|
237
|
+
File.open('out/item2.txt').read.should eq "id:item2/title:book2/price:200\n"
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
after(:all) { FakeFS.deactivate! }
|
242
|
+
end
|
243
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/yampla.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yampla/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "yampla"
|
8
|
+
gem.version = Yampla::VERSION
|
9
|
+
gem.authors = ["kyoendo"]
|
10
|
+
gem.email = ["postagie@gmail.com"]
|
11
|
+
gem.description = %q{Build List & Item pages from YAML data with a template engine}
|
12
|
+
gem.summary = %q{Build List & Item pages from YAML data with a template engine}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.required_ruby_version = '>=1.9.3'
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'fakefs'
|
22
|
+
gem.add_dependency 'hashie'
|
23
|
+
gem.add_dependency 'liquid'
|
24
|
+
gem.add_dependency 'trollop'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yampla
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kyoendo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-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: :development
|
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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: fakefs
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: hashie
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: liquid
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: trollop
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Build List & Item pages from YAML data with a template engine
|
95
|
+
email:
|
96
|
+
- postagie@gmail.com
|
97
|
+
executables:
|
98
|
+
- yampla
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- bin/yampla
|
108
|
+
- lib/yampla.rb
|
109
|
+
- lib/yampla/build.rb
|
110
|
+
- lib/yampla/system_extension.rb
|
111
|
+
- lib/yampla/version.rb
|
112
|
+
- samples/book_build.rb
|
113
|
+
- samples/books.yaml
|
114
|
+
- samples/index_template.html
|
115
|
+
- samples/item_template.html
|
116
|
+
- spec/build_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- yampla.gemspec
|
119
|
+
homepage: ''
|
120
|
+
licenses: []
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 1.9.3
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.8.23
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: Build List & Item pages from YAML data with a template engine
|
143
|
+
test_files:
|
144
|
+
- spec/build_spec.rb
|
145
|
+
- spec/spec_helper.rb
|