module Forma::Html

Utilities for html text generation.

Public Class Methods

attr(*params) click to toggle source

Attribute creation.

# File lib/forma/html.rb, line 6
def attr(*params)
  if params.length == 2
    SimpleAttr.new(params[0].to_s, params[1].to_s)
  elsif params.length == 1 and params[0].is_a?(Hash)
    StyleAttr.new(params[0])
  elsif params.length == 1
    ClassAttr.new(params[0])
  else
    raise "illegal attr specification: #{params}"
  end
end
el(tag, opts = {}) click to toggle source

You can easily create elements using this method.

“` include Forma::Html element = el(“div”, attrs = {id: 'main', class: 'header', style: {'font-size' => '20px'}}) html = element.to_s “`

# File lib/forma/html.rb, line 25
def el(tag, opts = {})
  opts = opts.symbolize_keys
  h = { text: opts[:text], html: opts[:html] }
  if opts[:attrs]
    attributes = []
    opts[:attrs].each do |k, v|
      if k == :class || k == :style
        attributes << attr(v)
      else
        attributes << attr(k, v)
      end
    end
    h[:attrs] = attributes
  end
  h[:children] = opts[:children]
  Element.new(tag, h)
end

Private Instance Methods

attr(*params) click to toggle source

Attribute creation.

# File lib/forma/html.rb, line 6
def attr(*params)
  if params.length == 2
    SimpleAttr.new(params[0].to_s, params[1].to_s)
  elsif params.length == 1 and params[0].is_a?(Hash)
    StyleAttr.new(params[0])
  elsif params.length == 1
    ClassAttr.new(params[0])
  else
    raise "illegal attr specification: #{params}"
  end
end
el(tag, opts = {}) click to toggle source

You can easily create elements using this method.

“` include Forma::Html element = el(“div”, attrs = {id: 'main', class: 'header', style: {'font-size' => '20px'}}) html = element.to_s “`

# File lib/forma/html.rb, line 25
def el(tag, opts = {})
  opts = opts.symbolize_keys
  h = { text: opts[:text], html: opts[:html] }
  if opts[:attrs]
    attributes = []
    opts[:attrs].each do |k, v|
      if k == :class || k == :style
        attributes << attr(v)
      else
        attributes << attr(k, v)
      end
    end
    h[:attrs] = attributes
  end
  h[:children] = opts[:children]
  Element.new(tag, h)
end