--- test/html-webhacc/WebHACC/Output.pm 2008/07/21 05:24:32 1.3 +++ test/html-webhacc/WebHACC/Output.pm 2008/07/27 10:33:46 1.9 @@ -17,7 +17,9 @@ }; sub new ($) { - return bless {nav => []}, shift; + require WebHACC::Input; + return bless {nav => [], section_rank => 1, + input => WebHACC::Input->new}, shift; } # new sub input ($;$) { @@ -25,7 +27,7 @@ if (defined $_[1]) { $_[0]->{input} = $_[1]; } else { - delete $_[0]->{input}; + $_[0]->{input} = WebHACC::Input->new; } } @@ -91,22 +93,105 @@ sub start_section ($%) { my ($self, %opt) = @_; + + if (defined $opt{role}) { + if ($opt{role} eq 'parse-errors') { + $opt{id} ||= 'parse-errors'; + $opt{title} ||= 'Parse Errors Section'; + $opt{short_title} ||= 'Parse Errors'; + delete $opt{role}; + } elsif ($opt{role} eq 'structure-errors') { + $opt{id} ||= 'document-errors'; + $opt{title} ||= 'Structural Errors'; + $opt{short_title} ||= 'Struct. Errors'; + delete $opt{role}; + } elsif ($opt{role} eq 'reformatted') { + $opt{id} ||= 'document-tree'; + $opt{title} ||= 'Reformatted Document Source'; + $opt{short_title} ||= 'Reformatted'; + delete $opt{role} + } elsif ($opt{role} eq 'tree') { + $opt{id} ||= 'document-tree'; + $opt{title} ||= 'Document Tree'; + $opt{short_title} ||= 'Tree'; + delete $opt{role}; + } elsif ($opt{role} eq 'structure') { + $opt{id} ||= 'document-structure'; + $opt{title} ||= 'Document Structure'; + $opt{short_title} ||= 'Structure'; + delete $opt{role}; + } + } + + $self->{section_rank}++; $self->html ('
input->id_prefix . $opt{id}; $self->html (' id="' . $htescape->($id) . '"'); - push @{$self->{nav}}, [$id => $opt{short_title} || $opt{title}] - unless $self->input->nested; + push @{$self->{nav}}, + [$id => $opt{short_title} || $opt{title} => $opt{text}] + if $self->{section_rank} == 2; } - $self->html ('>

' . $htescape->($opt{title}) . '

'); + my $section_rank = $self->{section_rank}; + $section_rank = 6 if $section_rank > 6; + $self->html ('>'); + $self->nl_text ($opt{title}, text => $opt{text}); + $self->html (''); } # start_section sub end_section ($) { my $self = shift; $self->html ('
'); $self->{handle}->flush; + $self->{section_rank}--; } # end_section +sub start_error_list ($%) { + my ($self, %opt) = @_; + + if (defined $opt{role}) { + if ($opt{role} eq 'parse-errors') { + $opt{id} ||= 'parse-errors-list'; + delete $opt{role}; + } elsif ($opt{role} eq 'structure-errors') { + $opt{id} ||= 'document-errors-list'; + delete $opt{role}; + } + } + + $self->start_tag ('dl', %opt); +} # start_error_list + +sub end_error_list ($%) { + my ($self, %opt) = @_; + + if (defined $opt{role}) { + if ($opt{role} eq 'parse-errors') { + delete $opt{role}; + $self->end_tag ('dl'); + ## NOTE: For parse error list, the |add_source_to_parse_error_list| + ## method is invoked at the end of |generate_source_string_section|, + ## since that generation method is invoked after the error list + ## is generated. + } elsif ($opt{role} eq 'structure-errors') { + delete $opt{role}; + $self->end_tag ('dl'); + $self->add_source_to_parse_error_list ('document-errors-list'); + } else { + $self->end_tag ('dl'); + } + } else { + $self->end_tag ('dl'); + } +} # end_error_list + +sub add_source_to_parse_error_list ($$) { + my $self = shift; + + $self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix . + q[', '] . shift () . q[')]); +} # add_source_to_parse_error_list + sub start_code_block ($) { shift->html ('
');
 } # start_code_block
@@ -132,9 +217,38 @@
 sub dt ($$;%) {
   my ($self, $content, %opt) = @_;
   $self->start_tag ('dt', %opt);
-  $self->text ($content);
+  $self->nl_text ($content, text => $opt{text});
 } # dt
 
+sub select ($$%) {
+  my ($self, $options, %opt) = @_;
+
+  my $selected = $opt{selected};
+  delete $opt{selected};
+
+  $self->start_tag ('select', %opt);
+  
+  my @options = @$options;
+  while (@options) {
+    my $opt = shift @options;
+    if ($opt->{options}) {
+      $self->html ('');
+      unshift @options, @{$opt->{options}}, {end_options => 1};
+    } elsif ($opt->{end_options}) {
+      $self->end_tag ('optgroup');
+    } else {
+      $self->start_tag ('option', value => $opt->{value},
+                        ((defined $selected and $opt->{value} eq $selected)
+                             ? (selected => '') : ()));
+      $self->nl_text (defined $opt->{label} ? $opt->{label} : $opt->{value});
+    }
+  }
+
+  $self->end_tag ('select');
+} # select
+
 sub link ($$%) {
   my ($self, $content, %opt) = @_;
   $self->start_tag ('a', %opt, href => $opt{url});
@@ -145,7 +259,7 @@
 sub xref ($$%) {
   my ($self, $content, %opt) = @_;
   $self->html ('');
-  $self->text ($content);
+  $self->nl_text ($content, text => $opt{text});
   $self->html ('');
 } # xref
 
@@ -155,7 +269,6 @@
   $self->link ($content, %opt);
 } # link_to_webhacc
 
-
 my $get_node_path = sub ($) {
   my $node = shift;
   my @r;
@@ -188,15 +301,254 @@
   $self->xref ($get_node_path->($node), target => 'node-' . refaddr $node);
 } # node_link
 
+{
+  my $Msg = {};
+
+sub load_text_catalog ($$) {
+  my $self = shift;
+
+  my $lang = shift; # MUST be a canonical lang name
+  my $file_name = qq[cc-msg.$lang.txt];
+  $lang = 'en' unless -f $file_name;
+  $self->{primary_language} = $lang;
+  
+  open my $file, '<:utf8', $file_name or die "$0: $file_name: $!";
+  while (<$file>) {
+    if (s/^([^;]+);([^;]*);//) {
+      my ($type, $cls, $msg) = ($1, $2, $_);
+      $msg =~ tr/\x0D\x0A//d;
+      $Msg->{$type} = [$cls, $msg];
+    }
+  }
+} # load_text_catalog
+
+sub nl_text ($$;%) {
+  my ($self, $type, %opt) = @_;
+  my $node = $opt{node};
+
+  my @arg;
+  {
+    if (defined $Msg->{$type}) {
+      my $msg = $Msg->{$type}->[1];
+      if ($msg =~ //) {
+        $msg =~ s{\$([0-9]+)}{
+          defined $arg[$1] ? $htescape->($arg[$1]) : '(undef)';
+        }ge;
+        $msg =~ s{{\@([A-Za-z0-9:_.-]+)}}{
+          UNIVERSAL::can ($node, 'get_attribute_ns')
+              ? $htescape->($node->get_attribute_ns (undef, $1)) : ''
+        }ge;
+        $msg =~ s{{\@}}{
+          UNIVERSAL::can ($node, 'value') ? $htescape->($node->value) : ''
+        }ge;
+        $msg =~ s{{text}}{
+          defined $opt{text} ? $htescape->($opt{text}) : ''
+        }ge;
+        $msg =~ s{{local-name}}{
+          UNIVERSAL::can ($node, 'manakai_local_name')
+            ? $htescape->($node->manakai_local_name) : ''
+        }ge;
+        $msg =~ s{{element-local-name}}{
+          (UNIVERSAL::can ($node, 'owner_element') and
+           $node->owner_element)
+            ? $htescape->($node->owner_element->manakai_local_name) : ''
+        }ge;
+      }
+      $self->html ($msg);
+      return;
+    } elsif ($type =~ s/:([^:]*)$//) {
+      unshift @arg, $1;
+      redo;
+    }
+  }
+  $self->text ($type);
+} # nl_text
+
+}
+
 sub nav_list ($) {
   my $self = shift;
   $self->html (q[');
 } # nav_list
 
+sub http_header ($) {
+  shift->html (qq[Content-Type: text/html; charset=utf-8\n\n]);
+} # http_header
+
+sub http_error ($$) {
+  my $self = shift;
+  my $code = 0+shift;
+  my $text = {
+    404 => 'Not Found',
+  }->{$code};
+  $self->html (qq[Status: $code $text\nContent-Type: text/html ; charset=us-ascii\n\n$code $text]);
+} # http_error
+
+sub html_header ($) {
+  my $self = shift;
+  $self->html (q[]);
+  $self->start_tag ('html', lang => $self->{primary_language});
+  $self->html (q[]);
+  $self->nl_text (q[WebHACC:Title]);
+  $self->html (q[
+
+
+
+
+

]); + $self->nl_text (q[WebHACC:Heading]); + $self->html ('

'); +} # html_header + +sub generate_input_section ($$) { + my ($out, $cgi) = @_; + + my $options = sub ($) { + my $context = shift; + + $out->html (q[

]); + $out->nl_text (q[Options]); + $out->start_tag ('div'); + + if ($context eq 'url') { + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->start_tag ('input', type => 'checkbox', name => 'error-page', + value => 1, + ($cgi->get_parameter ('error-page') + ? (checked => '') : ())); + $out->nl_text ('Check error page'); + $out->end_tag ('label'); + } + + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->nl_text (q[Content type]); + $out->text (': '); + $out->select ([ + {value => '', label => 'As specified'}, + {value => 'application/atom+xml'}, + {value => 'application/xhtml+xml'}, + {value => 'application/xml'}, + {value => 'text/html'}, + {value => 'text/xml'}, + {value => 'text/css'}, + {value => 'text/cache-manifest'}, + {value => 'text/x-webidl'}, + ], name => 'i', selected => scalar $cgi->get_parameter ('i')); + $out->end_tag ('label'); + + if ($context ne 'text') { + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->nl_text (q[Charset]); + $out->text (q[: ]); + $out->select ([ + {value => '', label => 'As specified'}, + {label => 'Japanese charsets', options => [ + {value => 'Windows-31J'}, + {value => 'Shift_JIS'}, + {value => 'EUC-JP'}, + {value => 'ISO-2022-JP'}, + ]}, + {label => 'European charsets', options => [ + {value => 'Windows-1252'}, + {value => 'ISO-8859-1'}, + {value => 'US-ASCII'}, + ]}, + {label => 'Asian charsets', options => [ + {value => 'Windows-874'}, + {value => 'ISO-8859-11'}, + {value => 'TIS-620'}, + ]}, + {label => 'Unicode charsets', options => [ + {value => 'UTF-8'}, + {value => 'UTF-8n'}, + ]}, + ], name => 'charset', + selected => scalar $cgi->get_parameter ('charset')); + $out->end_tag ('label'); + } + + if ($context eq 'text') { + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->nl_text ('Setting innerHTML'); + $out->text (': '); + $out->start_tag ('input', name => 'e', + value => scalar $cgi->get_parameter ('e')); + $out->end_tag ('label'); + } + + $out->html (q[

]); + }; # $options + + $out->start_section (id => 'input', title => 'Input'); + + $out->start_section (id => 'input-url', title => 'By URL'); + $out->start_tag ('form', action => './', 'accept-charset' => 'utf-8', + method => 'get'); + $out->start_tag ('input', type => 'hidden', name => '_charset_'); + + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->nl_text ('URL'); + $out->text (': '); + $out->start_tag ('input', + name => 'uri', + type => 'url', + value => $cgi->get_parameter ('uri')); + $out->end_tag ('label'); + + $options->('url'); + + $out->start_tag ('p'); + $out->start_tag ('button', type => 'submit'); + $out->nl_text ('Check'); + + $out->end_tag ('form'); + $out->end_section; + + $out->end_tag ('fieldset'); + + ## TODO: File upload + + $out->start_section (id => 'input-text', title => 'By direct input'); + $out->start_tag ('form', action => './', 'accept-charset' => 'utf-8', + method => 'post'); + $out->start_tag ('input', type => 'hidden', name => '_charset_'); + + $out->start_tag ('p'); + $out->start_tag ('label'); + $out->nl_text ('Document source to check'); + $out->text (': '); + $out->start_tag ('br'); + $out->start_tag ('textarea', + name => 's'); + my $s = $cgi->get_parameter ('s'); + $out->text ($s) if defined $s; + $out->end_tag ('textarea'); + $out->end_tag ('label'); + + $options->('text'); + + $out->start_tag ('p'); + $out->start_tag ('button', type => 'submit', + onclick => 'form.method = form.s.value.length > 512 ? "post" : "get"'); + $out->nl_text ('Check'); + $out->end_tag ('button'); + + $out->end_tag ('form'); + $out->end_section; + + $out->end_section; +} # generate_input_section sub encode_url_component ($$) { shift;