-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi_to_methods.rb
executable file
·73 lines (54 loc) · 1.28 KB
/
pi_to_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
require 'nokogiri'
require 'ripper'
require 'pp'
require 'chunker'
include Chunker
class InversionTemplate
def initialize( io )
@document = Nokogiri::HTML( io )
@attrs = {}
self.parse()
end
def parse
@document.xpath( '//processing-instruction()' ).each do |pi|
self.parse_processing_instruction( pi )
end
end
def parse_processing_instruction( pi )
puts "text is: %p" % [ pi.text ]
identifier = pi.text.strip[ /[a-z]\w+/ ]
puts "defining a method for #{identifier} "
@attrs[ identifier.to_sym ] = []
block = self.create_tag( pi.name, pi.text )
self.class.send( :define_method, identifier, &block )
self.class.send( :define_method, "#{identifier}=" ) do |arg|
@attrs[ identifier ] << arg
end
end
def create_tag( type, content )
case type
when 'call'
return eval "lambda { #{content} }", binding()
else
raise "D'oh!"
end
end
def render
@document.each do |elem|
# if elem.is_a?( Nokogiri::XML::ProcessingInstruction )
puts elem
end
end
alias_method :to_s, :render
end
template1 = InversionTemplate.new( DATA_TEMPLATE1 )
template1.foo = "woo!"
pp template1.to_s
__END__
__TEMPLATE1__
<div>
<p>something awesome</p>
<?call foo.length >
<?call person.names.select {|name| name != supervisor.lastname } ?>
</div>