Class: Egalite::HTMLTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/egalite/template.rb

Direct Known Subclasses

CSRFTemplate

Constant Summary collapse

RE_NEST =
/<(group|if|unless)\s+name=['"](.+?)['"]>/i
RE_ENDNEST =
/<\/(group|if|unless)>/i
RE_PLACE =
/&=([.-_0-9a-zA-Z]+?);/
RE_INPUT =
/<input\s+(.+?)>/im
RE_SELECT =
/<select\s+name\s*=\s*['"](.+?)['"](.*?)>\s*<\/select>/im
RE_A =
/<a\s+(.+?)>/im
RE_FORM =
/<form\s+(.+?)>/im
RE_INCLUDE =
/<include\s+(.+?)\/>/i
RE_PARENT =
/<parent\s+name=['"](.+?)['"]\s*\/>/i
RE_YIELD =
/<yield\/>/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTMLTemplate

Returns a new instance of HTMLTemplate.



25
26
27
28
# File 'lib/egalite/template.rb', line 25

def initialize
  @default_escape = true
  @controller = nil
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



23
24
25
# File 'lib/egalite/template.rb', line 23

def controller
  @controller
end

#default_escapeObject

Returns the value of attribute default_escape.



23
24
25
# File 'lib/egalite/template.rb', line 23

def default_escape
  @default_escape
end

Instance Method Details

#escapeHTML(s) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/egalite/template.rb', line 212

def escapeHTML(s)
  if RUBY_VERSION < '1.9.0'
    s.to_s.gsub(/&/n, '&amp;').gsub(/'/n,'&#039;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
  else
    s.to_s.gsub(/&/, '&amp;').gsub(/'/,'&#039;').gsub(/\"/, '&quot;').gsub(/>/, '&gt;').gsub(/</, '&lt;')
  end
end

#handleTemplate(html, orig_values, parent_params = {}, &block) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/egalite/template.rb', line 220

def handleTemplate(html, orig_values, parent_params={}, &block)
  params = keyexpander(orig_values, parent_params)
  
  # parse group tag and recurse inner loop
  while md1 = RE_NEST.match(html) # beware: complicated....
    # break if it is innermost loop.
    break if (RE_ENDNEST.match(md1.pre_match))

    # obtain a length of outermost group tag.
    post = string_after_outermost_closetag(md1.post_match)
    
    tag = md1[1]
    key = md1[2]
    
    # recursive-call for each element of array
    innertext = ""
    case tag.downcase
      when 'group'
        groupval = params[key]
        groupval = [] if (groupval == nil)
        groupval = [groupval] unless (groupval.is_a?(Array))
        groupval.each { |v| 
          innertext << handleTemplate(md1.post_match, v, params)
        }
      when 'if'
        unless params[key].blank?
          innertext << handleTemplate(md1.post_match, orig_values, parent_params)
        end
      when 'unless'
        if params[key].blank?
          innertext << handleTemplate(md1.post_match, orig_values, parent_params)
        end
      else
        raise
    end
    # replace this group tag
    html[md1.begin(0)..-(post.size+1)] = innertext
  end
  # cutoff after end tag, in inner-most loop.
  md1 = RE_ENDNEST.match(html)
  html = md1.pre_match if (md1)

  nonnestedtags(html, params, &block)
end