Class: Undies::Template

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/undies/template.rb

Constant Summary collapse

ESCAPE_HTML =

Ripped from Rack v1.3.0 ======================================

> ripped b/c I don’t want a dependency on Rack for just this

{
  "&" => "&",
  "<" => "&lt;",
  ">" => "&gt;",
  "'" => "&#x27;",
  '"' => "&quot;",
  "/" => "&#x2F;"
}
ESCAPE_HTML_PATTERN =
Regexp.union(*ESCAPE_HTML.keys)

Constants included from API

API::OPEN_TAGS, API::SELF_CLOSING_TAGS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#_, #__attrs, #__closed_element, #__flush, #__open_element, #__partial, #__pop, #__push, #__yield, #closed_element, #open_element, #raw

Constructor Details

#initialize(*args) ⇒ Template

end Rip from Rack v1.3.0 =====================================



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
# File 'lib/undies/template.rb', line 36

def initialize(*args)
  # setup a node stack with the given output obj
  @_undies_io = if args.last.kind_of?(Undies::IO)
    args.pop
  else
    raise ArgumentError, "please provide an IO object"
  end

  # apply any given data to template scope as instance variables
  (args.last.kind_of?(::Hash) ? args.pop : {}).each do |k, v|
    self.instance_variable_set("@#{k}", v)
  end

  # push a root node onto the IO
  @_undies_io.push!(RootNode.new(@_undies_io)) if @_undies_io.empty?

  # if given some source, build it
  if args.last.kind_of?(Source)
    # setup a source stack with the given source
    @_undies_source_stack = SourceStack.new(args.pop)

    # yield to recursivley render the source stack
    __yield

    # flush any elements that need to be built
    __flush
  end

end

Class Method Details

.escape_html(string) ⇒ Object

Escape ampersands, brackets and quotes to their HTML/XML entities.



31
32
33
# File 'lib/undies/template.rb', line 31

def self.escape_html(string)
  string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] }
end

.flush(template) ⇒ Object

have as many methods on the class level as possible to keep from polluting the public instance methods, the instance scope, and to maximize the effectiveness of the Template#method_missing logic



15
16
17
# File 'lib/undies/template.rb', line 15

def self.flush(template)
  template.__flush
end