File: xml.tcl

package info (click to toggle)
libapache2-mod-rivet 2.3.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,156 kB
  • ctags: 1,093
  • sloc: xml: 7,696; tcl: 6,939; ansic: 5,682; sh: 4,862; makefile: 199; sql: 91; lisp: 78
file content (49 lines) | stat: -rw-r--r-- 1,124 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
# xml.tcl string ?tag ?attr val? ?attr val?? ?tag ?attr val? ?attr val??
#
# Example 1:
#
#  ::rivet::xml Test b i 
# <== <b><i>Test</i></b>
#  
# Example 2:
#
# ::rivet::xml Test [list div class box id testbox] b i
# <== <div class="box" id="testbox"><b><i>Test</i></b></div>
#
# Example 3
#
# ::rivet::xml "anything ..." div [list a href "http://..../" title "info message"] 
# <== <div><a href="http://..../" title="info message">anything ...</a></div>
#
# $Id: xml.tcl 1492289 2013-06-12 17:10:34Z mxmanghi $
#

namespace eval ::rivet {

    proc xml {textstring args} {

        set xmlout      ""
        set tags_stack  {}

        foreach el $args {

            set el  [lassign $el tag]
            lappend tags_stack $tag
            append xmlout "<$tag"

            foreach {attrib attrib_v} $el {
                append xmlout " $attrib=\"$attrib_v\""
            }

            append xmlout ">"
        }

        if {[::rivet::lempty $tags_stack]} {
            return $textstring
        } else {
            return [append xmlout "$textstring</[join [lreverse $tags_stack] "></"]>"]
        }
    }

}