root/lang/actionscript/todoshare/perl/Html.pm @ 16014

Revision 16014, 1.6 kB (checked in by hoge1e3, 5 years ago)
Line 
1package stream;
2
3sub new {
4  my $c=shift;
5  my $out=shift;
6  bless{out=>$out};     
7}
8my %noClose;
9for (split(/\n/,<<EOF)) { $noClose{$_}=1; }
10br
11input
12EOF
13
14sub p {
15  my $t=shift;
16  my $c=shift;
17  if ( (ref $c) eq "ARRAY") {
18    my $tag= $c->[0];
19    die ("No tag name: [".join(",",@{$c})."]") unless($tag);
20    if ((ref $tag) eq "ARRAY") {
21       for (@{$tag}) {
22          $t->p($_);
23       }
24       return;
25    }
26    $t->out("<$tag ");
27    my $params=$c->[1];
28    my $start=1;
29    if ((ref $params) eq "HASH") {
30       while (my ($k,$v)= each %{$params}) {
31          $t->out( &sym($k)."=".&attr($v)." " );
32       }
33       $start++;
34    }
35    $t->out(">");
36    for (my $i=$start; $i<@{$c}; $i++) {
37       $t->p($c->[$i] );
38    }
39    $t->out("</$tag>") unless($noClose{$tag});
40  } elsif ( (ref $c) eq "CODE") {
41     &{$c}($t);
42  } else {
43     $t->out(&literal($c));
44  } 
45  $t;
46}
47sub out {
48  my $t=shift;
49  my $s=shift;
50  &{$t->{out}}($s);
51  $t;
52}
53sub ln {
54  my $t=shift;
55  $t->out("\n");
56  $t;
57}
58sub brln {
59  my $t=shift;
60  $t->out("<BR>\n");
61  $t;
62}
63sub literal {
64  my $s=shift;
65  #$s =~ s/&/&amp;/g;
66  $s =~ s/"/&quot;/g;
67  $s =~ s/>/&gt;/g;
68  $s =~ s/</&lt;/g;
69  $s;
70}
71sub sym {
72  my $s=shift;
73  $s=~s/[^\w\d]//g;
74  $s;
75}
76sub attr {
77  my $s=shift;
78  '"'.&literal($s).'"';
79}
80
81package Html;
82
83our @EXPORT = qw(header p brln);
84use base qw(Exporter);
85
86sub header {
87  print "Content-type: text/html; charset=utf-8\n\n";
88}
89sub brln {
90  sub {$_[0]->out("<BR>\n");};
91}
92
93sub p {
94  my $c=shift;
95  new stream(sub {
96      my $s=shift;
97      print $s;
98  })->p($c);
99}
1001;
Note: See TracBrowser for help on using the browser.