class Forme::Serializer

  1. lib/forme/bs3.rb
  2. lib/forme/transformers/serializer.rb
  3. show all
Superclass: Object

Default serializer class used by the library. Any other serializer classes that want to produce html should probably subclass this class.

Registered as :default.

Methods

Public Instance

  1. call
  2. serialize_close
  3. serialize_open

Constants

ESCAPE_HTML = {"&" => "&", "<" => "<", ">" => ">", "'" => "'", '"' => """}  

Borrowed from Rack::Utils, map of single character strings to html escaped versions.

ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys)  

A regexp that matches all html characters requiring escaping.

SELF_CLOSING = [:img, :input]  

Which tags are self closing (such tags ignore children).

Public Instance methods

call (tag)

Serialize the tag object to an html string. Supports Tag instances, Input instances (recursing into call with the result of formatting the input), arrays (recurses into call for each entry and joins the result), and (html escapes the string version of them, unless they include the Raw module, in which case no escaping is done).

[show source]
# File lib/forme/transformers/serializer.rb, line 24
def call(tag)
  case tag
  when Tag
    if SELF_CLOSING.include?(tag.type)
      "<#{tag.type}#{attr_html(tag.attr)}/>"
    else
      "#{serialize_open(tag)}#{call(tag.children)}#{serialize_close(tag)}"
    end
  when Input
    call(tag.format)
  when Array
    tag.map{|x| call(x)}.join
  when DateTime, Time
    format_time(tag)
  when Date
    format_date(tag)
  when BigDecimal
    tag.to_s('F')
  when Raw
    tag.to_s
  else
    h tag
  end
end
serialize_close (tag)

Returns the closing part of the given tag.

[show source]
# File lib/forme/transformers/serializer.rb, line 55
def serialize_close(tag)
  "</#{tag.type}>"
end
serialize_open (tag)

Returns the opening part of the given tag.

[show source]
# File lib/forme/transformers/serializer.rb, line 50
def serialize_open(tag)
  "<#{tag.type}#{attr_html(tag.attr)}>"
end