amrita2 1.9.6

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.
@@ -0,0 +1,40 @@
1
+ require 'amrita2/template'
2
+
3
+ module Amrita2View
4
+ class Base
5
+ @@compiled_amrita2_templates = {}
6
+
7
+ def initialize( action_view )
8
+ @action_view = action_view
9
+ @action_view.extend(Amrita2::Runtime)
10
+ end
11
+
12
+ def render( template, local_assigns={} )
13
+ action_name = @action_view.controller.action_name
14
+ tmpl_method = action_name + "_setup_template"
15
+ tmpl = @@compiled_amrita2_templates[template] ||=
16
+ if @action_view.respond_to?(tmpl_method)
17
+ @action_view.send(tmpl_method, template)
18
+ else
19
+ t = Amrita2::TemplateText.new(template)
20
+ b = @action_view.instance_eval { binding }
21
+ t.use_erb(b)
22
+ t
23
+ end
24
+
25
+ po_method = action_name + "_po"
26
+ po = nil
27
+ if @action_view.respond_to?(po_method)
28
+ po = @action_view.send(po_method)
29
+ else
30
+ po = @action_view.controller
31
+ end
32
+
33
+ tmpl.expand(out="", po)
34
+ out
35
+ rescue
36
+ $!.to_s + $@.join("<br>")
37
+ end
38
+ end
39
+ end
40
+
data/lib/amrita2/rd.rb ADDED
@@ -0,0 +1,314 @@
1
+ require "rd/rd2html-lib"
2
+ require 'rd/rdfmt'
3
+ require 'amrita2/template'
4
+
5
+ module Amrita2
6
+ include Amrita2
7
+ RDTmpl = TemplateText.new <<-END
8
+ <html>
9
+ <head id='header'>
10
+ <title id='title' />
11
+ <link id='css' rel="stylesheet" type="text/css" />
12
+ <meta id='lang' content="text/html; charset=euc-jp" http-equiv="content-type" />
13
+ </head>
14
+ <body id='document'>
15
+ <span id='output'></span>
16
+ <div id='block'>
17
+ <h1 id='header1'> header</h1>
18
+ <h2 id='header2'> header</h2>
19
+ <h3 id='header3'> header</h3>
20
+ <p id='textblock'> </p>
21
+ <ul id='list'><li id='item'></li></ul>
22
+ <ol id='enumlist'><li id='item'></li></ol>
23
+ <dl id='desclist'><dt id='term' /><dd id='description' /></dl>
24
+ </div>
25
+ <div id='inline'>
26
+ <em id='emphasis'>*<span id='text'></span>*</em>
27
+ <code id='code'><span id='text'></span></code>
28
+ <var id='var'><span id='text'></span></var>
29
+ <kbd id='kbd'><span id='text'></span></kbd>
30
+ <a id='link_to_url'></a>
31
+ </div>
32
+ </body>
33
+ </html>
34
+ END
35
+ RDTmpl.define_parts(:block=>[:header1, :header2, :header3, :textblock,
36
+ { :list=>:item },
37
+ { :enumlist=>:item } ,
38
+ { :desclist=>[:term, :description] } ],
39
+ :inline=>[:emphasis, :code, :var, :kbd, :link_to_url])
40
+
41
+ class Amrita2Visitor < RD::RD2HTMLVisitor
42
+ include Amrita2
43
+ include Amrita2::Runtime
44
+ include Amrita2::Core
45
+
46
+ attr_accessor :option
47
+ def initialize(doctmpl, blocktmpl, inlinetmpl, extention_processor, option=nil)
48
+ @doctmpl, @blocktmpl, @inlinetmpl, @extention_processor =
49
+ doctmpl, blocktmpl, inlinetmpl, extention_processor
50
+ @option = option
51
+ end
52
+
53
+ def visit_DocumentElement(element)
54
+ @doctmpl.expand(out="") do |m|
55
+ if @option
56
+ m.header do |mh|
57
+ mh.title(@option[:title]) if @option[:title]
58
+ mh.css(nil,:href=>@option[:css]) if @option[:css]
59
+ mh.lang(nil,:content=>"text/html; charset=#{@option[:lang]}") if @option[:lang]
60
+ end
61
+ end
62
+ m.document do |md|
63
+ md.output(visit_children(element))
64
+ end
65
+ end
66
+ out
67
+ end
68
+
69
+ def visit_children(element)
70
+ SanitizedString[super.to_s]
71
+ end
72
+
73
+ def visit_TextBlock(element)
74
+ if (is_this_textblock_only_one_block_of_parent_listitem?(element) or
75
+ is_this_textblock_only_one_block_other_than_sublists_in_parent_listitem?(element))
76
+ visit_children(element).gsub(/\s/, " ")
77
+ else
78
+ @blocktmpl[:textblock][visit_children(element)]
79
+ end
80
+ end
81
+ HeadlineMethods = [
82
+ nil,
83
+ :header1,
84
+ :header2,
85
+ :header3,
86
+ :header4,
87
+ :header5,
88
+ :header6,
89
+ nil
90
+ ]
91
+
92
+ def visit_Headline(element)
93
+ c = visit_children(element)
94
+ @blocktmpl[HeadlineMethods[element.level]][c]
95
+ end
96
+
97
+ def visit_ItemList(element)
98
+ @blocktmpl[:list][visit_children(element)]
99
+ end
100
+
101
+ def visit_ItemListItem(element)
102
+ @blocktmpl[:list][:item][visit_children(element)]
103
+ end
104
+
105
+ def visit_EnumList(element)
106
+ @blocktmpl[:enumlist][visit_children(element)]
107
+ end
108
+
109
+ def apply_to_EnumListItem(element, content)
110
+ @blocktmpl[:enumlist][:item][content]
111
+ end
112
+
113
+ def visit_DescList(element)
114
+ @blocktmpl[:desclist][visit_children(element)]
115
+ end
116
+
117
+ def apply_to_DescListItem(element, term, description)
118
+ @blocktmpl[:desclist][:term][term] +
119
+ @blocktmpl[:desclist][:description][description]
120
+ end
121
+
122
+ def visit_StringElement(element)
123
+ element.to_label.amrita_sanitize
124
+ end
125
+
126
+ def visit_Emphasis(element)
127
+ @inlinetmpl[:emphasis][:text=>visit_children(element)]
128
+ end
129
+
130
+ def visit_Code(element)
131
+ @inlinetmpl[:code][:text=>visit_children(element)]
132
+ end
133
+
134
+ def visit_Var(element)
135
+ @inlinetmpl[:var][:text=>visit_children(element)]
136
+ end
137
+
138
+ def visit_Keyboard(element)
139
+ #@inlinetmpl[:kbd][:text=>visit_children(element)]
140
+ name, *a = visit_children(element).split(' ')
141
+ process_ext(name, a)
142
+ end
143
+
144
+ def visit_Reference(element)
145
+ case element.label
146
+ when RD::Reference::RDLabel
147
+ name, *a = element.to_label.split(':')
148
+ process_ext(name, *a)
149
+ when RD::Reference::URL
150
+ @inlinetmpl[:link_to_url][
151
+ a(:href=>element.label.url) do
152
+ visit_children(element)
153
+ end
154
+ ]
155
+ else
156
+ raise "can't happen"
157
+ end
158
+ end
159
+
160
+ def apply_to_MethodListItem(element, term, content)
161
+ name, *args = split_MethodListparams(term)
162
+ args.unshift content
163
+ @extention_processor.send(name.intern, args)
164
+ end
165
+
166
+ def split_MethodListparams(term)
167
+ ret = []
168
+ while term.size > 0
169
+ case term
170
+ when /^\s*\"([^"]+)\"(.*)/ #'"
171
+ ret << $1
172
+ term = $2
173
+ when /^\s*(\S+)(.*)/
174
+ ret << $1
175
+ term = $2
176
+ else
177
+ raise 'can not happen'
178
+ end
179
+ end
180
+ ret
181
+ end
182
+
183
+ def apply_to_MethodList(element, items)
184
+ items
185
+ end
186
+
187
+ def process_ext(name, *args)
188
+ @extention_processor.send(name.intern, *args)
189
+ end
190
+ end
191
+
192
+ class RDAStandardExtention
193
+ include Amrita2
194
+ include Amrita2::Core
195
+ include Amrita2::Runtime
196
+ extend Amrita2::Runtime
197
+
198
+ EXTTMPL = TemplateText.new <<-END
199
+ <div>
200
+ <a id='rubytalk' href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/$1">[ruby-talk:$1]</a>
201
+ <a id='raa' href="http://raa.ruby-lang.org/list.rhtml?name=$1">[$2]</a>]
202
+ <div id='include'><pre>$1$0</pre></div>
203
+ <amrita:erb id='blockquote'>
204
+ <blockquote cite='<%= $_[1] %>'>
205
+ <p><%= $_[0] %></p><p>(<a href='<%= $_[1] %>'><%= $_[2] %></a>)</p>
206
+ </blockquote>
207
+ </amrita:erb>
208
+ </div>
209
+ END
210
+
211
+ EXTTMPL.use_erb(binding)
212
+ EXTTMPL.element_option[:rubytalk] = :use_args
213
+ EXTTMPL.element_option[:raa] = :use_args
214
+ EXTTMPL.element_option[:include] = :use_args
215
+ EXTTMPL.define_parts(:rubytalk, :raa, :blockquote, :include)
216
+
217
+ def initialize(exttmpl=nil)
218
+ @exttmpl = (exttmpl || EXTTMPL)
219
+ end
220
+
221
+ def table(name, cols, *args)
222
+ content = yield
223
+ cols = cols.to_i
224
+ items = content.split
225
+ ret = self.send(name) do |mt|
226
+ mt.header do |mh|
227
+ cols.times do |i|
228
+ mh.header_item(items.shift)
229
+ end
230
+ end
231
+ lines = items.size/cols
232
+ lines.times do |i|
233
+ mt.detail(i) do |md|
234
+ md.header_item(items.shift)
235
+ (cols-1).times do |j|
236
+ md.item(items.shift)
237
+ end
238
+ end
239
+ end
240
+ end
241
+ ret
242
+ end
243
+
244
+ def include(name, title=name)
245
+ super("--- #{title} ---\n") { File::open(name).read }
246
+ end
247
+
248
+ def method_missing(sym , *arg, &block)
249
+ arg = t(*arg)
250
+ tmpl = @exttmpl[sym]
251
+ raise "extention template element #{sym} not found" unless tmpl
252
+ tmpl.expand(out="") do |m|
253
+ meth = m.method(sym)
254
+ begin
255
+ meth.call(*arg, &block)
256
+ end
257
+ end
258
+ end
259
+
260
+ def method_missing_x(sym , *arg, &block)
261
+ arg = t(*arg)
262
+ tmpl = @exttmpl[sym]
263
+ raise "extention template element #{sym} not found" unless tmpl
264
+ if block_given?
265
+ if arg.size == 0
266
+ tmpl.expand(out="") do |m|
267
+ m.send(sym, arg.first, &block)
268
+ end
269
+ out
270
+ else
271
+ tmpl.expand(out="") do |m|
272
+ m.send(sym, *arg, &block)
273
+ end
274
+ out
275
+ end
276
+ else
277
+ if arg.size == 0
278
+ tmpl[arg.first]
279
+ else
280
+ tmpl[arg]
281
+ end
282
+ end
283
+ end
284
+ end
285
+
286
+ class RDAProcessor
287
+ include Amrita2
288
+ def self.compile_rd(path, out=nil, opts={})
289
+ out = path + ".html" unless out
290
+ css = opts[:css]
291
+ File::open(path) do |i|
292
+ File::open(out, "w") do |out|
293
+ src = i.read
294
+ ret = Amrita2::RDAProcessor.new(opts).process(src, opts)
295
+ out.print ret
296
+ end
297
+ end
298
+ end
299
+
300
+ def initialize(opts={})
301
+ doc = opts[:doc_template] || RDTmpl
302
+ block = opts[:block_template] || RDTmpl[:block]
303
+ inline = opts[:inline_template] || RDTmpl[:inline]
304
+ ext = opts[:extention_processor] || RDAStandardExtention.new(opts[:exttmpl])
305
+ @visitor = Amrita2Visitor.new(doc, block, inline, ext)
306
+ end
307
+
308
+ def process(rdtext, opt=nil)
309
+ tree = RD::RDTree.new("=begin\n#{rdtext}\n=end\n\n")
310
+ @visitor.option = opt
311
+ @visitor.visit(tree)
312
+ end
313
+ end
314
+ end
@@ -0,0 +1,304 @@
1
+ require 'amrita2/core'
2
+
3
+ module Amrita2
4
+ class Template
5
+ include Amrita2
6
+
7
+ # Map amrita:type to element_option
8
+ DefaultTypeMap = {
9
+ :use_args => { :use_args => true},
10
+ :use_original_element=> { :use_original_element => true},
11
+ :expand_by_member => { :expand_by_member => true },
12
+ :erb => { :erb => true, :use_args=>true },
13
+ :link => { :link_attr => [:href, [/javascript:.*/]] },
14
+ :input => { :value_attr => :value },
15
+ :checkbox => { :value_attr => :checked },
16
+ :encoding_euc => { :encoding=>'EUC-JP' }
17
+ }
18
+
19
+ # keep id attribute of output if set.
20
+ attr_accessor :keep_id
21
+ # The name of attribute that will be used for template expandion by amrita.
22
+ # You will need to set this if you use _id_ attribute fom DOM.
23
+ # For expample, if this was set to "amrita_id",
24
+ # you can use amrita_id for amrita and id for DOM.
25
+ attr_accessor :amrita_id
26
+
27
+ attr_accessor :encoding, :spec, :ie_hack, :debug_source, :eval_cdata_as_erb # :nodoc:
28
+
29
+ attr_accessor :type_map, :parts_id # :nodoc:
30
+
31
+ attr_reader :mod, :element_option, :root # :nodoc:
32
+
33
+
34
+ def initialize(&block)
35
+ @keep_id = false
36
+ @amrita_id = nil
37
+ @setuped = false
38
+ @spec = nil
39
+ @spec_proc = block
40
+ @type_map = DefaultTypeMap.clone
41
+ @ie_hack = false
42
+ @eval_cdata_as_erb = false
43
+ @element_option = Hash.new
44
+ @parts_template = Hash.new
45
+ @parts_id = nil
46
+ @encoding = $KCODE
47
+ @debug_source = nil
48
+ end
49
+
50
+ def setup
51
+ return if @setuped
52
+ setup_document
53
+ @spec = get_spec
54
+ @mod = @spec.create_template_module(@rexml_doc, @rexml_doc)
55
+ @expander = get_expander
56
+ @setuped = true
57
+ end
58
+
59
+ def setup_document
60
+ return if @root
61
+ @rexml_doc =
62
+ if @eval_cdata_as_erb
63
+ t = get_document_text
64
+ t.gsub!(/<amrita:erb *id='(\w+)'>/, "<span id='\\1'><![CDATA[" )
65
+ t.gsub!('</amrita:erb>', ']]></span>')
66
+ REXML::Document.new(t)
67
+ else
68
+ get_document
69
+ end
70
+ @root = @rexml_doc.root
71
+ end
72
+
73
+ def get_root_option
74
+ ret = {
75
+ :delete_id => (not @keep_id),
76
+ :key_attr_name=>( @amrita_id or :id),
77
+ :ie_hack => @ie_hack,
78
+ :eval_cdata_as_erb => @eval_cdata_as_erb,
79
+ :debug_source => @debug_source,
80
+ }
81
+ case @encoding
82
+ when "EUC"
83
+ ret[:encoding]='EUC-JP'
84
+ when "SJIS"
85
+ ret[:encoding]='SHIFT_JIS'
86
+ when "UTF8"
87
+ ret[:encoding]='UTF-8'
88
+ when "NONE"
89
+ else
90
+ raise "unkonw encoding #{@encoding}"
91
+ end
92
+ ret
93
+ end
94
+
95
+ def get_spec
96
+ if @spec
97
+ @spec
98
+ elsif @spec_proc
99
+ @spec = @spec_proc.call
100
+ else
101
+ @spec = make_default_spec(@root, @type_map, get_root_option, @element_option)
102
+ end
103
+ @spec = @spec.clone unless @spec.cloned
104
+ @spec
105
+ end
106
+
107
+ def get_expander
108
+ @spec.generate_expander(@mod)
109
+ end
110
+
111
+ def use_erb(b=TOPLEVEL_BINDING)
112
+ @eval_cdata_as_erb = b
113
+ @simple_cdata = true
114
+ end
115
+
116
+ def define_parts(*parts_ids)
117
+ setup_document
118
+ parts_ids.each do |pid|
119
+ case pid
120
+ when Array
121
+ pid.each { |id| define_parts(id) }
122
+ when Symbol
123
+ make_parts(@root, pid)
124
+ raise "parts element #{pid} not found" unless @parts_template[pid]
125
+ when Hash
126
+ pid.each do |k, v|
127
+ define_parts(k)
128
+ @parts_template[k].define_parts(v) unless v == true
129
+ end
130
+ else
131
+ raise "#{pid.class} not supported"
132
+ end
133
+ end
134
+ end
135
+
136
+ def make_parts(element, parts_id)
137
+ element.each_element do |e|
138
+ if e.attributes['id'] == parts_id.to_s
139
+ e.parent.delete(e)
140
+ pt = @parts_template[parts_id] = TemplateElement.new(e)
141
+ pt.element_option[parts_id]= element_option[parts_id]
142
+ pt.parts_id = parts_id
143
+ pt.debug_source = debug_source
144
+ pt.use_erb(@eval_cdata_as_erb) if @eval_cdata_as_erb
145
+ else
146
+ make_parts(e, parts_id)
147
+ end
148
+ end
149
+ end
150
+
151
+ def [](x)
152
+ case x
153
+ when Symbol
154
+ @parts_template[x]
155
+ else
156
+ x = { parts_id=>x} if parts_id
157
+ expand(out="", x)
158
+ Amrita2::SanitizedString[out]
159
+ end
160
+ end
161
+
162
+ def expand(out, data=nil, &block)
163
+ setup unless @setuped
164
+ set_binding(@eval_cdata_as_erb) if @eval_cdata_as_erb.kind_of?(Binding)
165
+ if data
166
+ @expander.expand_template(out, data)
167
+ else
168
+ @mod.expand_template(out, &block)
169
+ end
170
+ end
171
+
172
+ def prettyprint=(f)
173
+ warn 'prettyprint is not supported'
174
+ end
175
+
176
+ def compact_space=(f)
177
+ warn 'compact_space is not supported'
178
+ end
179
+
180
+ def escaped_id=(f)
181
+ warn 'escaped_id is not supported'
182
+ end
183
+
184
+ def xml=(f)
185
+ warn 'xml option is not supported'
186
+ end
187
+
188
+ def asxml=(f)
189
+ warn 'asxml option is not supported'
190
+ end
191
+
192
+ def pre_format=(f)
193
+ warn 'pre_format option is not supported'
194
+ end
195
+
196
+ def use_compiler=(f)
197
+ warn 'use_compiler option is not supported'
198
+ end
199
+
200
+ def get_document
201
+ nil
202
+ end
203
+
204
+ def make_default_spec(root, type_map, option, element_option)
205
+ s = Core::RootSpec.new(option.clone)
206
+ make_default_spec1(root, option, [s], type_map, element_option)
207
+ s
208
+ end
209
+
210
+ def make_default_spec1(e, option, spec_stack, type_map, element_option)
211
+ key_attr = option[:key_attr_name].to_s
212
+ id_ = e.attributes[key_attr]
213
+ if id_
214
+ sym = id_.intern
215
+ opt = get_option(e, option, type_map, element_option, sym)
216
+ s = Core::DynamicElementSpec.new(sym, nil, opt)
217
+ s.parent = spec_stack.first
218
+ spec_stack.first.children << s
219
+ spec_stack.unshift(s)
220
+ e.elements.each do |c|
221
+ make_default_spec1(c, option, spec_stack, type_map, element_option)
222
+ end
223
+ spec_stack.shift
224
+ spec_stack.first.check_double_id if option[:check_double_id]
225
+ else
226
+ e.elements.each do |c|
227
+ make_default_spec1(c, option, spec_stack, type_map, element_option)
228
+ end
229
+ end
230
+ end
231
+
232
+ def get_option(e, option, type_map, element_option, sym)
233
+ ret = option.clone
234
+ a = e.attributes
235
+ typ = (a['amrita:type'] || element_option[sym])
236
+ case typ
237
+ when Symbol, String
238
+ default_opt = type_map[typ.intern] || { typ.intern=>true }
239
+ ret.merge!(default_opt) if default_opt
240
+ a.delete('amrita:type')
241
+ when Hash
242
+ default_opt = typ
243
+ ret.merge!(default_opt) if default_opt
244
+ a.delete('amrita:type')
245
+ end
246
+ if opt = a['amrita:option']
247
+ ret.merge!(eval("{#{opt} }"))
248
+ a.delete('amrita:option')
249
+ end
250
+ ret
251
+ end
252
+ end
253
+
254
+ class TemplateFile < Template
255
+ include Amrita2
256
+ def initialize(fname, &block)
257
+ super(&block)
258
+ @fname = fname
259
+ end
260
+
261
+ def get_document
262
+ REXML::Document.new(File.new(@fname))
263
+ end
264
+
265
+ def get_document_text
266
+ File.new(@fname).read
267
+ end
268
+ end
269
+
270
+ class TemplateText < Template
271
+ include Amrita2
272
+ def initialize(text, &block)
273
+ super(&block)
274
+ @text = text
275
+ end
276
+
277
+ def get_document
278
+ REXML::Document.new(@text)
279
+ end
280
+
281
+ def get_document_text
282
+ @text
283
+ end
284
+ end
285
+
286
+ class TemplateElement < Template
287
+ def initialize(e, &block)
288
+ super(&block)
289
+ @element = e
290
+ end
291
+
292
+ def get_document
293
+ doc = REXML::Document.new
294
+ doc << @element
295
+ doc
296
+ end
297
+
298
+ def get_document_text
299
+ doc = REXML::Document.new
300
+ doc << @element
301
+ doc.to_s
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,9 @@
1
+ module Amrita2
2
+ module Version
3
+ MAJOR=1
4
+ MINOR=9
5
+ TINY=6
6
+
7
+ STRING=[MAJOR,MINOR,TINY].join('.')
8
+ end
9
+ end
data/lib/amrita2.rb ADDED
@@ -0,0 +1 @@
1
+ require 'amrita2/template'
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: amrita2
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.9.6
7
+ date: 2005-12-07 00:00:00 +09:00
8
+ summary: Amrita2 is a a xml/xhtml template library for Ruby
9
+ require_paths:
10
+ - lib
11
+ email: tnakajima@brain-tokyo.jp
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: amrita2
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ authors:
30
+ - Taku Nakajima
31
+ files:
32
+ - lib/amrita2.rb
33
+ - lib/amrita2/template.rb
34
+ - lib/amrita2/core.rb
35
+ - lib/amrita2/rd.rb
36
+ - lib/amrita2/rails_bridge.rb
37
+ - lib/amrita2/version.rb
38
+ test_files: []
39
+ rdoc_options: []
40
+ extra_rdoc_files: []
41
+ executables: []
42
+ extensions: []
43
+ requirements: []
44
+ dependencies: []