Class: Forme::Serializer
- Inherits:
-
Object
- Object
- Forme::Serializer
- Defined in:
- lib/forme.rb
Overview
Default serializer class used by the library. Any other serializer classes that want to produce html should probably subclass this class.
Registered as :default.
Direct Known Subclasses
Defined Under Namespace
Classes: AmericanTime, PlainText
Constant Summary collapse
- ESCAPE_HTML =
Borrowed from Rack::Utils, map of single character strings to html escaped versions.
{"&" => "&", "<" => "<", ">" => ">", "'" => "'", '"' => """}
- ESCAPE_HTML_PATTERN =
A regexp that matches all html characters requiring escaping.
Regexp.union(*ESCAPE_HTML.keys)
- SELF_CLOSING =
Which tags are self closing (such tags ignore children).
[:img, :input]
Instance Method Summary collapse
-
#call(tag) ⇒ Object
Serialize the tag object to an html string.
-
#serialize_close(tag) ⇒ Object
Returns the closing part of the given tag.
-
#serialize_open(tag) ⇒ Object
Returns the opening part of the given tag.
Instance Method Details
#call(tag) ⇒ Object
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).
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 |
# File 'lib/forme.rb', line 1272 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) ⇒ Object
Returns the closing part of the given tag.
1303 1304 1305 |
# File 'lib/forme.rb', line 1303 def serialize_close(tag) "</#{tag.type}>" end |
#serialize_open(tag) ⇒ Object
Returns the opening part of the given tag.
1298 1299 1300 |
# File 'lib/forme.rb', line 1298 def serialize_open(tag) "<#{tag.type}#{attr_html(tag.attr)}>" end |