cadenza 0.7.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cadenza.rb +57 -0
- data/lib/cadenza/base_renderer.rb +24 -0
- data/lib/cadenza/blocks/standard_blocks.rb +9 -0
- data/lib/cadenza/context.rb +192 -0
- data/lib/cadenza/filesystem_loader.rb +28 -0
- data/lib/cadenza/filters/standard_filters.rb +97 -0
- data/lib/cadenza/functional_variables/standard_functional_variables.rb +14 -0
- data/lib/cadenza/lexer.rb +191 -0
- data/lib/cadenza/nodes/block_node.rb +20 -0
- data/lib/cadenza/nodes/boolean_inverse_node.rb +17 -0
- data/lib/cadenza/nodes/constant_node.rb +21 -0
- data/lib/cadenza/nodes/document_node.rb +27 -0
- data/lib/cadenza/nodes/filter_node.rb +25 -0
- data/lib/cadenza/nodes/for_node.rb +37 -0
- data/lib/cadenza/nodes/generic_block_node.rb +21 -0
- data/lib/cadenza/nodes/if_node.rb +42 -0
- data/lib/cadenza/nodes/inject_node.rb +34 -0
- data/lib/cadenza/nodes/operation_node.rb +66 -0
- data/lib/cadenza/nodes/text_node.rb +17 -0
- data/lib/cadenza/nodes/variable_node.rb +21 -0
- data/lib/cadenza/parser.rb +942 -0
- data/lib/cadenza/text_renderer.rb +90 -0
- data/lib/cadenza/token.rb +6 -0
- data/lib/cadenza/version.rb +11 -0
- metadata +158 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
module Cadenza
|
4
|
+
class TextRenderer < BaseRenderer
|
5
|
+
def self.render(template, context)
|
6
|
+
io = StringIO.new
|
7
|
+
new(io).render(template, context)
|
8
|
+
io.string
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_document(node, context, blocks)
|
12
|
+
if node.extends
|
13
|
+
# merge the inherited blocks onto this document's blocks to
|
14
|
+
# determine what to pass to the layout template
|
15
|
+
blocks = node.blocks.merge(blocks)
|
16
|
+
|
17
|
+
# load the template of the document and render it to the same output stream
|
18
|
+
template = context.load_template!(node.extends)
|
19
|
+
|
20
|
+
document = template
|
21
|
+
|
22
|
+
render(template, context, blocks)
|
23
|
+
else
|
24
|
+
node.children.each {|x| render(x, context, blocks) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def render_block(node, context, blocks)
|
29
|
+
(blocks[node.name] || node).children.each {|x| render(x, context) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def render_text(node, context, blocks)
|
33
|
+
output << node.text
|
34
|
+
end
|
35
|
+
|
36
|
+
def render_inject(node, context, blocks)
|
37
|
+
output << node.evaluate(context).to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def render_if(node, context, blocks)
|
41
|
+
node.evaluate_expression_for_children(context).each {|x| render(x, context) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def render_for(node, context, blocks)
|
45
|
+
# sadly to_enum doesn't work in 1.8.x so we need to array-ify the iterable first
|
46
|
+
values = node.iterable.eval(context).to_a
|
47
|
+
iterator = node.iterator.identifier
|
48
|
+
|
49
|
+
values.each_with_index do |value, counter|
|
50
|
+
is_first = (counter == 0) ? true : false
|
51
|
+
is_last = (counter == values.length - 1) ? true : false
|
52
|
+
|
53
|
+
# push the inner context with the 'magic' variables
|
54
|
+
context.push({
|
55
|
+
iterator => value,
|
56
|
+
'forloop' => {
|
57
|
+
'counter' => counter + 1,
|
58
|
+
'counter0' => counter,
|
59
|
+
'first' => is_first,
|
60
|
+
'last' => is_last
|
61
|
+
}
|
62
|
+
})
|
63
|
+
|
64
|
+
# render each of the child nodes with the context
|
65
|
+
node.children.each {|x| render(x, context) }
|
66
|
+
|
67
|
+
# pop the inner context off
|
68
|
+
context.pop
|
69
|
+
|
70
|
+
# increment the counter
|
71
|
+
counter += 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def render_generic_block(node, context, blocks)
|
76
|
+
output << context.evaluate_block(node.identifier, node.children, node.parameters)
|
77
|
+
end
|
78
|
+
|
79
|
+
# none of these should appear directly inside the body of the
|
80
|
+
# document but for safety we will render them anyways
|
81
|
+
def render_constant(node, context, blocks)
|
82
|
+
output << node.eval(context).to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
alias :render_variable :render_constant
|
86
|
+
alias :render_operation :render_constant
|
87
|
+
alias :render_boolean_inverse :render_constant
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cadenza
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0.rc1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- William Howard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70394600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70394600
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70394140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.6.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70394140
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: racc
|
38
|
+
requirement: &70393800 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.4.6
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70393800
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: nokogiri-diff
|
49
|
+
requirement: &70393560 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70393560
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: yard
|
60
|
+
requirement: &70393250 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70393250
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pygments.rb
|
71
|
+
requirement: &70392920 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70392920
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: guard-rspec
|
82
|
+
requirement: &70392690 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70392690
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: guard-shell
|
93
|
+
requirement: &70392460 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70392460
|
102
|
+
description:
|
103
|
+
email:
|
104
|
+
- whoward.tke@gmail.com
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files: []
|
108
|
+
files:
|
109
|
+
- lib/cadenza/nodes/generic_block_node.rb
|
110
|
+
- lib/cadenza/nodes/operation_node.rb
|
111
|
+
- lib/cadenza/nodes/filter_node.rb
|
112
|
+
- lib/cadenza/nodes/if_node.rb
|
113
|
+
- lib/cadenza/nodes/text_node.rb
|
114
|
+
- lib/cadenza/nodes/variable_node.rb
|
115
|
+
- lib/cadenza/nodes/document_node.rb
|
116
|
+
- lib/cadenza/nodes/for_node.rb
|
117
|
+
- lib/cadenza/nodes/boolean_inverse_node.rb
|
118
|
+
- lib/cadenza/nodes/inject_node.rb
|
119
|
+
- lib/cadenza/nodes/constant_node.rb
|
120
|
+
- lib/cadenza/nodes/block_node.rb
|
121
|
+
- lib/cadenza/token.rb
|
122
|
+
- lib/cadenza/functional_variables/standard_functional_variables.rb
|
123
|
+
- lib/cadenza/parser.rb
|
124
|
+
- lib/cadenza/filters/standard_filters.rb
|
125
|
+
- lib/cadenza/text_renderer.rb
|
126
|
+
- lib/cadenza/filesystem_loader.rb
|
127
|
+
- lib/cadenza/blocks/standard_blocks.rb
|
128
|
+
- lib/cadenza/base_renderer.rb
|
129
|
+
- lib/cadenza/version.rb
|
130
|
+
- lib/cadenza/lexer.rb
|
131
|
+
- lib/cadenza/context.rb
|
132
|
+
- lib/cadenza.rb
|
133
|
+
homepage: http://github.com/whoward/Cadenza
|
134
|
+
licenses: []
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ! '>'
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 1.3.1
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 1.8.10
|
154
|
+
signing_key:
|
155
|
+
specification_version: 3
|
156
|
+
summary: Powerful text templating language similar to Smarty/Liquid/Django
|
157
|
+
test_files: []
|
158
|
+
has_rdoc:
|