--- test/html-webhacc/cc.cgi 2007/07/16 13:56:26 1.12
+++ test/html-webhacc/cc.cgi 2008/07/20 16:53:10 1.54
@@ -1,47 +1,36 @@
#!/usr/bin/perl
use strict;
+use utf8;
use lib qw[/home/httpd/html/www/markup/html/whatpm
- /home/wakaba/work/manakai/lib
- /home/wakaba/public_html/-temp/wiki/lib];
+ /home/wakaba/work/manakai2/lib];
use CGI::Carp qw[fatalsToBrowser];
use Scalar::Util qw[refaddr];
-use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
+ require WebHACC::Input;
+ require WebHACC::Result;
+ require WebHACC::Output;
-sub htescape ($) {
- my $s = $_[0];
- $s =~ s/&/&/g;
- $s =~ s/</g;
- $s =~ s/>/>/g;
- $s =~ s/"/"/g;
- $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{
- sprintf 'U+%04X', ord $1;
- }ge;
- return $s;
-} # htescape
+my $out;
-my $http = SuikaWiki::Input::HTTP->new;
-
-## TODO: _charset_
+ require Message::DOM::DOMImplementation;
+ my $dom = Message::DOM::DOMImplementation->new;
+{
+ use Message::CGI::HTTP;
+ my $http = Message::CGI::HTTP->new;
- if ($http->meta_variable ('PATH_INFO') ne '/') {
+ if ($http->get_meta_variable ('PATH_INFO') ne '/') {
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";
exit;
}
-
- binmode STDOUT, ':utf8';
-
- require Message::DOM::DOMImplementation;
- my $dom = Message::DOM::DOMImplementation->new;
-
- my $input = get_input_document ($http, $dom);
- my $inner_html_element = $http->parameter ('e');
-
+
load_text_catalog ('en'); ## TODO: conneg
- my @nav;
- print STDOUT qq[Content-Type: text/html; charset=utf-8
+ $out = WebHACC::Output->new;
+ $out->handle (*STDOUT);
+ $out->set_utf8;
+ $out->set_flush;
+ $out->html (qq[Content-Type: text/html; charset=utf-8
@@ -50,169 +39,201 @@
-Web Document Conformance Checker (beta)
+
+]);
-
-
-- Request URI
- <@{[htescape $input->{request_uri}]}>
-- Document URI
- <@{[htescape $input->{uri}]}>
-]; # no
yet
- push @nav, ['#document-info' => 'Information'];
+ my $input = get_input_document ($http, $dom);
+ $out->input ($input);
+ $out->unset_flush;
-if (defined $input->{s}) {
+ my $char_length = 0;
- print STDOUT qq[
-
Base URI
-
<@{[htescape $input->{base_uri}]}>
-
Internet Media Type
-
@{[htescape $input->{media_type}]}
- @{[$input->{media_type_overridden} ? '(overridden)' : '']}
-
Character Encoding
-
@{[defined $input->{charset} ? ''.htescape ($input->{charset}).'
' : '(none)']}
- @{[$input->{charset_overridden} ? '(overridden)' : '']}
+ $out->start_section (id => 'document-info', title => 'Information');
+ $out->html (qq[
+- Request URL
+ - ]);
+ $out->url ($input->{request_uri});
+ $out->html (q[
- Document URL
+
- ]);
+ $out->url ($input->{uri}, id => 'anchor-document-url');
+ $out->html (q[
+ ]);
+ ## NOTE: no
yet
+
+ if (defined $input->{s}) {
+ $char_length = length $input->{s};
+
+ $out->html (qq[
Base URI]);
+ $out->url ($input->{base_uri});
+ $out->html (qq[Internet Media Type
+
]);
+ $out->text ($input->{media_type});
+ $out->html (qq[
]);
+ if ($input->{media_type_overridden}) {
+ $out->html ('(overridden)');
+ } elsif (defined $input->{official_type}) {
+ if ($input->{media_type} eq $input->{official_type}) {
+ #
+ } else {
+ $out->html ('(sniffed; official type is: ');
+ $out->text ($input->{official_type});
+ $out->html ('
)');
+ }
+ } else {
+ $out->html ('(sniffed)');
+ }
+ $out->html (q[Character Encoding]);
+ if (defined $input->{charset}) {
+ $out->html ('');
+ $out->text ($input->{charset});
+ $out->html ('
');
+ } else {
+ $out->text ('(none)');
+ }
+ $out->html (' overridden') if $input->{charset_overridden};
+ $out->html (qq[
+Length
+ $char_length byte@{[$char_length == 1 ? '' : 's']}
-
-];
- print_http_header_section ($input);
-
- my $doc;
- my $el;
-
- if ($input->{media_type} eq 'text/html') {
- require Encode;
- require Whatpm::HTML;
-
- $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now.
-
- my $t = Encode::decode ($input->{charset}, $input->{s});
-
- print STDOUT qq[
-
-
Parse Errors
+
+]);
+ $out->end_section;
+
+ my $result = WebHACC::Result->new;
+ $result->output ($out);
+ $result->{conforming_min} = 1;
+ $result->{conforming_max} = 1;
+ check_and_print ($input => $result => $out);
+ $result->generate_result_section;
+ } else {
+ $out->html ('');
+ $out->end_section;
-
];
- push @nav, ['#parse-errors' => 'Parse Error'];
+ my $result = WebHACC::Result->new;
+ $result->output ($out);
+ $result->{conforming_min} = 0;
+ $result->{conforming_max} = 1;
- my $onerror = sub {
- my (%opt) = @_;
- my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level});
- if ($opt{column} > 0) {
- print STDOUT qq[- Line $opt{line} column $opt{column}
\n];
- } else {
- $opt{line} = $opt{line} - 1 || 1;
- print STDOUT qq[- Line $opt{line}
\n];
- }
- $type =~ tr/ /-/;
- $type =~ s/\|/%7C/g;
- $msg .= qq[ [Description]];
- print STDOUT qq[- $msg
\n];
- };
-
- $doc = $dom->create_document;
- if (defined $inner_html_element and length $inner_html_element) {
- $el = $doc->create_element_ns
- ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);
- Whatpm::HTML->set_inner_html ($el, $t, $onerror);
- } else {
- Whatpm::HTML->parse_string ($t => $doc, $onerror);
+ $input->generate_transfer_sections ($result);
+ $result->generate_result_section;
}
- print STDOUT qq[
-
-];
+ $out->nav_list;
- print_source_string_section (\($input->{s}), $input->{charset});
- } elsif ({
- 'text/xml' => 1,
- 'application/xhtml+xml' => 1,
- 'application/xml' => 1,
- }->{$input->{media_type}}) {
- require Message::DOM::XMLParserTemp;
+ exit;
+}
- print STDOUT qq[
-
-
Parse Errors
+sub check_and_print ($$$) {
+ my ($input, $result, $out) = @_;
+ my $original_input = $out->input;
+ $out->input ($input);
+
+ $input->generate_transfer_sections ($result);
+
+ my @subdoc;
+
+ my $checker_class = {
+ 'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
+ 'text/css' => 'WebHACC::Language::CSS',
+ 'text/html' => 'WebHACC::Language::HTML',
+ 'text/x-webidl' => 'WebHACC::Language::WebIDL',
+
+ 'text/xml' => 'WebHACC::Language::XML',
+ 'application/atom+xml' => 'WebHACC::Language::XML',
+ 'application/rss+xml' => 'WebHACC::Language::XML',
+ 'image/svg+xml' => 'WebHACC::Language::XML',
+ 'application/xhtml+xml' => 'WebHACC::Language::XML',
+ 'application/xml' => 'WebHACC::Language::XML',
+ ## TODO: Should we make all XML MIME Types fall
+ ## into this category?
+
+ ## NOTE: This type has different model from normal XML types.
+ 'application/rdf+xml' => 'WebHACC::Language::XML',
+ }->{$input->{media_type}} || 'WebHACC::Language::Default';
+
+ eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
+ my $checker = $checker_class->new;
+ $checker->input ($input);
+ $checker->output ($out);
+ $checker->result ($result);
+
+ ## TODO: A cache manifest MUST be text/cache-manifest
+ ## TODO: WebIDL media type "text/x-webidl"
+
+ $checker->generate_syntax_error_section;
+ $checker->generate_source_string_section;
+
+ $checker->onsubdoc (sub {
+ push @subdoc, shift;
+ });
+
+ $checker->generate_structure_dump_section;
+ $checker->generate_structure_error_section;
+ $checker->generate_additional_sections;
-
];
- push @nav, ['#parse-errors' => 'Parse Error'];
+=pod
- my $onerror = sub {
- my $err = shift;
- my $line = $err->location->line_number;
- print STDOUT qq[- Line $line column ];
- print STDOUT $err->location->column_number, "
- ";
- print STDOUT htescape $err->text, "
\n";
- return 1;
- };
-
- open my $fh, '<', \($input->{s});
- $doc = Message::DOM::XMLParserTemp->parse_byte_stream
- ($fh => $dom, $onerror, charset => $input->{charset});
-
- print STDOUT qq[
-
+ if (defined $doc or defined $el) {
-];
- print_source_string_section (\($input->{s}), $doc->input_encoding);
- } else {
- ## TODO: Change HTTP status code??
- print STDOUT qq[
-
-
Media type @{[htescape $input->{media_type}]}
is not supported!
-
-];
- push @nav, ['#result-summary' => 'Result'];
+ print_table_section ($input, $elements->{table}) if @{$elements->{table}};
+ print_listing_section ({
+ id => 'identifiers', label => 'IDs', heading => 'Identifiers',
+ }, $input, $elements->{id}) if keys %{$elements->{id}};
+ print_listing_section ({
+ id => 'terms', label => 'Terms', heading => 'Terms',
+ }, $input, $elements->{term}) if keys %{$elements->{term}};
+ print_listing_section ({
+ id => 'classes', label => 'Classes', heading => 'Classes',
+ }, $input, $elements->{class}) if keys %{$elements->{class}};
+
+ print_rdf_section ($input, $elements->{rdf}) if @{$elements->{rdf}};
}
+=cut
- if (defined $doc or defined $el) {
- print STDOUT qq[
-
-
Document Tree
-];
- push @nav, ['#document-tree' => 'Tree'];
-
- print_document_tree ($el || $doc);
-
+ my $id_prefix = 0;
+ for my $_subinput (@subdoc) {
+ my $subinput = WebHACC::Input->new;
+ $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
+ $subinput->id_prefix ('subdoc-' . ++$id_prefix);
+ $subinput->nested (1);
+ $subinput->{base_uri} = $subinput->{container_node}->base_uri
+ unless defined $subinput->{base_uri};
+ my $ebaseuri = htescape ($subinput->{base_uri});
+ $out->start_section (id => $subinput->id_prefix,
+ title => qq[Subdocument #$id_prefix]);
print STDOUT qq[
-
+
+ - Internet Media Type
+ @{[htescape $subinput->{media_type}]}
+ - Container Node
+ - @{[get_node_link ($input, $subinput->{container_node})]}
+ - Base URI
+ <$ebaseuri>
+
];
+
+ $subinput->{id_prefix} .= '-';
+ check_and_print ($subinput => $result => $out);
-
-
Document Errors
-
-
];
- push @nav, ['#document-errors' => 'Document Error'];
-
- require Whatpm::ContentChecker;
- my $onerror = sub {
- my %opt = @_;
- my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level});
- $type =~ tr/ /-/;
- $type =~ s/\|/%7C/g;
- $msg .= qq[ [Description]];
- print STDOUT qq[- ] . get_node_link ($opt{node}) .
- qq[
\n- ], $msg, "
\n";
- };
-
- my $elements;
- if ($el) {
- $elements = Whatpm::ContentChecker->check_element ($el, $onerror);
- } else {
- $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);
- }
-
- print STDOUT qq[
-
-];
+ $out->end_section;
+ }
- if (@{$elements->{table}}) {
- require JSON;
+ $out->input ($original_input);
+} # check_and_print
- print STDOUT qq[
-
+sub print_table_section ($$) {
+ my ($input, $tables) = @_;
+
+# push @nav, [qq[#$input->{id_prefix}tables] => 'Tables']
+# unless $input->{nested};
+ print STDOUT qq[
+
];
}
+
+ print STDOUT qq[
];
+} # print_table_section
- ## TODO: Show result
-} else {
+sub print_listing_section ($$$) {
+ my ($opt, $input, $ids) = @_;
+
+# push @nav, ['#' . $input->{id_prefix} . $opt->{id} => $opt->{label}]
+# unless $input->{nested};
print STDOUT qq[
-
-
+