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
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
/* 
 * $Id: module.pmod.in,v 1.5 1999/06/12 19:51:38 mirar Exp $ 
 * 
 */ 
 
inherit @module@; 
 
//! module Parser 
 
class SGML 
//! class SGML 
//!     This is a handy simple parser of SGML-like 
//!     syntax like HTML. It doesn't do anything advanced, 
//!     but finding the corresponding end-tags. 
//!      
//!     It's used like this: 
//!     <pre>array res=Parser.SGML()->feed(string)->finish()->result();</pre> 
//! 
//!     The resulting structure is an array of atoms, 
//!     where the atom can be a string or a tag. 
//!     A tag contains a similar array, as data.  
//!      
//!     Example: 
//!     A string 
//!     <tt>"&lt;gat>&nbsp;&lt;gurka>&nbsp;&lt;/gurka>&nbsp;&lt;banan>&nbsp;&lt;kiwi>&nbsp;&lt;/gat>"</tt> 
//!     results in  
//!     <pre> 
//!     ({ 
//!         tag "gat" object with data: 
//!         ({ 
//!             tag "gurka" object with data: 
//!             ({ 
//!                 " " 
//!             }) 
//!             tag "banan" object with data: 
//!             ({ 
//!                 " " 
//!                 tag "kiwi" object with data: 
//!                 ({ 
//!                    " " 
//!                 }) 
//!             }) 
//!         }) 
//!     }) 
//!     </pre> 
//!              
//!     ie, simple "tags" (not containers) are not detected, 
//!     but containers are ended implicitely by a surrounding 
//!     container _with_ an end tag. 
//! 
//!     The 'tag' is an object with the following variables: 
//!     <pre> 
//!      string name;           - name of tag 
//!      mapping args;          - argument to tag 
//!      int line,char,column;  - position of tag 
//!      string file;           - filename (see <ref>create</ref>) 
//!      array(SGMLatom) data;  - contained data 
//!     </pre> 
//! 
{ 
   string file; 
 
   class SGMLatom 
   { 
      string name; 
      mapping args; 
      int line,char,column; 
      string file; 
      array(SGMLatom) data; 
   } 
 
   static array(array(object(SGMLatom)|string)) res=({({})}); 
   static array(SGMLatom) tagstack=({}); 
   static array(object) errors; 
 
   array(object(SGMLatom)|string) data; 
 
   static private array(string) got_tag(object g) 
   { 
      string name=g->tag_name(); 
 
      if (name!="" && name[0]=='/') 
      { 
         int i=search(tagstack->name,name[1..]); 
         if (i!=-1)  
         { 
            i++; 
            while (i--) 
            { 
               tagstack[0]->data=res[0]; 
               res=res[1..]; 
               tagstack=tagstack[1..]; 
            } 
            return ({}); 
         } 
      } 
 
      object t=SGMLatom(); 
      t->name=name; 
      t->args=g->tag_args(); 
      [t->line,t->char,t->column]=g->at(); 
      t->file=file; 
      res[0]+=({t}); 
      tagstack=({t})+tagstack; 
      res=({({})})+res; 
       
      return ({}); // don't care 
   } 
 
   void debug(array|void arr,void|int level) 
   { 
      level+=2; 
      if (!arr) arr=data; 
      foreach (arr,string|object(SGMLatom) t) 
         if (stringp(t)) 
            write("%*s%-=*s\n",level,"",79-level,sprintf("%O",t)); 
         else 
         { 
            write("%*stag %O\n",level,"",t->name,); 
            if (sizeof(t->args)) 
               write("%*s%-=*s\n",level+4,"",75-level,sprintf("%O",t->args)); 
            debug(t->data,level); 
         } 
   } 
 
 
   private static object p=HTML(); 
 
//! method void create() 
//! method void create(string filename) 
//!     This object is created with this filename. 
//!     It's passed to all created tags, for debug and trace purposes. 
//! note: 
//!     No, it doesn't read the file itself. See <ref>feed</ref>. 
 
   void create(void|string _file) 
   { 
      file=_file; 
 
      p->_set_tag_callback(got_tag); 
      p->_set_data_callback(lambda(object g,string data)  
                            { if (data!="") res[0]+=({data}); return ({}); }); 
   } 
 
//! method object feed(string s) 
//! method array finish() 
//! method array result(string s) 
//!     Feed new data to the object, or finish the stream. 
//!     No result can be used until finish() is called. 
//! 
//!     Both finish() and result() returns the computed data. 
//! 
//!     feed() returns the called object. 
 
   object feed(string s) 
   { 
      p->feed(s); 
      return this_object(); 
   } 
 
   array finish() 
   { 
      p->finish(); 
      data=res[0]; 
      res=0; 
      return data; 
   } 
 
   array(object(SGMLatom)|string) result() 
   { 
      return data; 
   } 
}