class Html

Html Creation and Rendering

This class, combined with the Html::Element class, provides a DSL for html creation, similar to the XmlBuilder class, but tailored for HTML generation.

An Html class instance is an ordered collection of Html::Elements and Strings, that together can be rendered out as HTML.

Usage:

Html.build do |html|

html.div(:id => 'some-div') {
  html.em('HTML is neat!')
}

end

HTML Element Class

Used with the Html class to generate html content for a single tag/element. Represents a single element with attributes and optional contents (including other elements). Generally, you won't use this by itself. Check out Html.build() instead.

Simple useage:

>> Html::Element.new('span','some text', :id => 'title-text').render
=> '<span id="title-text">some text</span>'

Complex usage:

span = Html::Element.new('span')   # Creates a span element for customization
span.id = 'title-text'       # Set some attributes
span.style = 'color: #f00;'
span.html = 'some text'      # Adds some content
span.render                  # Converts to html string
=> '<span id="title-text" style="color: #f00;">some text</span>

Constants

HTML_ESCAPE

Constants

JS_ESCAPE

Public Class Methods

build() { |builder| ... } click to toggle source

Primary entry point for HTML generation using these tools.

# File lib/iron/web/html.rb, line 34
def self.build
  builder = Html.new
  yield builder if block_given?
  builder.render.html_safe
end
escape_javascript(js) click to toggle source

And again, thanks Rails team!

# File lib/iron/web/html.rb, line 66
def self.escape_javascript(js)
  if js
    res = js.gsub(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|special| JS_ESCAPE[special] }
    js.html_safe? ? res.html_safe : res
  else
    ''
  end
end
escape_once(html) click to toggle source

The escape methods escape_once and escape_javascript are both taken from Rails, which like this library is MIT licensed. The following license statement applies to those two methods and the constants they reference. Thanks Rails team!

Copyright © 2004-2013 David Heinemeier Hansson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Ripped from Rails…

# File lib/iron/web/html.rb, line 60
def self.escape_once(html)
  return html if html.html_safe?
  html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }.html_safe
end
new() { |self| ... } click to toggle source

Sets up internal state, natch, and accepts a block that customizes the resulting object.

# File lib/iron/web/html.rb, line 76
def initialize
  @items = []
  @item_stack = []
  yield self if block_given?
end

Public Instance Methods

<<(new_item) click to toggle source

Allow pushing new elements

# File lib/iron/web/html.rb, line 97
def <<(new_item)
  if @item_stack.empty?
    @items << new_item
  else
    @item_stack.last.html << new_item
  end
  self
end
blank?() click to toggle source
# File lib/iron/web/html.rb, line 119
def blank?
  empty?
end
comment!(str) click to toggle source

Inserts an HTML comment (eg <!– yo –>)

# File lib/iron/web/html.rb, line 83
def comment!(str)
  if str.include? "\n"
    text! "<!--\n#{str}\n-->\n"
  else
    text! "<!-- #{str} -->\n"
  end
end
count() click to toggle source
# File lib/iron/web/html.rb, line 111
def count
  @items.count
end
each() { |v| ... } click to toggle source

Implement enumerable

# File lib/iron/web/html.rb, line 107
def each
  @items.each {|v| yield v} if block_given?
end
empty?() click to toggle source
# File lib/iron/web/html.rb, line 115
def empty?
  @items.empty?
end
inspect() click to toggle source

Alias for render

# File lib/iron/web/html.rb, line 183
def inspect
  render
end
is_a?(other) click to toggle source
# File lib/iron/web/html.rb, line 187
def is_a?(other)
  return other == Html
end
method_missing(method, *args, &block) click to toggle source

Creates a new element on any method missing calls. Returns self, so you can chain calls (eg html.div('foo').span('bar') )

Calls superclass method
# File lib/iron/web/html.rb, line 137
def method_missing(method, *args, &block)
  parts = method.to_s.match(/^([a-z]+[0-9]?)$/)
  if parts
    # Assume it's a new element, create the tag
    tag(parts[1], *args, &block)
  else
    # There really is no method...
    super
  end
end
render(depth = 0, inblock = true) click to toggle source

Renders out as html - accepts depth param to indicate level of indentation

# File lib/iron/web/html.rb, line 159
def render(depth = 0, inblock = true)
  # Convert elements to strings
  @items.collect do |item|
    if item.is_a?(String)
      if inblock
        inblock = false
        '  '*depth + item
      else
        item
      end
    elsif item.nil?
      ''
    else
      item.render(depth,inblock)
    end
  end.join('')
end
respond_to_missing?(method, include_private) click to toggle source

Make sure our objects advertise their support of tags

Calls superclass method
# File lib/iron/web/html.rb, line 149
def respond_to_missing?(method, include_private)
  parts = method.to_s.match(/^([a-z]+[0-9]?)$/)
  if parts
    true
  else
    super
  end
end
tag(tag, *args, &block) click to toggle source

Create a new element explicitly

# File lib/iron/web/html.rb, line 124
def tag(tag, *args, &block)
  item = Html::Element.new(tag, *args)
  self << item
  if block
    @item_stack.push item
    block.call(item) 
    @item_stack.pop
  end
  return self
end
text!(str) click to toggle source

Inserts raw text

# File lib/iron/web/html.rb, line 92
def text!(str)
  self << str
end
to_s() click to toggle source

Alias for render

# File lib/iron/web/html.rb, line 178
def to_s
  render
end