~ubuntu-branches/ubuntu/saucy/steam/saucy-backports

« back to all changes in this revision

Viewing changes to server/libraries/htmllib.pmod

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (0.2.4) (3.2.1 trusty-proposed)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
3
 
 *  This program is free software; you can redistribute it and/or modify
4
 
 *  it under the terms of the GNU General Public License as published by
5
 
 *  the Free Software Foundation; either version 2 of the License, or
6
 
 *  (at your option) any later version.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
 
 * 
17
 
 * $Id: htmllib.pmod,v 1.1.1.1 2006/03/27 12:40:10 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: htmllib.pmod,v 1.1.1.1 2006/03/27 12:40:10 exodusd Exp $";
21
 
 
22
 
#include <classes.h>
23
 
#include <macros.h>
24
 
#include <database.h>
25
 
#include <attributes.h>
26
 
 
27
 
//#define KEEP_UTF // keep the UTF8 converted encoding for output
28
 
 
29
 
import httplib;
30
 
 
31
 
string ahref_link_navigate(object obj, void|string prefix)
32
 
{
33
 
    if ( !stringp(prefix) ) prefix = "";
34
 
    return "<a "+href_link_navigate(obj)+">"+prefix+obj->get_identifier()+
35
 
        "</a>";
36
 
}
37
 
 
38
 
string href_link_navigate_postfix(object obj, string prefix, string postfix)
39
 
{
40
 
    string path;
41
 
    string href;
42
 
    object dest = obj;
43
 
 
44
 
    if (!stringp(prefix)) prefix="";
45
 
    if (!stringp(postfix))postfix="";
46
 
 
47
 
    if ( obj->get_object_class() & CLASS_EXIT ) {
48
 
        dest = obj->get_exit();
49
 
        path = get_module("filepath:tree")->object_to_filename(dest);
50
 
        href = "href=\""+path+postfix+"\"";
51
 
    }
52
 
    else
53
 
        href = "href=\""+prefix+replace_uml(obj->get_identifier())+postfix+"\"";
54
 
    return href;
55
 
}
56
 
 
57
 
string href_link_navigate(object obj, void|string prefix)
58
 
{
59
 
    string path;
60
 
    string href;
61
 
    object dest = obj;
62
 
 
63
 
    if ( !stringp(prefix) ) prefix = "";
64
 
 
65
 
    if ( obj->get_object_class() & CLASS_EXIT ) {
66
 
        dest = obj->get_exit();
67
 
        path = get_module("filepath:tree")->object_to_filename(dest);
68
 
        href = "href=\""+path+"\"";
69
 
    }
70
 
    else 
71
 
        href = "href=\""+prefix+replace_uml(obj->get_identifier())+"\"";
72
 
    return href;
73
 
}
74
 
 
75
 
string create_tag(string name, mapping attrs)
76
 
{
77
 
  string attr_string = "";
78
 
  foreach(indices(attrs), string a) {
79
 
    attr_string += " " + a + "=\""+attrs[a]+"\"";
80
 
  }
81
 
  return sprintf("<%s%s>", name, attr_string);
82
 
}
83
 
 
84
 
class rxmlHandler 
85
 
{
86
 
    inherit "AbstractCallbacks";
87
 
 
88
 
    static string           output = ""; // the output
89
 
    static mapping rxml_handlers   = ([ ]);
90
 
    static mapping rxml_attributes = ([ ]);
91
 
    static mapping variables       = ([ ]);
92
 
    static string encoding         = "utf-8";
93
 
    static int scriptmode          = 0;
94
 
 
95
 
    static ADT.Queue NodeDataQueue = ADT.Queue();
96
 
 
97
 
    void create(mapping vars) {
98
 
        variables = vars;
99
 
    }
100
 
 
101
 
    int store_data(string data) {
102
 
      string node_data = NodeDataQueue->read();
103
 
      if ( stringp(node_data) ) {
104
 
        node_data += data;
105
 
        NodeDataQueue->write(node_data);
106
 
        return 1;
107
 
      }
108
 
      return 0;
109
 
    }
110
 
    
111
 
    void startDocumentSAX(object parser, void|mixed userData) {
112
 
      output = "<!-- sTeam link consistency and HTML extension parser - modified document view !-->\n";
113
 
    }
114
 
 
115
 
    void startElementSAX(object parser, string name, 
116
 
                         mapping(string:string) attrs, void|mixed userData) 
117
 
    {
118
 
      if ( name == "script" )
119
 
          scriptmode = 1;
120
 
 
121
 
      if ( !rxml_handlers[name] ) {
122
 
        string attr_string = "";
123
 
        if ( mappingp(attrs) ) {
124
 
          foreach(indices(attrs), string a) {
125
 
            
126
 
            attr_string += " " + a + "=\""+attrs[a]+"\"";
127
 
          }
128
 
        }
129
 
        string tagstr = "<"+name+ attr_string + ">";
130
 
        
131
 
        if ( !store_data(tagstr) )
132
 
          output += tagstr;
133
 
      }
134
 
      else {
135
 
        rxml_attributes[name] = attrs;
136
 
        NodeDataQueue->write(""); // if is empty string then fill
137
 
      }
138
 
    }
139
 
    static string call_handler(function f, mapping attributes, string data)
140
 
    {
141
 
        mapping params = variables;
142
 
        if ( !mappingp(attributes) )
143
 
          attributes = ([ ]);
144
 
        
145
 
        params->args = attributes;
146
 
        params->args->body = data;
147
 
        string result;
148
 
        mixed err = catch(result=f(params));
149
 
        if ( err ) {
150
 
          FATAL("SAX: error calling handler %s\n%O", err[0], err[1]);
151
 
          result = "<!-- error calling handler -->";
152
 
        }
153
 
        return result;
154
 
    }
155
 
 
156
 
    void endElementSAX(object parser, string name, void|mixed userData)
157
 
    {
158
 
        string tagstr;
159
 
        
160
 
        if ( name == "script" )
161
 
            scriptmode = 0;
162
 
        function hfunc = rxml_handlers[name];
163
 
        mapping attr = rxml_attributes[name];
164
 
        
165
 
        if ( functionp(hfunc) ) {
166
 
          tagstr = call_handler(hfunc, attr, NodeDataQueue->read());
167
 
          if ( !store_data(tagstr) )
168
 
            output += tagstr;
169
 
        }
170
 
        else if ( lower_case(name) != "br" ) 
171
 
        {
172
 
          tagstr = "</"+name+">";
173
 
          if ( !store_data(tagstr) )
174
 
            output += tagstr;
175
 
        }
176
 
    }
177
 
 
178
 
    void errorSAX(object parser, string msg, void|mixed userData) {
179
 
        output += "<!-- SAX: " + msg + "-->\n";
180
 
    }
181
 
    void cdataBlockSAX(object parser, string value, void|mixed userData)
182
 
    {
183
 
        if ( !scriptmode )
184
 
            value = replace(value, ({ "<", ">", }), ({ "&lt;", "&gt;" }));
185
 
        if ( !store_data(value) )
186
 
            output += value;
187
 
    }
188
 
    void charactersSAX(object parser, string chars, void|mixed userData)
189
 
    {
190
 
        if ( !scriptmode )
191
 
            chars = replace(chars, ({ "<", ">", }), ({ "&lt;", "&gt;" }));
192
 
        
193
 
        if ( !store_data(chars) )
194
 
            output += chars;
195
 
    }
196
 
    void commentSAX(object parser, string value, void|mixed userData) 
197
 
    {
198
 
      output += "<!--"+value+"-->\n";
199
 
    }
200
 
    void referenceSAX(object parser, string name, void|mixed userData)
201
 
    {
202
 
      werror("referenceSAX(%s)\n", name);
203
 
      output += name;
204
 
    }
205
 
    void entityDeclSAX(object parser, string name, int type, string publicId,
206
 
                       string systemId, string content, void|mixed userData)
207
 
    {
208
 
      werror("entityDecl(%s)\n", name);
209
 
      output +=name;
210
 
    }
211
 
    void notationDeclSAX(object parser, string name, string publicId, 
212
 
                         string systemId, void|mixed userData) 
213
 
    {
214
 
        werror("notationDecl(%s)\n", name);
215
 
    }
216
 
    void unparsedEntityDeclSAX(object parser, string name, string publicId, 
217
 
                               string systemId, string notationName, 
218
 
                               void|mixed userData) 
219
 
    {
220
 
        werror("unparsedEntityDecl(%s)\n", name);
221
 
    }
222
 
    string getEntitySAX(object parser, string name, void|mixed userData)
223
 
    {
224
 
        werror("getEntitySax(%s)\n", name);
225
 
    }
226
 
    void attributeDeclSAX(object parser, string elem, string fullname, 
227
 
                          int type, int def, void|mixed userData)
228
 
    {
229
 
        werror("attributeDeclSAX(%s, %s)\n", elem, fullname);
230
 
    }
231
 
    void internalSubsetSAX(object parser, string name, string externalID, 
232
 
                           string systemID, void|mixed uData)
233
 
    {
234
 
    }
235
 
    void ignorableWhitespaceSAX(object parser, string chars, void|mixed uData)
236
 
    {
237
 
    }
238
 
 
239
 
    void set_handlers(mapping h) 
240
 
    {
241
 
        rxml_handlers = h;
242
 
    }
243
 
    string get_result() 
244
 
    {
245
 
      return output;
246
 
    }
247
 
}
248
 
 
249
 
string get_tag_name(object tag)
250
 
{
251
 
  string name = tag->get_identifier();
252
 
  sscanf(name, "%s.pike", name);
253
 
  return name;
254
 
}
255
 
 
256
 
function get_tag_function(object tag)
257
 
{
258
 
  object instance;  
259
 
  if ( !objectp(tag) )
260
 
    return 0;
261
 
  instance = tag->get_instance();
262
 
  if ( !objectp(instance) )
263
 
    return 0;
264
 
 
265
 
  return instance->execute;
266
 
}
267
 
 
268
 
 
269
 
 
270
 
string parse_rxml(string|object html, mapping variables, mapping tags, string|void encoding)
271
 
{
272
 
    object cb = rxmlHandler(variables);
273
 
    string inp;
274
 
 
275
 
    cb->set_handlers(tags);
276
 
    if ( objectp(html) ) {
277
 
        encoding = html->query_attribute(DOC_ENCODING);
278
 
    }
279
 
    else if ( !stringp(encoding) )
280
 
        encoding = detect_encoding(html);
281
 
    
282
 
    encoding = lower_case(encoding);
283
 
 
284
 
    inp = html;
285
 
    if ( stringp(inp) && strlen(inp) == 0 )
286
 
      return "";
287
 
    
288
 
    object sax = xml.HTML(inp, cb, ([ ]), 0, stringp(html));
289
 
    sax->parse(encoding);
290
 
    string res = cb->get_result();
291
 
#ifndef KEEP_UTF
292
 
    // now it IS utf8 - change back to former encoding
293
 
    if ( stringp(encoding) && encoding != "utf-8" ) {
294
 
      if ( catch(res = xml.utf8_to_html(res)) ) {
295
 
        werror("HTML Conversion failed !\n");
296
 
        if ( encoding == "iso-8859-1" ) {
297
 
          if ( catch(res = xml.utf8_to_isolat1(res)) ) {
298
 
            werror("Failed conversion - skipping rxml !\n");
299
 
            return html;
300
 
          }
301
 
        }
302
 
        else {
303
 
          werror("Failed conversion - skipping !\n");
304
 
          return html; // do nothing
305
 
        }
306
 
      }
307
 
    }
308
 
#endif
309
 
    return res;
310
 
}
311
 
 
312
 
 
313
 
class testTag {
314
 
    string execute(mapping vars) {
315
 
        return "Hello World to " + vars->args->name;
316
 
    }
317
 
}
318
 
 
319
 
class tagTag {
320
 
    string execute(mapping vars) {
321
 
        return "<BODY>"+vars->args->body+"</BODY>";
322
 
    }
323
 
}
324
 
 
325
 
void test()
326
 
{
327
 
    // first test rxml
328
 
    string result = 
329
 
        "<html><body>Welcome! <h2><test name='test'/></h2></body></html>";
330
 
 
331
 
    result = parse_rxml(result, ([ ]), ([ "test": testTag()->execute, ]));
332
 
    if ( result !=
333
 
         "<html><body>Welcome! <h2>Hello World to test</h2></body></html>" )
334
 
        error("rxml test failed - wrong result " + result);
335
 
    
336
 
    result =  "<a><b>&lt;c&gt;<c apply='1'>"+
337
 
        "<d name='x'/></c>"+
338
 
        "<d name='y'/></b></a>";
339
 
 
340
 
    result = parse_rxml(result, ([ ]), ([ "d": testTag()->execute, 
341
 
                                          "c":tagTag()->execute,]));
342
 
    if ( result != 
343
 
         "<a><b><c><BODY>Hello World to x</BODY>Hello World to y</b></a>" )
344
 
        error("nested rxml test failed !");
345
 
}
346
 
 
347
 
function find_tag(string name)
348
 
{
349
 
  object tags = OBJ("/tags");
350
 
  if ( !objectp(tags) ) 
351
 
    return 0;
352
 
  object tag = tags->get_object_byname(name+".pike");
353
 
  if ( !objectp(tag) )
354
 
    return 0;
355
 
  return get_tag_function(tag);
356
 
}
357
 
 
358
 
 
359
 
mapping find_tags(object obj)
360
 
{
361
 
  if ( !objectp(obj) )
362
 
    return 0;
363
 
  if ( obj->get_object_class() & CLASS_CONTAINER ) {
364
 
    mapping result = ([ ]);
365
 
    foreach(obj->get_inventory_by_class(CLASS_DOCLPC), object tag) {
366
 
      function f = get_tag_function(tag);
367
 
      string tagname = get_tag_name(tag);
368
 
      if ( !functionp(f) )
369
 
        MESSAGE("Warning - no tag function for tag: %s", tagname);
370
 
      else
371
 
        result[tagname] = f;
372
 
    }
373
 
    return result;
374
 
  }
375
 
  else if ( obj->get_object_class() & CLASS_DOCXSL) {
376
 
    object env = obj->get_environment();
377
 
    if ( objectp(env) )
378
 
      return find_tags(env->get_object_byname("tags"));
379
 
  }
380
 
  return 0;
381
 
}
382
 
 
383
 
string describe() { return "htmllib"; }