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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 170b6db184dd6e7179927e347cbd615f7388c494
4
- data.tar.gz: 200b0db10151a10214ceb3bc4fab2516e085d1db
3
+ metadata.gz: be3bc122cddb4add5b5937f1cfd815c021429b61
4
+ data.tar.gz: d99e9fe4564abdeb690aced53bd66ba682a5231e
5
5
  SHA512:
6
- metadata.gz: a3730771a339db49d5cba3de902ac72fa0f79fa3cc8715c5b07ac0d26db7f7e45e058cee342a5f1ae87f7a1b3e442082be6a6656f7d0c50e732565d9ae964334
7
- data.tar.gz: ed7529da098310db75ed6e03689b10a1dc248960b285e94a2ce9bf5f1af145643cc25928bb51a29a2908d24dea07e4de4b04267d0f2771605ea626b1f2d522f6
6
+ metadata.gz: 0029c067c75d50fb5a8703b56d0de9855aad1a194b40c43b65573ce217a8d40e3c00facc2c3c7bbe5029a5d76679c6d017db70f66a22efb92776c242c3a2fa38
7
+ data.tar.gz: a338c47357085a4ffe5129fb6634bada1ad5db9f7f2dff28ba69eb8f2269c6068b13df223a02f5235af19fab8cf189fc6f9a8e6c0bf548c13ee2bcabdfb04e68
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
- *.gem
1
+ *.gem
2
+ Gemfile.lock
3
+ spec-data/*.out
data/Gemfile CHANGED
@@ -1 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rake"
4
+ gem "rspec"
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 :rcurse` to your `Gemfile` and run `bundle install`
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.render "file.rcurse"` to render `file.rcurse` and return the rendered file contents, you can then write it to a file.
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
- ### Complete example
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.render_file("index.rcurse", "index.html")
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 the only functionality Rcurse provides yet. Use them by inserting `{{helper-name arguments}}` in source files.
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) { block }` to create a helper, your block can accept a single array of arguments.
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
  ```
@@ -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]
@@ -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))
@@ -0,0 +1,18 @@
1
+ module Rcurse
2
+ class Context
3
+ def initialize path = nil
4
+ @binding = binding
5
+ @path = path
6
+ end
7
+
8
+ def get_binding
9
+ @binding
10
+ end
11
+
12
+ def eval code
13
+ @binding.eval code
14
+ end
15
+
16
+ attr_accessor :path
17
+ end
18
+ end
@@ -1,29 +1,43 @@
1
1
  module Rcurse
2
- @helpers = Hash.new
2
+ class Engine
3
+ def self.render content, context = Context.new
4
+ content.gsub /{({|%=|%)(.+?)[}|%]}/ do |s|
5
+ result = s
3
6
 
4
- def self.add_helper helper
5
- @helpers[helper.name] = helper
6
- end
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
- def self.helpers
9
- @helpers
10
- end
22
+ result
23
+ end
24
+ end
11
25
 
12
- def self.render content
13
- content.gsub /{{([^ ]+) *(.+)?}}/ do |s|
14
- name = $1
15
- args = $2 ? $2.split(" ") : []
16
- if @helpers[name].is_a? Rcurse::Helper then
17
- @helpers[name].callback.call(args)
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.render_file filename, out_filename
23
- content = File.read(filename)
24
- rendered_content = self.render(content)
25
- File.open(out_filename, "w+") do |file|
26
- file.write(rendered_content)
27
- end
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
@@ -1,4 +1,14 @@
1
1
  module Rcurse
2
+ @helpers = Hash.new
3
+
4
+ def self.add_helper helper
5
+ @helpers[helper.name] = helper
6
+ end
7
+
8
+ def self.helpers
9
+ @helpers
10
+ end
11
+
2
12
  class Helper
3
13
  attr_accessor :name
4
14
  attr_accessor :callback
@@ -1,3 +1,8 @@
1
- Rcurse.add_helper(Rcurse::Helper.new("include") do |args|
2
- Rcurse::render File.read(args[0])
3
- end)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rcurse'
3
- s.version = '0.4.0'
3
+ s.version = '0.5.1'
4
4
  s.licenses = ['MIT']
5
5
  s.summary = "Super simple template engine."
6
6
  s.description = "Super simple template engine."
@@ -0,0 +1,2 @@
1
+ <!DOCTYPE html>
2
+ <title>Hello World</title>
@@ -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
@@ -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
@@ -0,0 +1,8 @@
1
+ describe Rcurse::Helper do
2
+ it "Initializes with dummy name and callback" do
3
+ helper = Rcurse::Helper.new("dummy") {}
4
+ expect(helper).to be_a(Rcurse::Helper)
5
+ expect(helper.name).to eq("dummy")
6
+ expect(helper.callback).to be_a(Proc)
7
+ end
8
+ end
@@ -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.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-01-28 00:00:00.000000000 Z
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.1.11
59
+ rubygems_version: 2.2.2
51
60
  signing_key:
52
61
  specification_version: 4
53
62
  summary: Super simple template engine.