jam 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +21 -0
- data/MANIFEST +52 -0
- data/README +39 -0
- data/TODO +22 -0
- data/bin/jam +0 -0
- data/demo/array.rb +16 -0
- data/demo/attribute.rb +24 -0
- data/demo/demi/fixtures/ex1.html +10 -0
- data/demo/demi/fixtures/samples.yaml +19 -0
- data/demo/demi/integration/01_jam.rd +41 -0
- data/demo/demi/integration/02_xpath.rd +41 -0
- data/demo/demi/integration/samples.rd +28 -0
- data/demo/demi/unit/hpricot.rd +150 -0
- data/demo/demi/unit/libxml.rd +75 -0
- data/demo/demi/unit/nokogiri.rd +148 -0
- data/demo/demi/unit/rexml.rd +63 -0
- data/demo/hash.rb +20 -0
- data/demo/helloworld.rb +12 -0
- data/demo/object.rb +19 -0
- data/demo/table.rb +24 -0
- data/js/jquery.jam.js +230 -0
- data/lib/jam.rb +2 -0
- data/lib/jam/cherry.rb +1 -0
- data/lib/jam/css2xpath.rb +104 -0
- data/lib/jam/css_to_xpath.rb +132 -0
- data/lib/jam/engine.rb +169 -0
- data/lib/jam/hpricot.rb +137 -0
- data/lib/jam/libxml.rb +151 -0
- data/lib/jam/nokogiri.rb +167 -0
- data/lib/jam/rexml.rb +135 -0
- data/lib/jam/template.rb +224 -0
- data/meta/abstract +2 -0
- data/meta/author +1 -0
- data/meta/created +1 -0
- data/meta/homepage +1 -0
- data/meta/package +1 -0
- data/meta/project +1 -0
- data/meta/status +1 -0
- data/meta/summary +1 -0
- data/meta/timestamp +1 -0
- data/meta/title +1 -0
- data/meta/version +1 -0
- metadata +103 -0
data/lib/jam/template.rb
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
require 'jam/nokogiri'
|
2
|
+
require 'jam/libxml'
|
3
|
+
|
4
|
+
module Jam
|
5
|
+
|
6
|
+
# Jam Templates are data-driven templates. They take
|
7
|
+
# valid XML/XHTML documents and expand them based on
|
8
|
+
# the data structures provided.
|
9
|
+
#
|
10
|
+
#--
|
11
|
+
# TODO: Should rendered output have document headers like <?xml or a doctype for HTML.
|
12
|
+
#++
|
13
|
+
class Template
|
14
|
+
DEFAULT_ADAPTER = 'Nokogiri'
|
15
|
+
|
16
|
+
attr :engine
|
17
|
+
#attr :adapter
|
18
|
+
#attr :file
|
19
|
+
|
20
|
+
# New template from source file.
|
21
|
+
#
|
22
|
+
# * +file+ is the file location of a source.
|
23
|
+
# * +adapter+ is either :nokogiri, :libxml or :rexml.
|
24
|
+
# * +options+ are per-adapter and passed through to the underlying adapter's parser
|
25
|
+
#
|
26
|
+
def self.load(file, adapter=DEFAULT_ADAPTER, *options)
|
27
|
+
new(File.read(file), adapter, *options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# New template from source string.
|
31
|
+
#
|
32
|
+
# * +source+ can be either a String or an IO object.
|
33
|
+
# * +adapter+ is either :nokogiri, :libxml or :rexml.
|
34
|
+
# * +options+ are per-adapter and passed through to the underlying adapter's parser
|
35
|
+
#
|
36
|
+
def initialize(source, adapter=DEFAULT_ADAPTER, *options)
|
37
|
+
#@engine = Engine.new(adapter)
|
38
|
+
@engine = Jam.const_get(adapter.to_s).new(*options)
|
39
|
+
@source = source
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
def document
|
44
|
+
@document ||= engine.document(@source)
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# TODO: Should we render as doc, this might add a header, or render as root where it does not.
|
49
|
+
#
|
50
|
+
def render(data)
|
51
|
+
engine.interpolate(document, data).root.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
alias_method :expand, :render
|
55
|
+
|
56
|
+
#
|
57
|
+
def to_s
|
58
|
+
document.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
=begin
|
63
|
+
# Interpolate data into document, returning the document object.
|
64
|
+
#
|
65
|
+
def interpolate(data)
|
66
|
+
interpolate_node(data, document)
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
def jam(data)
|
71
|
+
interpolate_node(data, document)
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
# Converts plain keys to css selectors.
|
78
|
+
#
|
79
|
+
# TODO: Change to use special attribute instead of 'id'?
|
80
|
+
#
|
81
|
+
def prepare_data(data)
|
82
|
+
d = {}
|
83
|
+
data.each do |key, val|
|
84
|
+
case key.to_s
|
85
|
+
when /^<(.*?)>(.*)$/
|
86
|
+
d["#{$1}#{$2}"] = val
|
87
|
+
when /^\w/
|
88
|
+
d["##{key}"] = val
|
89
|
+
else
|
90
|
+
d[key.to_s] = val
|
91
|
+
end
|
92
|
+
end
|
93
|
+
return d
|
94
|
+
end
|
95
|
+
|
96
|
+
# Interpolate data.
|
97
|
+
#
|
98
|
+
def interpolate_node(data, node)
|
99
|
+
case data
|
100
|
+
when nil
|
101
|
+
adapter.remove(node)
|
102
|
+
when Array
|
103
|
+
interpolate_sequence(node, data)
|
104
|
+
when String, Numeric
|
105
|
+
interpolate_scalar(node, data)
|
106
|
+
when Hash
|
107
|
+
interpolate_mapping(node, data)
|
108
|
+
else
|
109
|
+
interpolate_object(node, data)
|
110
|
+
end
|
111
|
+
return node
|
112
|
+
end
|
113
|
+
|
114
|
+
# Interpolate object.
|
115
|
+
#
|
116
|
+
def interpolate_object(node, data)
|
117
|
+
data = Hash[data.instance_variables.map{ |iv| [iv[1..-1], data.instance_variable_get(iv)] }]
|
118
|
+
interpolate_mapping(node, data)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Interpolate mapping.
|
122
|
+
#
|
123
|
+
def interpolate_mapping(node, data)
|
124
|
+
data = prepare_data(data)
|
125
|
+
|
126
|
+
data.each do |id, val|
|
127
|
+
att = false
|
128
|
+
qry = id.to_s
|
129
|
+
|
130
|
+
result = qry.match(/^((.*?)\/)?([@](.*?))$/);
|
131
|
+
if result
|
132
|
+
att = result[4]
|
133
|
+
qry = result[2] #+ '[@' + att + ']'
|
134
|
+
else
|
135
|
+
att = false
|
136
|
+
end
|
137
|
+
|
138
|
+
if att
|
139
|
+
#qry = qry + '[@' + att + ']';
|
140
|
+
#search(node,qry).attr(att,val);
|
141
|
+
if qry
|
142
|
+
#if tag == false
|
143
|
+
# qry = '#' + qry
|
144
|
+
#end
|
145
|
+
set = adapter.search(node, qry)
|
146
|
+
adapter.attribute(set, att, val)
|
147
|
+
else
|
148
|
+
adapter.attribute(node, att, val)
|
149
|
+
end
|
150
|
+
else
|
151
|
+
set = adapter.search(node, qry)
|
152
|
+
if set.size > 0
|
153
|
+
interpolate_node(val, set)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Interpolate attribute.
|
160
|
+
#
|
161
|
+
#def interpolate_attribute(nodes, data)
|
162
|
+
#
|
163
|
+
#end
|
164
|
+
|
165
|
+
# Interpolate array sequence.
|
166
|
+
#
|
167
|
+
def interpolate_sequence(nodes, data)
|
168
|
+
adapter.each_node(nodes) do |node|
|
169
|
+
parent = node.parent
|
170
|
+
data.each do |new_data|
|
171
|
+
new_node = interpolate_node(new_data, node.dup)
|
172
|
+
parent << new_node
|
173
|
+
node.remove
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# Interpolate value.
|
179
|
+
#
|
180
|
+
# TODO This should have some special HTML features ?
|
181
|
+
#
|
182
|
+
# TODO Should we have two modes --one with and one without the extra HTML features?
|
183
|
+
#
|
184
|
+
def interpolate_scalar(nodes, data)
|
185
|
+
#var all_special = new Array;
|
186
|
+
|
187
|
+
# text inputs
|
188
|
+
#var special = this.find('input[@type=text]');
|
189
|
+
#special.val(data.toString());
|
190
|
+
#all_special.concat(special);
|
191
|
+
# textarea
|
192
|
+
# TODO
|
193
|
+
|
194
|
+
#this.not(special).empty();
|
195
|
+
#this.not(special).append(data.toString());
|
196
|
+
#alert(data);
|
197
|
+
#adapter.empty(node)
|
198
|
+
adapter.replace(nodes, data.to_s)
|
199
|
+
end
|
200
|
+
=end
|
201
|
+
|
202
|
+
end #class Template
|
203
|
+
|
204
|
+
end #module Jam
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
=begin
|
209
|
+
# Interpolate list.
|
210
|
+
|
211
|
+
function interpolate_list(node, data) {
|
212
|
+
temp = node.copy(true)
|
213
|
+
node.empty!
|
214
|
+
for (var d in data) {
|
215
|
+
nc = temp.copy(true);
|
216
|
+
interpolate_children( nc, d )
|
217
|
+
for (var c in nc.children) {
|
218
|
+
node.add(c);
|
219
|
+
};
|
220
|
+
};
|
221
|
+
};
|
222
|
+
=end
|
223
|
+
|
224
|
+
|
data/meta/abstract
ADDED
data/meta/author
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Trans <transfire@gmail.com>
|
data/meta/created
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2008-12-16
|
data/meta/homepage
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
http://rubyworks.github.com/jam
|
data/meta/package
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jam
|
data/meta/project
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rubyworks
|
data/meta/status
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
beta
|
data/meta/summary
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Jam is a data-driven template system.
|
data/meta/timestamp
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2009-06-14 17:24:46
|
data/meta/title
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Jam
|
data/meta/version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jam
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-23 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Jam is a data-driven templater system. Jam provides 100% sepearation between logic and presentation. Jam can operate on the back-end via Ruby or on the front-end via Javascript/jQuery off-loading some of the back-end work load, while also providing 100% SOC
|
17
|
+
email:
|
18
|
+
executables:
|
19
|
+
- jam
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- MANIFEST
|
25
|
+
- TODO
|
26
|
+
- HISTORY
|
27
|
+
files:
|
28
|
+
- bin/jam
|
29
|
+
- demo/array.rb
|
30
|
+
- demo/attribute.rb
|
31
|
+
- demo/demi/fixtures/ex1.html
|
32
|
+
- demo/demi/fixtures/samples.yaml
|
33
|
+
- demo/demi/integration/01_jam.rd
|
34
|
+
- demo/demi/integration/02_xpath.rd
|
35
|
+
- demo/demi/integration/samples.rd
|
36
|
+
- demo/demi/unit/hpricot.rd
|
37
|
+
- demo/demi/unit/libxml.rd
|
38
|
+
- demo/demi/unit/nokogiri.rd
|
39
|
+
- demo/demi/unit/rexml.rd
|
40
|
+
- demo/hash.rb
|
41
|
+
- demo/helloworld.rb
|
42
|
+
- demo/object.rb
|
43
|
+
- demo/table.rb
|
44
|
+
- js/jquery.jam.js
|
45
|
+
- lib/jam/cherry.rb
|
46
|
+
- lib/jam/css2xpath.rb
|
47
|
+
- lib/jam/css_to_xpath.rb
|
48
|
+
- lib/jam/engine.rb
|
49
|
+
- lib/jam/hpricot.rb
|
50
|
+
- lib/jam/libxml.rb
|
51
|
+
- lib/jam/nokogiri.rb
|
52
|
+
- lib/jam/rexml.rb
|
53
|
+
- lib/jam/template.rb
|
54
|
+
- lib/jam.rb
|
55
|
+
- meta/abstract
|
56
|
+
- meta/author
|
57
|
+
- meta/created
|
58
|
+
- meta/homepage
|
59
|
+
- meta/package
|
60
|
+
- meta/project
|
61
|
+
- meta/status
|
62
|
+
- meta/summary
|
63
|
+
- meta/timestamp
|
64
|
+
- meta/title
|
65
|
+
- meta/version
|
66
|
+
- TODO
|
67
|
+
- README
|
68
|
+
- HISTORY
|
69
|
+
- MANIFEST
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://rubyworks.github.com/jam
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --inline-source
|
77
|
+
- --title
|
78
|
+
- jam api
|
79
|
+
- --main
|
80
|
+
- README
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: "0"
|
88
|
+
version:
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
version:
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: jam
|
98
|
+
rubygems_version: 1.3.5
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Jam is a data-driven template system.
|
102
|
+
test_files: []
|
103
|
+
|