rcurse 0.4.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/Gemfile +4 -1
- data/README.md +36 -9
- data/Rakefile +14 -0
- data/lib/rcurse.rb +3 -2
- data/lib/rcurse/context.rb +18 -0
- data/lib/rcurse/engine.rb +33 -19
- data/lib/rcurse/helper.rb +10 -0
- data/lib/rcurse/helpers/include.rb +8 -3
- data/rcurse.gemspec +1 -1
- data/spec-data/hello.html +2 -0
- data/spec-data/included.html +1 -0
- data/spec-data/includes.html +1 -0
- data/spec/context_spec.rb +16 -0
- data/spec/engine_spec.rb +35 -0
- data/spec/helper_spec.rb +8 -0
- data/spec/include_spec.rb +10 -0
- metadata +12 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be3bc122cddb4add5b5937f1cfd815c021429b61
|
4
|
+
data.tar.gz: d99e9fe4564abdeb690aced53bd66ba682a5231e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0029c067c75d50fb5a8703b56d0de9855aad1a194b40c43b65573ce217a8d40e3c00facc2c3c7bbe5029a5d76679c6d017db70f66a22efb92776c242c3a2fa38
|
7
|
+
data.tar.gz: a338c47357085a4ffe5129fb6634bada1ad5db9f7f2dff28ba69eb8f2269c6068b13df223a02f5235af19fab8cf189fc6f9a8e6c0bf548c13ee2bcabdfb04e68
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,37 +6,64 @@ Install it with `gem install rcurse`,
|
|
6
6
|
|
7
7
|
### Using Bundler
|
8
8
|
|
9
|
-
add `gem
|
9
|
+
add `gem "rcurse"` to your `Gemfile` and run `bundle install`
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
13
|
Require it with `require 'rcurse'`.
|
14
|
-
Call `Rcurse
|
14
|
+
Call `Rcurse::render "file.rcurse"` to render `file.rcurse` and return the rendered file contents, you can then write it to a file.
|
15
15
|
|
16
|
-
###
|
16
|
+
### Usage example
|
17
17
|
|
18
18
|
Let's assume that we have file names `index.rcurse` and want to render it to `index.html`
|
19
19
|
|
20
20
|
```ruby
|
21
21
|
require 'rcurse'
|
22
|
-
Rcurse
|
22
|
+
Rcurse::render_file("index.rcurse", "index.html")
|
23
23
|
```
|
24
24
|
|
25
|
+
## Evaluating code
|
26
|
+
|
27
|
+
Wrap your Ruby code in `{%` and `%}` to evaluate it. You can use it to set variables or do other stuff. This doesn't output anything to the template, if you want to output the result of your code, use `{%=` instead of `{%`, like `{%= "trololo".upcase %}`.
|
28
|
+
|
29
|
+
### Examples
|
30
|
+
|
31
|
+
Using variables:
|
32
|
+
|
33
|
+
```html
|
34
|
+
{% name = "Hodor" %}
|
35
|
+
|
36
|
+
<h1>Hello, {%= name %}!</h1>
|
37
|
+
```
|
38
|
+
|
39
|
+
Renders as:
|
40
|
+
|
41
|
+
```html
|
42
|
+
<h1>Hello, Hodor!</h1>
|
43
|
+
```
|
44
|
+
|
45
|
+
## Context
|
46
|
+
|
47
|
+
Context contains a binding for template variables, and a path of current template if Rcurse is rendering a file.
|
48
|
+
Use `context.eval` to evaluate Ruby code.
|
49
|
+
|
50
|
+
`context.eval "a = 1"`
|
51
|
+
|
52
|
+
See `context.path` for the path of currently rendered template.
|
53
|
+
|
25
54
|
## Helpers
|
26
55
|
|
27
|
-
Helpers are
|
56
|
+
Helpers are like functions you can call in your templates. Use them by inserting `{{helper-name arguments}}` in source files.
|
28
57
|
|
29
58
|
### Builtin helpers
|
30
59
|
- `include filename` - include another file, like so: `{{include header.rcurse}}`
|
31
60
|
|
32
61
|
### Adding a helper
|
33
62
|
|
34
|
-
Use `Rcurse::Helper.new(name) {
|
35
|
-
Use `Rcurse.add_helper(helper)` to add your helper to Rcurse.
|
63
|
+
Use `Rcurse::Helper.new(name) { |args, context| do_something }` to create a helper, your block can accept a single array of arguments and a context.
|
36
64
|
|
37
65
|
```ruby
|
38
|
-
my_helper = Rcurse::Helper.new("my_helper") do |arguments|
|
66
|
+
my_helper = Rcurse::Helper.new("my_helper") do |arguments, context|
|
39
67
|
# do something with arguments and return a string containing the new content
|
40
68
|
end
|
41
|
-
Rcurse.add_helper(my_helper)
|
42
69
|
```
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
project_root = File.dirname(File.absolute_path(__FILE__))
|
2
|
+
rcurse_path = project_root + "/lib/rcurse.rb"
|
3
|
+
|
4
|
+
task :spec do
|
5
|
+
system "rspec --color -f d -r #{rcurse_path}"
|
6
|
+
end
|
7
|
+
|
8
|
+
task :clean do
|
9
|
+
Dir.glob(project_root + "/spec-data/*.out") do |f|
|
10
|
+
File.delete f
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:spec, :clean]
|
data/lib/rcurse.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
project_root = File.dirname(File.absolute_path(__FILE__))
|
2
2
|
|
3
|
-
require "rcurse/engine"
|
4
|
-
require "rcurse/helper"
|
3
|
+
require project_root + "/rcurse/engine.rb"
|
4
|
+
require project_root + "/rcurse/helper.rb"
|
5
|
+
require project_root + "/rcurse/context.rb"
|
5
6
|
|
6
7
|
Dir.glob(project_root + '/rcurse/helpers/*.rb', &method(:require))
|
data/lib/rcurse/engine.rb
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
module Rcurse
|
2
|
-
|
2
|
+
class Engine
|
3
|
+
def self.render content, context = Context.new
|
4
|
+
content.gsub /{({|%=|%)(.+?)[}|%]}/ do |s|
|
5
|
+
result = s
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
+
case $1
|
8
|
+
when "{"
|
9
|
+
s = $2.chomp.split(" ")
|
10
|
+
name = s[0]
|
11
|
+
args = s[1..s.length]
|
12
|
+
if Rcurse::helpers[name].is_a? Rcurse::Helper then
|
13
|
+
result = Rcurse::helpers[name].callback.call(args, context)
|
14
|
+
end
|
15
|
+
when "%"
|
16
|
+
context.eval($2)
|
17
|
+
result = ""
|
18
|
+
when "%="
|
19
|
+
result = context.eval($2)
|
20
|
+
end
|
7
21
|
|
8
|
-
|
9
|
-
|
10
|
-
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
11
25
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
26
|
+
def self.render_file filename, out_filename, context = Context.new
|
27
|
+
content = File.read(filename)
|
28
|
+
context.path = File.dirname(filename)
|
29
|
+
rendered_content = self.render(content, context)
|
30
|
+
File.open(out_filename, "w+") do |file|
|
31
|
+
file.write(rendered_content)
|
18
32
|
end
|
19
33
|
end
|
20
34
|
end
|
21
35
|
|
22
|
-
def self.
|
23
|
-
content
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
36
|
+
def self.render content, context = Context.new
|
37
|
+
Rcurse::Engine.render content, context
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.render_file filename, out_filename, context = Context.new
|
41
|
+
Rcurse::Engine.render_file filename, out_filename, context
|
28
42
|
end
|
29
43
|
end
|
data/lib/rcurse/helper.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
-
Rcurse
|
2
|
-
|
3
|
-
|
1
|
+
Rcurse::Helper.new("include") do |args, context|
|
2
|
+
if context.path then
|
3
|
+
file_path = File.expand_path(args[0], context.path)
|
4
|
+
else
|
5
|
+
file_path = args[0]
|
6
|
+
end
|
7
|
+
Rcurse::render(File.read(file_path), context)
|
8
|
+
end
|
data/rcurse.gemspec
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
hello world
|
@@ -0,0 +1 @@
|
|
1
|
+
{{include included.html}}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
describe Rcurse::Context do
|
2
|
+
it "initializes" do
|
3
|
+
context = Rcurse::Context.new
|
4
|
+
expect(context).to be_a(Rcurse::Context)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns binding" do
|
8
|
+
context = Rcurse::Context.new
|
9
|
+
expect(context.get_binding).to be_a(Binding)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns the same binding every time" do
|
13
|
+
context = Rcurse::Context.new
|
14
|
+
expect(context.get_binding).to eq(context.get_binding)
|
15
|
+
end
|
16
|
+
end
|
data/spec/engine_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
describe Rcurse::Engine do
|
2
|
+
it "renders an empty template" do
|
3
|
+
result = Rcurse::render("")
|
4
|
+
expect(result).to eq("")
|
5
|
+
end
|
6
|
+
|
7
|
+
it "renders a template with text" do
|
8
|
+
result = Rcurse::render("Hello World")
|
9
|
+
expect(result).to eq("Hello World")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "renders a template with unicode text" do
|
13
|
+
result = Rcurse::render("გამარჯობა")
|
14
|
+
expect(result).to eq("გამარჯობა")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "renders a hello world file" do
|
18
|
+
file_path = File.expand_path("../../spec-data/hello.html", __FILE__)
|
19
|
+
out_file_path = File.expand_path("../../spec-data/hello.html.out", __FILE__)
|
20
|
+
|
21
|
+
result = Rcurse::render_file(file_path, out_file_path)
|
22
|
+
|
23
|
+
expect(File.new out_file_path).to be_a(File)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "renders {% nil %} as empty"do
|
27
|
+
result = Rcurse::render("{% nil %}")
|
28
|
+
expect(result).to eq("")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "renders {%= 1 %} as 1"do
|
32
|
+
result = Rcurse::render("{%=1 %}")
|
33
|
+
expect(result).to eq("1")
|
34
|
+
end
|
35
|
+
end
|
data/spec/helper_spec.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "include helper" do
|
2
|
+
it "includes another template" do
|
3
|
+
file_path = File.expand_path("../../spec-data/includes.html", __FILE__)
|
4
|
+
out_file_path = File.expand_path("../../spec-data/includes.html.out", __FILE__)
|
5
|
+
|
6
|
+
result = Rcurse::render_file(file_path, out_file_path)
|
7
|
+
|
8
|
+
expect(File.read out_file_path).to eq("hello world")
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rcurse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Shvelidze
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Super simple template engine.
|
14
14
|
email: captain@pirrate.me
|
@@ -21,12 +21,21 @@ files:
|
|
21
21
|
- Gemfile
|
22
22
|
- LICENSE
|
23
23
|
- README.md
|
24
|
+
- Rakefile
|
24
25
|
- bin/rcurse
|
25
26
|
- lib/rcurse.rb
|
27
|
+
- lib/rcurse/context.rb
|
26
28
|
- lib/rcurse/engine.rb
|
27
29
|
- lib/rcurse/helper.rb
|
28
30
|
- lib/rcurse/helpers/include.rb
|
29
31
|
- rcurse.gemspec
|
32
|
+
- spec-data/hello.html
|
33
|
+
- spec-data/included.html
|
34
|
+
- spec-data/includes.html
|
35
|
+
- spec/context_spec.rb
|
36
|
+
- spec/engine_spec.rb
|
37
|
+
- spec/helper_spec.rb
|
38
|
+
- spec/include_spec.rb
|
30
39
|
homepage: http://shvelo.github.io/rcurse
|
31
40
|
licenses:
|
32
41
|
- MIT
|
@@ -47,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
56
|
version: '0'
|
48
57
|
requirements: []
|
49
58
|
rubyforge_project:
|
50
|
-
rubygems_version: 2.
|
59
|
+
rubygems_version: 2.2.2
|
51
60
|
signing_key:
|
52
61
|
specification_version: 4
|
53
62
|
summary: Super simple template engine.
|