/[suikacvs]/test/html-whatpm/parser.cgi
Suika

Diff of /test/html-whatpm/parser.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by wakaba, Tue May 1 09:25:31 2007 UTC revision 1.5 by wakaba, Fri May 25 14:24:31 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2  use strict;  use strict;
3    
4  use lib qw[.. /home/wakaba/public_html/-temp/wiki/lib];  use lib qw[/home/httpd/html/www/markup/html/whatpm
5               /home/wakaba/public_html/-temp/wiki/lib];
6    use CGI::Carp qw[fatalsToBrowser];
7    
8  use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module  use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
9    
# Line 12  my $http = SuikaWiki::Input::HTTP->new; Line 14  my $http = SuikaWiki::Input::HTTP->new;
14  my $mode = $http->meta_variable ('PATH_INFO');  my $mode = $http->meta_variable ('PATH_INFO');
15  ## TODO: decode unreserved characters  ## TODO: decode unreserved characters
16    
17  if ($mode eq '/html' or $mode eq '/error-and-html') {  if ($mode eq '/html' or $mode eq '/test') {
18    require Encode;    require Encode;
19    require What::HTML;    require Whatpm::HTML;
20    require What::NanoDOM;    require Whatpm::NanoDOM;
21    
22    my $s = $http->parameter ('s');    my $s = $http->parameter ('s');
23    if (length $s > 1000_000) {    if (length $s > 1000_000) {
# Line 27  if ($mode eq '/html' or $mode eq '/error Line 29  if ($mode eq '/html' or $mode eq '/error
29    
30    print STDOUT "Content-Type: text/plain; charset=utf-8\n\n";    print STDOUT "Content-Type: text/plain; charset=utf-8\n\n";
31    
32    my $onerror = $mode eq '/error-and-html' ? sub {    print STDOUT "#errors\n";
     print STDOUT "0,0,", $_[0], "\n";  
   } : sub { };  
33    
34    my $doc = What::HTML->parse_string    my $onerror = sub {
35        ($s => What::NanoDOM::Document->new, $onerror);      my (%opt) = @_;
36        print STDOUT "$opt{line},$opt{column},$opt{type}\n";
37    if ($mode eq '/error-and-html') {    };
38      print "--\n";  
39      my $doc = Whatpm::HTML->parse_string
40          ($s => Whatpm::NanoDOM::Document->new, $onerror);
41    
42      print "#document\n";
43    
44      my $out;
45      if ($mode eq '/html') {
46        $out = Whatpm::HTML->get_inner_html ($doc);
47      } else { # test
48        $out = test_serialize ($doc);
49    }    }
50      print STDOUT Encode::encode ('utf-8', $$out);
51      print STDOUT "\n";
52    
53    my $html = What::HTML->get_inner_html ($doc);    if ($http->parameter ('dom5')) {
54    print STDOUT Encode::encode ('utf-8', $$html);      require Whatpm::ContentChecker;
55        print STDOUT "#domerrors\n";
56        my $docel = $doc->document_element;
57        my $docel_nsuri = $docel->namespace_uri;
58        if (defined $docel_nsuri and
59            $docel_nsuri eq q<http://www.w3.org/1999/xhtml> and
60            $docel->manakai_local_name eq 'html') {
61          #
62        } else {
63          print STDOUT get_node_path ($docel) . ";element not allowed\n";
64        }
65        my $cc = Whatpm::ContentChecker->new;
66        $cc->check_element ($docel, sub {
67          my %opt = @_;
68          print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";
69        });
70      }
71  } else {  } else {
72    print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404";    print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404";
73  }  }
74    
75    exit;
76    
77    sub test_serialize ($) {
78      my $node = shift;
79      my $r = '';
80    
81      my @node = map { [$_, ''] } @{$node->child_nodes};
82      while (@node) {
83        my $child = shift @node;
84        my $nt = $child->[0]->node_type;
85        if ($nt == $child->[0]->ELEMENT_NODE) {
86          $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
87    
88          for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
89                        @{$child->[0]->attributes}) {
90            $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?
91            $r .= $attr->[1] . '"' . "\x0A";
92          }
93          
94          unshift @node,
95            map { [$_, $child->[1] . '  '] } @{$child->[0]->child_nodes};
96        } elsif ($nt == $child->[0]->TEXT_NODE) {
97          $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
98        } elsif ($nt == $child->[0]->COMMENT_NODE) {
99          $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
100        } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
101          $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
102        } else {
103          $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
104        }
105      }
106      
107      return \$r;
108    } # test_serialize
109    
110    sub get_node_path ($) {
111      my $node = shift;
112      my @r;
113      while (defined $node) {
114        my $rs;
115        if ($node->node_type == 1) {
116          $rs = $node->manakai_local_name;
117          $node = $node->parent_node;
118        } elsif ($node->node_type == 2) {
119          $rs = '@' . $node->manakai_local_name;
120          $node = $node->owner_element;
121        } elsif ($node->node_type == 3) {
122          $rs = '"' . $node->data . '"';
123          $node = $node->parent_node;
124        } elsif ($node->node_type == 9) {
125          $rs = '';
126          $node = $node->parent_node;
127        } else {
128          $rs = '#' . $node->node_type;
129          $node = $node->parent_node;
130        }
131        unshift @r, $rs;
132      }
133      return join '/', @r;
134    } # get_node_path
135    
136    =head1 AUTHOR
137    
138    Wakaba <w@suika.fam.cx>.
139    
140    =head1 LICENSE
141    
142    Copyright 2007 Wakaba <w@suika.fam.cx>
143    
144    This library is free software; you can redistribute it
145    and/or modify it under the same terms as Perl itself.
146    
147    =cut
148    
149    ## $Date$

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24