1#!/usr/bin/perl
2require HTML::TokeParser;
3
4# html2wml 0.1 13/01/2000
5# Taneli Leppa <rosmo@SEKTORI.COM>
6# License: public domain
7
8
9$error_cardstart = "<card id=\"error\" title=\"Error\" newcontext=\"true\">\n";
10$error_cardend   = "</card>\n";
11$error_filenotfound = $error_cardstart . "<p>The file was not found.</p>\n" . $error_cardend;
12
13$HTML_HEAD = 0x0001;
14$HTML_UL   = 0x0002;
15$HTML_NL   = 0x0004;
16
17# Print default headers
18#
19
20$wml = q{<?xml version="1.0"?>
21<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
22
23<wml>
24};
25
26# Read in the HTML
27#
28
29if (!($p = HTML::TokeParser->new($ARGV[0])))
30  { $wml .= $error_filenotfound; goto "FINISH"; }
31
32$p->get_tag("title");
33$title = $p->get_text;
34$wml .= "<card id=\"foo\" title=\"$title\">\n";
35
36if (!($p = HTML::TokeParser->new($ARGV[0])))
37  { $wml .= $error_filenotfound; goto "FINISH"; }
38
39$hide_text = 0;
40$current_place = 0;
41
42while ($token = $p->get_token)
43{
44    $_ = $token->[0];
45    TAGTYPE: {
46	/S/ &&  do { $wmlbit = start_tag($token->[1], $token->[2]); last TAGTYPE; };
47	/E/ &&  do { $wmlbit = end_tag($token->[1]); last TAGTYPE; };
48	/T/ &&  do { $wmlbit = $token->[1]; chomp $wmlbit; last TAGTYPE; };
49#	/D/ &&  do { $text = $token->[0]; last TAGTYPE; };
50#	/PI/ && do { $text = $token->[0]; last TAGTYPE; };
51    }
52    if (!$hide_text) { $wml .= $wmlbit; }
53}
54
55close(F);
56
57FINISH:
58
59$wml .= "\n</card>\n</wml>";
60
61print length($wml), "\n";
62print $wml;
63
64sub start_tag {
65    local($tag, $attrs) = @_;
66    my $s;
67
68    if (uc($tag) eq "LI")
69    {
70      if ($current_place & $HTML_UL)
71       { if ($list_index > 1) { $s = "<BR/>"; } $s .= "* "; }
72
73      if ($current_place & $HTML_OL)
74       { if ($list_index > 1) { $s = "<BR/>"; } $s .= " " . $list_index . ". "; }
75
76      $list_index++;
77    }
78
79
80
81    if (uc($tag) eq "BR")
82    {
83	$s = "<BR/>";
84    }
85
86    if (uc($tag) eq "UL")
87    {
88	$list_index = 1;
89	$current_place |= $HTML_UL;
90	$s = "<BR/>";
91    }
92
93    if (uc($tag) eq "NL")
94    {
95	$list_index = 1;
96	$current_place |= $HTML_NL;
97	$s = "<BR/>";
98    }
99
100    if (uc($tag) eq "H1" || uc($tag) eq "H2")
101    {
102	$s = "<STRONG>";
103    }
104
105    if (uc($tag) eq "H3" || uc($tag) eq "H4")
106    {
107	$s = "<EM>";
108    }
109
110    if (uc($tag) eq "H5" || uc($tag) eq "H6")
111    {
112	$s = "<B>";
113    }
114
115    if (uc($tag) eq "HEAD")
116    {
117	$current_place |= $HTML_HEAD;
118	$hide_text = 1;
119    }
120
121    if (uc($tag) eq "IMG")
122    {
123	if ($attrs->{"alt"} ne "")
124        {
125	    $s = $attrs->{"alt"};
126	}
127    }
128
129    if (uc($tag) eq "TEXTAREA")
130    {
131	$s = "<INPUT type=\"text\" name=\"" . $attrs->{"name"} . "\"";
132	if ($attrs->{"size"} ne "") { $s .= " size=\"" . $attrs->{"size"} . "\""; }
133	$s .= " value=\"";
134	push @form_dynamic_vars, $attrs->{"name"};
135    }
136
137    if (uc($tag) eq "INPUT")
138    {
139
140	if (uc($attrs->{"type"}) eq "TEXT")
141	{
142	    $s = "<INPUT type=\"text\" name=\"" . $attrs->{"name"} . "\" value=\"" . $attrs->{"value"} . "\"";
143	    if ($attrs->{"size"} ne "") { $s .= " size=\"" . $attrs->{"size"} . "\""; }
144            $s .= "/>\n";
145	    push @form_dynamic_vars, $attrs->{"name"};
146	}
147
148	if (uc($attrs->{"type"}) eq "SUBMIT")
149	{
150	    $form_submit_label = $attrs->{"value"};
151	}
152
153	if (uc($attrs->{"type"}) eq "PASSWORD")
154	{
155	    $s = "<INPUT type=\"text\" name=\"" . $attrs->{"name"} . "\"";
156	    if ($attrs->{"size"} ne "") { $s .= " size=\"" . $attrs->{"size"} . "\""; }
157	    $s .= "/>";
158	    push @form_dynamic_vars, $attrs->{"name"};
159	}
160
161	if (uc($attrs->{"type"}) eq "HIDDEN")
162	{
163	    push @form_static_vars, ($attrs->{"name"}, $attrs->{"value"});
164	}
165    }
166
167    if (uc($tag) eq "FORM")
168    {
169	reset(@form_dynamic_vars);
170	reset(@form_static_vars);
171        reset $form_submit_label;
172	$form_method = $attrs->{"method"};
173	$form_action = $attrs->{"action"};
174    }
175
176
177    if (uc($tag) eq "A")
178    {
179     $s = "<A href=\"" . $attrs->{"href"} . "\">";
180    }
181
182    if (uc($tag) eq "B")
183    {
184	$s = "<EM>";
185    }
186
187    if (uc($tag) eq "I" || uc($tag) eq "U")
188    {
189	$s = "<" . uc($tag) . ">";
190    }
191
192    if (uc($tag) eq "P")
193    {
194	$s = "<P/>";
195    }
196
197    return $s;
198}
199
200sub end_tag {
201    local($tag) = @_;
202    my $s;
203
204
205    if (uc($tag) eq "UL")
206    {
207	$current_place ^= $HTML_UL;
208	$s = "<BR/>";
209    }
210
211    if (uc($tag) eq "NL")
212    {
213	$current_place ^= $HTML_NL;
214	$s = "<BR/>";
215    }
216
217    if (uc($tag) eq "H1" || uc($tag) eq "H2")
218    {
219	$s = "</STRONG><BR/>";
220    }
221
222    if (uc($tag) eq "H3" || uc($tag) eq "H4")
223    {
224	$s = "</EM><BR/>";
225    }
226
227    if (uc($tag) eq "H5" || uc($tag) eq "H6")
228    {
229	$s = "</B><BR/>";
230    }
231
232    if (uc($tag) eq "HEAD")
233    {
234	$current_place ^= $HTML_HEAD;
235	$hide_text = 0;
236    }
237
238    if (uc($tag) eq "STYLE")
239    {
240	if ($current_place & $HTML_HEAD) { $hide_text = 0; }
241    }
242
243    if (uc($tag) eq "TEXTAREA")
244    {
245	$s = "\"/>";
246    }
247
248    if (uc($tag) eq "FORM")
249    {
250	$s =  "\n<DO type=\"accept\" label=\"$form_submit_label\">\n";
251        if (uc($form_method) eq "") { $form_method = "GET"; }
252        if (uc($form_method) eq "POST")
253	{
254	    $s .= "\t<GO method=\"POST\" href=\"$form_action\">\n";
255	    $z = pop @form_static_vars;
256	    while (defined($z))
257	    {
258	        $x = pop @form_static_vars;
259		$s .= "\t\t" . "<POSTFIELD name=\"$z\" value=\"$x\">\n";
260	        $z = pop @form_static_vars;
261            }
262	    foreach $z (@form_dynamic_vars)
263	    {
264		$s .= "\t\t" . "<POSTFIELD name=\"$z\" value=\"\${$z}\">\n";
265            }
266	    $s .= "\n\t</GO>\n";
267        }
268
269        if (uc($form_method) eq "GET")
270	{
271	    $s .= "\t<GO href=\"$form_action?";
272
273   	    $z = pop @form_static_vars; $i = 0;
274            while (defined($z))
275	    {
276		if ($i++ > 0) { $s .= "&"; }
277		$s .= $z;
278                $z = pop @form_static_vars;
279		if (defined($z))
280                  {
281		   $s .= "=" . $z;
282                   $z = pop @form_static_vars;
283		  }
284            }
285   	    $z = pop @form_dynamic_vars;
286            while (defined($z))
287	    {
288		if ($i++ > 0) { $s .= "&amp;"; }
289		$s .= $z . "=\${" . $z . "}";
290                $z = pop @form_dynamic_vars;
291            }
292	    $s .= "\"/>\t</GO>\n";
293        }
294	$s .= "</DO>\n";
295    }
296
297    if (uc($tag) eq "A")
298    {
299	$s = "</A>";
300    }
301
302    if (uc($tag) eq "B")
303    {
304	$s = "</EM>";
305    }
306
307    if (uc($tag) eq "I" || uc($tag) eq "U")
308    {
309	$s = "</" . uc($tag) . ">";
310    }
311
312    return $s;
313}
314
315exit 0;
316