sbuilder-ial 0.0.1

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,142 @@
1
+ require 'mustache'
2
+
3
+
4
+ # ------------------------------------------------------------------
5
+ # Hack mustache to allow change default otag '{{' ctag '}}'
6
+
7
+ class Mustache
8
+
9
+
10
+ # Open mustache to define class accessors 'otag' and 'ctag'
11
+ def self.otag=( o )
12
+ @@otag = o
13
+ end
14
+ def self.ctag=( c )
15
+ @@ctag = c
16
+ end
17
+ def self.otag
18
+ @@otag ||= '{{'
19
+ end
20
+ def self.ctag
21
+ @@ctag ||= '}}'
22
+ end
23
+
24
+
25
+ class Template
26
+
27
+ # Open template to set otag/ctag on parser.
28
+ #
29
+ # Returns an array of tokens for a given template.
30
+ #
31
+ # @return [Array] Array of tokens.
32
+ #
33
+ def tokens(src = @source)
34
+ p = Parser.new
35
+ p.otag = Mustache.otag
36
+ p.ctag = Mustache.ctag
37
+ p.compile(src)
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ module Sbuilder
44
+ module Ial
45
+ module Action
46
+ module Render
47
+ class Renderer < Mustache
48
+
49
+ # @attr [Logger] loger
50
+ attr_reader :logger
51
+ PROGNAME = nil # progname for logger default class name
52
+ include Sbuilder::Ial::MyLogger # mix logger
53
+
54
+ # @attr [String:Array] partials must be set before +to_str+
55
+ attr_accessor :partials
56
+
57
+ # @attr [Hash] preferences modify mustache rendering (debug?)
58
+ attr_reader :preferences
59
+
60
+ def initialize( options={} )
61
+ # exception raise if accessing unknown property
62
+ self.raise_on_context_miss = true
63
+
64
+ @options = options
65
+ @logger = getLogger( nil, options )
66
+ @logger.info "#{__method__}: starting options=#{options}"
67
+
68
+ self.partials = {}
69
+
70
+ end
71
+
72
+ def self.start( options = {} )
73
+ renderer = Renderer.new( options )
74
+ renderer
75
+ end
76
+
77
+ def setPreferences( preferences )
78
+ @preferences = preferences
79
+ @logger.info "#{__method__}: preferences=#{preferences}"
80
+ end
81
+
82
+ # Use 'to_str' instead of 'render' to use non-default otag/ctag's
83
+ def to_str( template, data=nil )
84
+ Mustache.otag = '{|'
85
+ Mustache.ctag = '|}'
86
+
87
+ begin
88
+ ret = render( template, data )
89
+ ensure
90
+ # restore default otag/ctag
91
+ Mustache.otag = '{{'
92
+ Mustache.ctag = '}}'
93
+ end
94
+ ret
95
+ end
96
+
97
+ # Render conxtext.current[key].
98
+
99
+ # @param [String, '.'] key to render, '.' means current context,
100
+ # string lookup key in current context
101
+ def EVAL( key )
102
+
103
+ logger.info "#{__method__}: key='#{key}', context.current=#{context.current}" if logger.debug?
104
+
105
+ # find generate sexp( :sym, data ) to render
106
+ if key == '.'
107
+ sexp = context.current
108
+ else
109
+ # to evaluate
110
+ hash = context.current.is_a?( Array ) ? context.current[1] : context.current
111
+ sexp = hash[key] || hash[key.to_sym]
112
+ raise MustacheException, "No such key #{key}:#{key.class} found in #{hash}" if sexp.nil?
113
+ end
114
+
115
+ sexp_type = sexp[0]
116
+ data = sexp[1]
117
+ logger.debug "#{__method__}: key=#{key}, sexp_type=#{sexp_type}" if logger.debug?
118
+
119
+
120
+ # find correct template
121
+ template = partials[sexp_type]
122
+ raise MustacheException,
123
+ <<-EOS if (template.nil?)
124
+ Unknown partial for sexp_type '#{sexp_type}'
125
+
126
+ Known partials: #{partials.keys.join(',')}"
127
+ Conxt = #{context.current}
128
+ EOS
129
+
130
+ logger.debug "#{__method__}: template=#{template}, data=#{data}" if logger.debug?
131
+ return render( template, data )
132
+
133
+ end
134
+
135
+
136
+
137
+ end
138
+
139
+ end
140
+ end
141
+ end
142
+ end