Index: xotcl/library/lib/htmllib.xotcl
===================================================================
diff -u -rf7894d9be99a8da3a04218abcdb9bd46b6d625c8 -r435b41481fb51bf000ebe736d8574fefbeec1710
--- xotcl/library/lib/htmllib.xotcl (.../htmllib.xotcl) (revision f7894d9be99a8da3a04218abcdb9bd46b6d625c8)
+++ xotcl/library/lib/htmllib.xotcl (.../htmllib.xotcl) (revision 435b41481fb51bf000ebe736d8574fefbeec1710)
@@ -1,4 +1,4 @@
-## $Header: /home/neumann/cvs/xotcl/xotcl/library/lib/htmllib.xotcl,v 1.1 2004/05/23 22:50:39 neumann Exp $
+## $Header: /home/neumann/cvs/xotcl/xotcl/library/lib/htmllib.xotcl,v 1.2 2005/09/09 21:07:23 neumann Exp $
#
# htmllib.xotcl
@@ -36,636 +36,642 @@
#
package provide xotcl::htmllib 0.1
-
package require XOTcl
+namespace eval ::xotcl::htmllib {
+ namespace import ::xotcl::*
-@ @File {
- description {
- This package provides the class HtmlBuilder, which can be used to
- generate HTML documents, or a part of a document.
+ @ @File {
+ description {
+ This package provides the class HtmlBuilder, which can be used to
+ generate HTML documents, or a part of a document.
+ }
+ authors {
+ Antti Salonen, as@fishpool.fi
+ }
+ date {
+ $Date: 2005/09/09 21:07:23 $
+ }
}
- authors {
- Antti Salonen, as@fishpool.fi
+
+ #
+ # the compressed parameter means that minimal HTML page are created
+ # i.e. that space indentation is turned off
+ #
+ Class HtmlBuilder -parameter {
+ {compressed 0}
}
- date {
- $Date: 2004/05/23 22:50:39 $
- }
-}
-
-#
-# the compressed parameter means that minimal HTML page are created
-# i.e. that space indentation is turned off
-#
-Class HtmlBuilder -parameter {
- {compressed 0}
-}
-## The constructor.
-##
-## The HtmlBuilder object has two instance variables. The document Tcl list
-## contains the document as a list of strings. The document is stored as a list
-## rather than a single string to allow further indentation of the whole
-## document when necessary.
-## The indentLevel variable is the level of indentation, which is generally
-## increased for the contents of any HTML element that may contain block-level
-## elements. Typical examples would be
,
,
and so forth.
+ ## The constructor.
+ ##
+ ## The HtmlBuilder object has two instance variables. The document Tcl list
+ ## contains the document as a list of strings. The document is stored as a list
+ ## rather than a single string to allow further indentation of the whole
+ ## document when necessary.
+ ## The indentLevel variable is the level of indentation, which is generally
+ ## increased for the contents of any HTML element that may contain block-level
+ ## elements. Typical examples would be
,
,
and so forth.
-HtmlBuilder instproc init {} {
- my instvar document indentLevel
- set document [list]
- set indentLevel 0
- return
-}
+ HtmlBuilder instproc init {} {
+ my instvar document indentLevel
+ set document [list]
+ set indentLevel 0
+ return
+ }
-HtmlBuilder instproc clear {} {
- my instvar document indentLevel
+ HtmlBuilder instproc clear {} {
+ my instvar document indentLevel
- set document [list]
- set indentLevel 0
- return
-}
+ set document [list]
+ set indentLevel 0
+ return
+ }
-HtmlBuilder instproc getDocument {} {
- my instvar document
- return $document
-}
+ HtmlBuilder instproc getDocument {} {
+ my instvar document
+ return $document
+ }
-HtmlBuilder instproc toString {} {
- my instvar document compressed
- set rvalue ""
- foreach line $document {
- if {$compressed == "0"} {
- append rvalue "$line\n"
- } else {
- ## only new line for closing tags at the beginnig
- ## of a document element
- if {[string equal -length 2 "" $line]} {
- append rvalue "$line\n"
- } else {
- append rvalue "$line "
+ HtmlBuilder instproc toString {} {
+ my instvar document compressed
+ set rvalue ""
+ foreach line $document {
+ if {$compressed == "0"} {
+ append rvalue "$line\n"
+ } else {
+ ## only new line for closing tags at the beginnig
+ ## of a document element
+ if {[string equal -length 2 "" $line]} {
+ append rvalue "$line\n"
+ } else {
+ append rvalue "$line "
+ }
+ }
}
- }
+ return $rvalue
}
- return $rvalue
-}
-## parseArguments - Parses the arguments in argList as described in the two
-## additional Tcl lists. In addition to the arguments listed in the two
-## additional lists, the procedure also accepts arguments common to all
-## HTML elements.
-## Arguments:
-## argList - List of arguments to be parsed
-## argParamList - List of arguments that take a parameter
-## argNoParamList - List of arguments that don't take a parameter
-## Returns:
-## A string with arguments to an HTML element.
+ ## parseArguments - Parses the arguments in argList as described in the two
+ ## additional Tcl lists. In addition to the arguments listed in the two
+ ## additional lists, the procedure also accepts arguments common to all
+ ## HTML elements.
+ ## Arguments:
+ ## argList - List of arguments to be parsed
+ ## argParamList - List of arguments that take a parameter
+ ## argNoParamList - List of arguments that don't take a parameter
+ ## Returns:
+ ## A string with arguments to an HTML element.
-HtmlBuilder proc parseArguments {argList argParamList argNoParamList} {
- set rvalue ""
- set argParamList [concat $argParamList [list "ID" "CLASS" "STYLE" "TITLE" "LANG" "DIR"]]
- set param 0
- foreach arg $argList {
- if {$param} {
- append rvalue "=\"$arg\""
- set param 0
- } else {
- set arg2 [string toupper [string trimleft $arg "-"]]
- if {[lsearch -exact $argParamList $arg2] != -1} {
- append rvalue " $arg2"
- set param 1
- } elseif {[lsearch -exact $argNoParamList $arg2] != -1} {
- append rvalue " $arg2"
- } else {
- error "HTML syntax error: Invalid argument $arg2 to element"
- }
- }
+ HtmlBuilder proc parseArguments {argList argParamList argNoParamList} {
+ set rvalue ""
+ set argParamList [concat $argParamList [list "ID" "CLASS" "STYLE" "TITLE" "LANG" "DIR"]]
+ set param 0
+ foreach arg $argList {
+ if {$param} {
+ append rvalue "=\"$arg\""
+ set param 0
+ } else {
+ set arg2 [string toupper [string trimleft $arg "-"]]
+ if {[lsearch -exact $argParamList $arg2] != -1} {
+ append rvalue " $arg2"
+ set param 1
+ } elseif {[lsearch -exact $argNoParamList $arg2] != -1} {
+ append rvalue " $arg2"
+ } else {
+ error "HTML syntax error: Invalid argument $arg2 to element"
+ }
+ }
+ }
+ if {$param} {
+ error "HTML syntax error: Missing parameter to argument $arg2"
+ }
+ return $rvalue
}
- if {$param} {
- error "HTML syntax error: Missing parameter to argument $arg2"
- }
- return $rvalue
-}
-##############################################################################
-## Low-level modification methods:
-##
-## The efficiency of these is of utmost importance if efficiency is an issue
-## in the first place.
-##
-## addString
-## addStringIncr
-## addStringDecr
-## addWhiteSpace
-## addDocument
-## mergeDocument
+ ##############################################################################
+ ## Low-level modification methods:
+ ##
+ ## The efficiency of these is of utmost importance if efficiency is an issue
+ ## in the first place.
+ ##
+ ## addString
+ ## addStringIncr
+ ## addStringDecr
+ ## addWhiteSpace
+ ## addDocument
+ ## mergeDocument
-## Add a new arbitrary string to the document. This method is used by other
-## modification methods, as well as the user directly to add content other than
-## HTML elements. The string str is appended to the document with proper
-## indentation.
+ ## Add a new arbitrary string to the document. This method is used by other
+ ## modification methods, as well as the user directly to add content other than
+ ## HTML elements. The string str is appended to the document with proper
+ ## indentation.
-HtmlBuilder instproc addString {str} {
- my instvar document indentLevel compressed
-
- if {$compressed == "0"} {
- for {set n 0} {$n < $indentLevel} {incr n} {
- append newLine " "
- }
+ HtmlBuilder instproc addString {str} {
+ my instvar document indentLevel compressed
+
+ if {$compressed == "0"} {
+ for {set n 0} {$n < $indentLevel} {incr n} {
+ append newLine " "
+ }
+ }
+ append newLine $str
+ lappend document $newLine
+
+ return
}
- append newLine $str
- lappend document $newLine
-
- return
-}
-## Add a string to the document and increase the indentation level.
+ ## Add a string to the document and increase the indentation level.
-HtmlBuilder instproc addStringIncr {str} {
- my instvar indentLevel
- my addString $str
- incr indentLevel
- return
-}
+ HtmlBuilder instproc addStringIncr {str} {
+ my instvar indentLevel
+ my addString $str
+ incr indentLevel
+ return
+ }
-## Decrease the indentation level and add a string to the document.
+ ## Decrease the indentation level and add a string to the document.
-HtmlBuilder instproc addStringDecr {str} {
- my instvar indentLevel
- incr indentLevel -1
- my addString $str
- return
-}
+ HtmlBuilder instproc addStringDecr {str} {
+ my instvar indentLevel
+ incr indentLevel -1
+ my addString $str
+ return
+ }
-#
-# add the string and replace all line breaks in the
-# string with addLineBreak calls so that given plain text
-# appears similar in HTML output
+ #
+ # add the string and replace all line breaks in the
+ # string with addLineBreak calls so that given plain text
+ # appears similar in HTML output
-HtmlBuilder instproc addStringWithLineBreaks {str} {
- while {[set idx [string first "\n" $str]] != -1} {
- my addString [string range $str 0 [expr $idx - 1]]
- my addLineBreak
- set str [string range $str [expr $idx + 1] end]
- }
- my addString $str
-}
-
-## Add a single line of white space to the HTML document.
-
-HtmlBuilder instproc addWhiteSpace {} {
- my addString ""
- return
-}
+ HtmlBuilder instproc addStringWithLineBreaks {str} {
+ while {[set idx [string first "\n" $str]] != -1} {
+ my addString [string range $str 0 [expr $idx - 1]]
+ my addLineBreak
+ set str [string range $str [expr $idx + 1] end]
+ }
+ my addString $str
+ }
+
+ ## Add a single line of white space to the HTML document.
+
+ HtmlBuilder instproc addWhiteSpace {} {
+ my addString ""
+ return
+ }
-## Add the content of the document given as parameter.
+ ## Add the content of the document given as parameter.
-HtmlBuilder instproc addDocument {document} {
- set documentList [$document getDocument]
-
- foreach line $documentList {
- my addString $line
+ HtmlBuilder instproc addDocument {document} {
+ set documentList [$document getDocument]
+
+ foreach line $documentList {
+ my addString $line
+ }
+ return
}
- return
-}
-## Merge the content of the document given as a parameter. The difference
-## to addDocument is that the document merged is destroyed.
+ ## Merge the content of the document given as a parameter. The difference
+ ## to addDocument is that the document merged is destroyed.
-HtmlBuilder instproc mergeDocument {document} {
- set documentList [$document getDocument]
-
- foreach line $documentList {
- my addString $line
+ HtmlBuilder instproc mergeDocument {document} {
+ set documentList [$document getDocument]
+
+ foreach line $documentList {
+ my addString $line
+ }
+ $document destroy
+ return
}
- $document destroy
- return
-}
-##############################################################################
-## HTML generation methods:
-##
-## The methods for generating various HTML structures are either a pair of
-## start and end methods, such as startParagraph and endParagraph, or a single
-## method such as addListItem. Even if the the closing tag for
, for
-## example, is not required by the HTML specification, using the closing method
-## is necessary to have the document properly indented.
+ ##############################################################################
+ ## HTML generation methods:
+ ##
+ ## The methods for generating various HTML structures are either a pair of
+ ## start and end methods, such as startParagraph and endParagraph, or a single
+ ## method such as addListItem. Even if the the closing tag for
, for
+ ## example, is not required by the HTML specification, using the closing method
+ ## is necessary to have the document properly indented.
-# Add a string to the document within ...
+ # Add a string to the document within ...
-HtmlBuilder instproc addStringStrong {str} {
- my addString "$str"
- return
-}
+ HtmlBuilder instproc addStringStrong {str} {
+ my addString "$str"
+ return
+ }
-# Add a string to the document within ...
+ # Add a string to the document within ...
-HtmlBuilder instproc addStringEmphasized {str} {
- my addString "$str"
- return
-}
+ HtmlBuilder instproc addStringEmphasized {str} {
+ my addString "$str"
+ return
+ }
-# Add a comment to the document
+ # Add a comment to the document
-HtmlBuilder instproc addComment {str} {
- my addString ""
- return
-}
+ HtmlBuilder instproc addComment {str} {
+ my addString ""
+ return
+ }
-HtmlBuilder instproc addLineBreak {} {
- my addString " "
- return
-}
+ HtmlBuilder instproc addLineBreak {} {
+ my addString " "
+ return
+ }
-## startDocument - Start an HTML document. Currently all documents are HTML 4.0
-## Transitional. HTML, BODY, HEAD and TITLE elements are added/started here.
-## Optional arguments:
-## -title documentTitle (empty if not given)
-## -stylesheet externalStyleSheet
-## -bgcolor backgroundColour (deprecated in HTML 4.0)
+ ## startDocument - Start an HTML document. Currently all documents are HTML 4.0
+ ## Transitional. HTML, BODY, HEAD and TITLE elements are added/started here.
+ ## Optional arguments:
+ ## -title documentTitle (empty if not given)
+ ## -stylesheet externalStyleSheet
+ ## -bgcolor backgroundColour (deprecated in HTML 4.0)
-HtmlBuilder instproc startDocument {args} {
- set title ""
- foreach {name value} $args {
- switch -- $name {
- -title {
- set title $value
- }
- -stylesheet {
- set stylesheet $value
- }
- -bgcolor {
- set bgcolor $value
+ HtmlBuilder instproc startDocument {args} {
+ set title ""
+ foreach {name value} $args {
+ switch -- $name {
+ -title {
+ set title $value
+ }
+ -stylesheet {
+ set stylesheet $value
+ }
+ -bgcolor {
+ set bgcolor $value
+ }
}
- }
+ }
+ my addString {}
+ my addWhiteSpace
+ my addString {}
+ my addStringIncr {
}
+ my addString "$title"
+ if {[info exists stylesheet]} {
+ my addString ""
+ }
+ my addStringDecr {}
+ my addWhiteSpace
+ if {[info exists bgcolor]} {
+ my addStringIncr ""
+ } else {
+ my addStringIncr {}
+ }
+ return
}
- my addString {}
- my addWhiteSpace
- my addString {}
- my addStringIncr {}
- my addString "$title"
- if {[info exists stylesheet]} {
- my addString ""
- }
- my addStringDecr {}
- my addWhiteSpace
- if {[info exists bgcolor]} {
- my addStringIncr ""
- } else {
- my addStringIncr {}
- }
- return
-}
-## endDocument - end an HTML document
+ ## endDocument - end an HTML document
-HtmlBuilder instproc endDocument {} {
- my addStringDecr {}
- my addString {}
- return
-}
+ HtmlBuilder instproc endDocument {} {
+ my addStringDecr {}
+ my addString {}
+ return
+ }
-## startParagraph - start a P element
-## Optional arguments:
-## Common HTML arguments
+ ## startParagraph - start a P element
+ ## Optional arguments:
+ ## Common HTML arguments
-HtmlBuilder instproc startParagraph {args} {
- set attributes [HtmlBuilder parseArguments $args [list] [list]]
- my addStringIncr "
"
+ return
+ }
-## endUnorderedList - end a UL element
+ ## endUnorderedList - end a UL element
-HtmlBuilder instproc endUnorderedList {} {
- my addStringDecr {
}
+ return
+ }
-## startListItem - start an LI element
-## Optional arguments:
-## Common HTML arguments
+ ## startListItem - start an LI element
+ ## Optional arguments:
+ ## Common HTML arguments
-HtmlBuilder instproc startListItem {args} {
- set attributes [HtmlBuilder parseArguments $args [list] [list]]
- my addStringIncr "
"
+ return
+ }
-## endListItem - end an LI element
+ ## endListItem - end an LI element
-HtmlBuilder instproc endListItem {} {
- my addStringDecr {
}
- return
-}
+ HtmlBuilder instproc endListItem {} {
+ my addStringDecr {}
+ return
+ }
-## add a simple list item
-HtmlBuilder instproc addListItem {content} {
- my startListItem
- my addString $content
- my endListItem
-}
+ ## add a simple list item
+ HtmlBuilder instproc addListItem {content} {
+ my startListItem
+ my addString $content
+ my endListItem
+ }
-## startTable - start a TABLE element. Note that if the -border argument isn't
-## used, by default the table are created with borders (
).
+ ## startTable - start a TABLE element. Note that if the -border argument isn't
+ ## used, by default the table are created with borders (
"
+ return
+ }
-## endTableHeaderCell - end a TH element
+ ## endTableHeaderCell - end a TH element
-HtmlBuilder instproc endTableHeaderCell {} {
- my addStringDecr {
}
- return
-}
+ HtmlBuilder instproc endTableHeaderCell {} {
+ my addStringDecr {}
+ return
+ }
-## startForm - start a FORM element
-## Required arguments:
-## -action URI
-## Optional arguments:
-## -method get|post
-## Common HTML arguments
+ ## startForm - start a FORM element
+ ## Required arguments:
+ ## -action URI
+ ## Optional arguments:
+ ## -method get|post
+ ## Common HTML arguments
-HtmlBuilder instproc startForm {args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "ACTION" "METHOD" "ENCTYPE"] [list]]
- my addStringIncr "}
- return
-}
+ HtmlBuilder instproc endForm {} {
+ my addStringDecr {}
+ return
+ }
-## addInput - add in INPUT element
-## Required arguments:
-## -type
-## -name
-## Optional arguments:
-## -value
-## -size
-## -maxlength
-## -checked
-## Common HTML arguments
-
-HtmlBuilder instproc addInput {args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "TYPE" "NAME" "VALUE" "SIZE" "MAXLENGTH"] \
- [list "CHECKED"]]
- my addString ""
- return
-}
+ ## addInput - add in INPUT element
+ ## Required arguments:
+ ## -type
+ ## -name
+ ## Optional arguments:
+ ## -value
+ ## -size
+ ## -maxlength
+ ## -checked
+ ## Common HTML arguments
+
+ HtmlBuilder instproc addInput {args} {
+ set attributes [HtmlBuilder parseArguments $args \
+ [list "TYPE" "NAME" "VALUE" "SIZE" "MAXLENGTH"] \
+ [list "CHECKED"]]
+ my addString ""
+ return
+ }
-## addTextArea - start a TEXTAREA element
-## First parameter: value - Default value of the text area
-## Required arguments:
-## -rows
-## -cols
-## Optional arguments:
-## -name
-## Common HTML Arguments
+ ## addTextArea - start a TEXTAREA element
+ ## First parameter: value - Default value of the text area
+ ## Required arguments:
+ ## -rows
+ ## -cols
+ ## Optional arguments:
+ ## -name
+ ## Common HTML Arguments
-HtmlBuilder instproc addTextArea {value args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "ROWS" "COLS" "NAME"] [list]]
- my addString ""
- return
-}
+ HtmlBuilder instproc addTextArea {value args} {
+ set attributes [HtmlBuilder parseArguments $args \
+ [list "ROWS" "COLS" "NAME"] [list]]
+ my addString ""
+ return
+ }
-## startOptionSelector - start a SELECT element
-## Optional arguments:
-## -name
-## -size
-## -multiple
-## Common HTML arguments
+ ## startOptionSelector - start a SELECT element
+ ## Optional arguments:
+ ## -name
+ ## -size
+ ## -multiple
+ ## Common HTML arguments
-HtmlBuilder instproc startOptionSelector {args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "NAME" "SIZE"] [list "MULTIPLE"]]
- my addStringIncr ""
+ return
+ }
-## startOption - start an OPTION element
-## Optional arguments:
-## -value
-## -selected
-## Common HTML arguments
+ ## startOption - start an OPTION element
+ ## Optional arguments:
+ ## -value
+ ## -selected
+ ## Common HTML arguments
-HtmlBuilder instproc startOption {args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "VALUE"] [list "SELECTED"]]
- my addStringIncr ""
- return
-}
+ HtmlBuilder instproc endOption {} {
+ my addStringDecr ""
+ return
+ }
-## addImage - add an IMG element
-## Required arguments:
-## -src
-## -alt
-## -align (deprecated in HTML 4.0)
-## Optional arguments:
-## Common HTML arguments
+ ## addImage - add an IMG element
+ ## Required arguments:
+ ## -src
+ ## -alt
+ ## -align (deprecated in HTML 4.0)
+ ## Optional arguments:
+ ## Common HTML arguments
-HtmlBuilder instproc addImage {args} {
- set attributes [HtmlBuilder parseArguments $args \
- [list "SRC" "ALT" "ALIGN"] [list]]
- my addString ""
- return
-}
+ HtmlBuilder instproc addImage {args} {
+ set attributes [HtmlBuilder parseArguments $args \
+ [list "SRC" "ALT" "ALIGN"] [list]]
+ my addString ""
+ return
+ }
-## startBlock - start a DIV element (a generic block-level container)
-## Optional arguments:
-## Common HTML attributes
+ ## startBlock - start a DIV element (a generic block-level container)
+ ## Optional arguments:
+ ## Common HTML attributes
-HtmlBuilder instproc startBlock {args} {
- set attributes [HtmlBuilder parseArguments $args [list] [list]]
- my addStringIncr "