File: XML.pmod

package info (click to toggle)
pike7 7.0.361-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 14,876 kB
  • ctags: 12,334
  • sloc: ansic: 142,667; makefile: 1,526; sh: 1,035; lisp: 290; sed: 34; perl: 3
file content (73 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download
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
import ".";

string *from=({"&#a0;","&","<",">"});
string *to=({"","&","<",">"});

string unquote(string x) { return replace(x,from,to); }
string quote(string x) { return replace(x,to,from); }

string *from_data=({"&#a0;","&amp;","&lt;","&gt;","&apos;","&quot;"});
string *to_data=({"","&","<",">","'","\""});

string unquote_data(string x) { return replace(x,from_data,to_data); }
string quote_data(string x) { return replace(x,to_data,from_data); }

#define TAG object(Sgml.Tag)|string
#define SGML array(TAG)

string mktag(string tag, mapping params, int end)
{
  string ret="<"+tag;
  foreach(indices(params),string i)
    if(params[i])
      ret+=" "+i+"='"+quote_data(stringp(params[i])?params[i]:i)+"'";

  return ret + (end?"/>":">");
}

string generate(SGML data, void|function mkt)
{
  string ret="";
  if(!mkt)
  {
    mkt=mktag;
  }
  foreach(data, TAG foo)
    {
      if(stringp(foo))
      {
	ret+=quote(foo);
      }
      else if (objectp(foo))
      {
	ret+=mkt(foo->tag,foo->params,!foo->data);
	if(foo->data)
	{
#if 0
	  if(foo->tag=="script")
	  {
	    // Magic for javascript!
	    ret+="\n<!--\n"+
	      foo->data*""+
	      "// -->\n";
	  }else
#endif
	    ret+=generate(foo->data,mkt);

	  ret+=mkt("/"+foo->tag,([]),0);
	}
      }
      else error("got an illegal tag or string : %O\n"
		 "in: %O\n",foo,data);
    }

  return ret;
}


#ifdef TEST
int main()
{
  write(sprintf("%O\n",group(lex(Stdio.read_file("tutorial.wmml")))));
}
#endif