util/xmllex.lua

Mon, 03 Jan 2011 21:55:58 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 03 Jan 2011 21:55:58 +0000
changeset 3992
de77ec2b49bc
parent 3991
7a2856c8ab7a
child 3995
7214dc7a5642
permissions
-rw-r--r--

All correcthg diff util/xmppstream.lua

3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
1 local assert, ipairs , pairs , setmetatable , rawget , rawset , tostring =
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
2 assert, ipairs , pairs , setmetatable , rawget , rawset , tostring
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local strsub = string.sub
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local tblconcat = table.concat
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
5 local tblinsert = table.insert
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
7 local function getstring ( msgs , startpos , finishpos )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
8 if #msgs == 1 then --All originated in same string
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
9 return strsub ( msgs[1] , startpos , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 else -- Over multiple source strings
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
11 return strsub ( msgs[1] , startpos , -1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
12 .. tblconcat ( msgs , "" , 2 , #msgs - 1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
13 .. strsub ( msgs[#msgs] , 1 , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local m_mt = {
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 __tostring = function ( v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
19 local str = v.stringform
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
20 if str then
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
21 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
22 else
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
23 str = getstring ( v.msgs , v.start , v.finish )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
24 v.stringform = str
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
25 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
26 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 }
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
30 local function handleoutside ( str , r , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
31 local a , b , close = str:find ( "<(/?)" , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
32 if not a then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
33 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
34 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
37 --Finalise text object
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
38 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
39 m.finish = a - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
40 m.type = "text"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
41
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
42 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
43 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
44 start = a ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
45 starte = b + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
46 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
47
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
48 if close ~= "/" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
49 r.depth = r.depth + 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
50 m.type = "open"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
51 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
52 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
53 m.type = "close"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
54 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
55
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
56 tblinsert ( r , m )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
57
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
58 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
59 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
60 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
61
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
62 local function handleinside ( str, r , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
63 local c , d , selfclosing = str:find ( "(/?)>" , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
64 if not c then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
65 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
66 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
69 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
70 m.finish = d
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
71 m.finishs = c - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
72 if selfclosing == "/" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
73 m.type = "selfclosing"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
74 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
75 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
76
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
77 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
78 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
79 start = d + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
80 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
81 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
82 tblinsert ( r , m )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
83
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
84 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
85 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
86 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
88 local function index ( str , r )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
89 r = r or { depth = 0, state = "outside" }
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
90
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
91 if not r[#r] then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
92 r[1] = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
93 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
94 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
95 start = 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
96 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
97 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
98 tblinsert ( r[#r].msgs , str )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
99 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
100
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
101 repeat
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
102 if r.state == "outside" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
103 if not handleoutside ( str , r , r[#r].start ) then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
104 break
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
105 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
106 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
107 if not handleinside ( str , r , r[#r].start ) then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
108 break
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
111 until false
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
112
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 return r
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
116 local function process_starttag ( starttag )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local str = tostring ( starttag )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
118 local attr = { }
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local elem = str:match ( "[^%s=></]+" )
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 for name , quote, attvalue in str:gmatch ( [=[([^%s=<]+)%s*=%s*(["'])([^"]*)%2]=] ) do
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 attr [ name ] = attvalue
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return elem , attr
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
127 local stanza_mt = {
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
128 __index = function ( t , k )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
129 if k == "name" or k == "attr" then
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
130 local elem , attr = process_starttag ( t.opentag )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
131 rawset ( t , "name" , elem )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
132 rawset ( t , "attr" , attr )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
133 return rawget ( t , k )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
134 else
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
135 print("METHOD",k)
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
136 return stanza_methods [ k ]
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
137 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
138 end ;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
139 __tostring = function ( t )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
140 local opentag = t.opentag
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
141 local endtag = assert ( rawget ( t , "endtag" ) or rawget ( t , "selfclosing" ) and t.opentag )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
142 return getstring ( opentag.msgs , opentag.start , endtag.finish )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
143 end ;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
144 }
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
145
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
146 local function new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
147 return setmetatable ( { tags = { } } , stanza_mt )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
148 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
149
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
150 local function tagindex_to_tree(indices)
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
151 local root = { tags = { } }
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
152 local leaf = root
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
153
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
154 for k ,v in ipairs ( indices ) do
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
155 if v.type == "selfclosing" then
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
156 local newleaf = new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
157 newleaf.opentag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
158 newleaf.selfclosing = true
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
159 newleaf.parent = leaf
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
160
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
161 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
162 tblinsert ( leaf.tags , newleaf )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
163 elseif v.type == "close" then -- Close tag
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
164 leaf.endtag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
165 leaf = leaf.parent
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
166 elseif v.type == "text" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
167 tblinsert ( leaf, v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
168 else -- Open tag
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
169 local newleaf = new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
170 newleaf.opentag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
171 newleaf.parent = leaf
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
172
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
173 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
174 tblinsert ( leaf.tags , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
175
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
176 leaf = newleaf
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
177 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
178 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
179
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
180 assert ( leaf == root , "Mismatched opening/closing tags" )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
181 return root;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
182 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
183
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 return {
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 index = index;
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 tagindex_to_tree = tagindex_to_tree;
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 };

mercurial