module CGI::HtmlExtension

def a(href = "") # :yield:

:yield:

# => "Example"
a("HREF" => "http://www.example.com", "TARGET" => "_top") { "Example" }

# => "Example"
a("http://www.example.com") { "Example" }

block passed in.
The body of the element is the string returned by the no-argument

the element's attributes.
for the HREF attribute, or it can be a hash of
+href+ can either be a string, giving the URL

Generate an Anchor element as a string.
def a(href = "") # :yield:
  attributes = if href.kind_of?(String)
                 { "HREF" => href }
               else
                 href
               end
  super(attributes)
end

def base(href = "") # :yield:

:yield:
# => ""
base("http://www.example.com/cgi")

The passed-in no-argument block is ignored.

attribute, or it can be a has of the element's attributes.
+href+ can either by a string, giving the base URL for the HREF

Generate a Document Base URI element as a String.
def base(href = "") # :yield:
  attributes = if href.kind_of?(String)
                 { "HREF" => href }
               else
                 href
               end
  super(attributes)
end

def blockquote(cite = {}) # :yield:

:yield:
#=> "
Foo!

blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }

The body is provided by the passed-in no-argument block

or it can be omitted, in which case the element has no attributes.
the quoted text, or a hash, giving all attributes of the element,
+cite+ can either be a string, give the URI for the source of

Generate a BlockQuote element as a string.
def blockquote(cite = {})  # :yield:
  attributes = if cite.kind_of?(String)
                 { "CITE" => cite }
               else
                 cite
               end
  super(attributes)
end

def caption(align = {}) # :yield:

:yield:
# => Capital Cities
caption("left") { "Capital Cities" }

The body of the element is provided by the passed-in no-argument block.

all the attributes of the element. Or it can be omitted.
(one of top, bottom, left, or right). It can be a hash of
+align+ can be a string, giving the alignment of the caption

Generate a Table Caption element as a string.
def caption(align = {}) # :yield:
  attributes = if align.kind_of?(String)
                 { "ALIGN" => align }
               else
                 align
               end
  super(attributes)
end

def checkbox(name = "", value = nil, checked = nil)

# = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
checkbox("name", "value", true)

# = checkbox("NAME" => "name", "VALUE" => "value")
checkbox("name", "value")

# = checkbox("NAME" => "name")
checkbox("name")

Alternatively, the attributes can be specified as a hash.

if true, the CHECKED attribute will be included in the element.
+name+, +value+, and +checked+. +checked+ is a boolean value;
The attributes of the element can be specified as three arguments,

Generate a Checkbox Input element as a string.
def checkbox(name = "", value = nil, checked = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "checkbox", "NAME" => name,
                   "VALUE" => value, "CHECKED" => checked }
               else
                 name["TYPE"] = "checkbox"
                 name
               end
  input(attributes)
end

def checkbox_group(name = "", *values)

"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
checkbox_group("NAME" => "name",

"VALUES" => [["foo"], ["bar", true], "baz"])
checkbox_group("NAME" => "name",

"VALUES" => ["foo", "bar", "baz"])
checkbox_group("NAME" => "name",

# Baz
# Bar
# Foo
checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")

# baz
# bar
# foo
checkbox_group("name", ["foo"], ["bar", true], "baz")

# baz
# bar
# foo
checkbox_group("name", "foo", "bar", "baz")

(defaults to false).
to the same as the label), or the boolean checked element
array, by omitting either the value element (defaults
Each value can also be specified as a two-element

checkbox is CHECKED.
label; and the third is a boolean specifying whether this
The first element is the VALUE attribute; the second is the
Each value can also be specified as a three-element array.

same effect.
for that checkbox. A single-element array has the
as the value of the VALUE attribute and as the label
can be specified as a String, which will be used both
There will be one checkbox for each value. Each value
Each checkbox is followed by a label.
The checkboxes will all have the same +name+ attribute.

Generate a sequence of checkbox elements, as a String.
def checkbox_group(name = "", *values)
  if name.kind_of?(Hash)
    values = name["VALUES"]
    name = name["NAME"]
  end
  values.collect{|value|
    if value.kind_of?(String)
      checkbox(name, value) + value
    else
      if value[-1] == true || value[-1] == false
        checkbox(name, value[0],  value[-1]) +
        value[-2]
      else
        checkbox(name, value[0]) +
        value[-1]
      end
    end
  }.join
end

def file_field(name = "", size = 20, maxlength = nil)

#
file_field("NAME" => "name", "SIZE" => 40)

#
file_field("name", 40, 100)

#
file_field("name", 40)

#
file_field("name")

See #multipart_form() for forms that include file uploads.

Alternatively, the attributes can be specified as a hash.

of the file's _name_, not of the file's _contents_.
+name+, +size+, and +maxlength+. +maxlength+ is the maximum length
The attributes of the element can be specified as three arguments,

Generate an File Upload Input element as a string.
def file_field(name = "", size = 20, maxlength = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "file", "NAME" => name,
                   "SIZE" => size.to_s }
               else
                 name["TYPE"] = "file"
                 name
               end
  attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  input(attributes)
end

def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")

#
string

form("METHOD" => "post", "ENCTYPE" => "enctype") { "string" }

#
string

form("get", "url") { "string" }

#
string

form("get") { "string" }

#
string

form{ "string" }

See also #multipart_form() for forms that include file uploads.

Alternatively, the attributes can be specified as a hash.

defaults to "application/x-www-form-urlencoded".
+action+ defaults to the current CGI script name. +enctype+
+method+ should be either "get" or "post", and defaults to the latter.

Generate a Form element as a string.
def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
  attributes = if method.kind_of?(String)
                 { "METHOD" => method, "ACTION" => action,
                   "ENCTYPE" => enctype }
               else
                 unless method.has_key?("METHOD")
                   method["METHOD"] = "post"
                 end
                 unless method.has_key?("ENCTYPE")
                   method["ENCTYPE"] = enctype
                 end
                 method
               end
  if block_given?
    body = yield
  else
    body = ""
  end
  if @output_hidden
    body << @output_hidden.collect{|k,v|
      "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
    }.join
  end
  super(attributes){body}
end

def hidden(name = "", value = nil)

#
hidden("NAME" => "name", "VALUE" => "reset", "ID" => "foo")

#
hidden("name", "value")

#
hidden("name")

Alternatively, the attributes can be specified as a hash.

+name+ and +value+.
The attributes of the element can be specified as two arguments,

Generate a Hidden Input element as a string.
def hidden(name = "", value = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
               else
                 name["TYPE"] = "hidden"
                 name
               end
  input(attributes)
end

def html(attributes = {}) # :yield:

:yield:

html(if $VERBOSE then "PRETTY" end) { "HTML string" }

# = html("PRETTY" => " ") { "" }
html("PRETTY") { "" }

#
#
#
#
#
html("PRETTY" => "\t") { "" }

#
#
#
#
#
html("PRETTY" => " ") { "" }

# string
html("DOCTYPE" => '') { "string" }

# string
html("DOCTYPE" => false) { "string" }

# string
html("LANG" => "ja") { "string" }

# string
html{ "string" }

The body of the html element is supplied as a block.

should include the entire text of this tag, including angle brackets.
"DOCTYPE", if given, is used as the leading DOCTYPE SGML tag; it
a string as the sole argument to this method. The pseudo-attribute
HTML string should be indented. "PRETTY" can also be specified as
pseudo-attribute "PRETTY" can be used to specify that the generated
The attributes of the element are specified as a hash. The

Generate a top-level HTML element as a string.
def html(attributes = {}) # :yield:
  if nil == attributes
    attributes = {}
  elsif "PRETTY" == attributes
    attributes = { "PRETTY" => true }
  end
  pretty = attributes.delete("PRETTY")
  pretty = "  " if true == pretty
  buf = "".dup
  if attributes.has_key?("DOCTYPE")
    if attributes["DOCTYPE"]
      buf << attributes.delete("DOCTYPE")
    else
      attributes.delete("DOCTYPE")
    end
  else
    buf << doctype
  end
  buf << super(attributes)
  if pretty
    CGI.pretty(buf, pretty)
  else
    buf
  end
end

def image_button(src = "", name = nil, alt = nil)

#
image_button("SRC" => "url", "ALT" => "string")

#
image_button("url", "name", "string")

#
image_button("url")

Alternatively, the attributes can be specified as a hash.

is the input name. +alt+ is the alternative text for the image.
+src+ is the URL of the image to use for the button. +name+

Generate an Image Button Input element as a string.
def image_button(src = "", name = nil, alt = nil)
  attributes = if src.kind_of?(String)
                 { "TYPE" => "image", "SRC" => src, "NAME" => name,
                   "ALT" => alt }
               else
                 src["TYPE"] = "image"
                 src["SRC"] ||= ""
                 src
               end
  input(attributes)
end

def img(src = "", alt = "", width = nil, height = nil)

# alt
img("SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50)

# alt
img("src", "alt", 100, 50)

Alternatively, the attributes can be specified as a hash.

its height.
the image. +width+ is the width of the image, and +height+ is
+src+ is the URL of the image. +alt+ is the alternative text for

Generate an Image element as a string.
def img(src = "", alt = "", width = nil, height = nil)
  attributes = if src.kind_of?(String)
                 { "SRC" => src, "ALT" => alt }
               else
                 src
               end
  attributes["WIDTH"] = width.to_s if width
  attributes["HEIGHT"] = height.to_s if height
  super(attributes)
end

def multipart_form(action = nil, enctype = "multipart/form-data")

#
string

multipart_form("url") { "string" }

#
string

multipart_form{ "string" }

Alternatively, the attributes can be specified as a hash.

type, which defaults to "multipart/form-data".
+action+ is the action to perform. +enctype+ is the encoding

Multipart encoding is used for forms that include file uploads.

Generate a Form element with multipart encoding as a String.
def multipart_form(action = nil, enctype = "multipart/form-data")
  attributes = if action == nil
                 { "METHOD" => "post", "ENCTYPE" => enctype }
               elsif action.kind_of?(String)
                 { "METHOD" => "post", "ACTION" => action,
                   "ENCTYPE" => enctype }
               else
                 unless action.has_key?("METHOD")
                   action["METHOD"] = "post"
                 end
                 unless action.has_key?("ENCTYPE")
                   action["ENCTYPE"] = enctype
                 end
                 action
               end
  if block_given?
    form(attributes){ yield }
  else
    form(attributes)
  end
end

def password_field(name = "", value = nil, size = 40, maxlength = nil)

#
password_field("NAME" => "name", "VALUE" => "value")

#
password_field("password", "value", 80, 200)

#
password_field("name", "value")

#
password_field("name")

Alternatively, attributes can be specified as a hash.

is the maximum length of the inputted password.
value. +size+ is the size of the input field display. +maxlength+
+name+ is the name of the input field. +value+ is its default

Generate a Password Input element as a string.
def password_field(name = "", value = nil, size = 40, maxlength = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "password", "NAME" => name,
                   "VALUE" => value, "SIZE" => size.to_s }
               else
                 name["TYPE"] = "password"
                 name
               end
  attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  input(attributes)
end

def popup_menu(name = "", *values)

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
radio_button("NAME" => "name", "VALUE" => "value", "ID" => "foo")

#
radio_button("name", "value", true)

#
radio_button("name", "value")

Alternatively, the attributes can be specified as a hash.

starts off checked.
the field if checked. +checked+ specifies whether the field
+name+ is the name of the input field. +value+ is the value of

Generates a radio-button Input element.
def radio_button(name = "", value = nil, checked = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "radio", "NAME" => name,
                   "VALUE" => value, "CHECKED" => checked }
               else
                 name["TYPE"] = "radio"
                 name
               end
  input(attributes)
end

def radio_group(name = "", *values)

"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
radio_group("NAME" => "name",

"VALUES" => [["foo"], ["bar", true], "baz"])
radio_group("NAME" => "name",

"VALUES" => ["foo", "bar", "baz"])
radio_group("NAME" => "name",

# Baz
# Bar
# Foo
radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")

# baz
# bar
# foo
radio_group("name", ["foo"], ["bar", true], "baz")

# baz
# bar
# foo
radio_group("name", "foo", "bar", "baz")

to have more than one radiobutton in a group checked.
This works the same as #checkbox_group(). However, it is not valid

Generate a sequence of radio button Input elements, as a String.
def radio_group(name = "", *values)
  if name.kind_of?(Hash)
    values = name["VALUES"]
    name = name["NAME"]
  end
  values.collect{|value|
    if value.kind_of?(String)
      radio_button(name, value) + value
    else
      if value[-1] == true || value[-1] == false
        radio_button(name, value[0],  value[-1]) +
        value[-2]
      else
        radio_button(name, value[0]) +
        value[-1]
      end
    end
  }.join
end

def reset(value = nil, name = nil)

#
reset("VALUE" => "reset", "ID" => "foo")

#
reset("reset")

#
reset

Alternatively, the attributes can be specified as a hash.

is the text displayed on the button. +name+ is the name of this button.
This resets the values on a form to their initial values. +value+

Generate a reset button Input element, as a String.
def reset(value = nil, name = nil)
  attributes = if (not value) or value.kind_of?(String)
                 { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
               else
                 value["TYPE"] = "reset"
                 value
               end
  input(attributes)
end

def submit(value = nil, name = nil)

#
submit("VALUE" => "ok", "NAME" => "button1", "ID" => "foo")

#
submit("ok", "button1")

#
submit("ok")

#
submit

Alternatively, the attributes can be specified as a hash.

of the input.
+value+ is the text to display on the button. +name+ is the name

Generate a submit button Input element, as a String.
def submit(value = nil, name = nil)
  attributes = if (not value) or value.kind_of?(String)
                 { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
               else
                 value["TYPE"] = "submit"
                 value
               end
  input(attributes)
end

def text_field(name = "", value = nil, size = 40, maxlength = nil)

#
text_field("NAME" => "name", "VALUE" => "value")

#
text_field("name", "value", 80, 200)

#
text_field("name", "value", 80)

#
text_field("name", "value")

#
text_field("name")

Alternatively, the attributes can be specified as a hash.

is the maximum length of input accepted.
value. +size+ is the size of the input area. +maxlength+
+name+ is the name of the input field. +value+ is its initial

Generate a text field Input element, as a String.
def text_field(name = "", value = nil, size = 40, maxlength = nil)
  attributes = if name.kind_of?(String)
                 { "TYPE" => "text", "NAME" => name, "VALUE" => value,
                   "SIZE" => size.to_s }
               else
                 name["TYPE"] = "text"
                 name
               end
  attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  input(attributes)
end

def textarea(name = "", cols = 70, rows = 10) # :yield:

:yield:
# = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
textarea("name", 40, 5)

# = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10)
textarea("name")

The body is provided by the passed-in no-argument block

Alternatively, the attributes can be specified as a hash.

columns and +rows+ is the number of rows in the display.
+name+ is the name of the textarea. +cols+ is the number of

Generate a TextArea element, as a String.
def textarea(name = "", cols = 70, rows = 10)  # :yield:
  attributes = if name.kind_of?(String)
                 { "NAME" => name, "COLS" => cols.to_s,
                   "ROWS" => rows.to_s }
               else
                 name
               end
  super(attributes)
end