/[suikacvs]/webroot/regexp/visualizer/regexp.cgi
Suika

Contents of /webroot/regexp/visualizer/regexp.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations) (download)
Sun Mar 8 15:14:52 2009 UTC (15 years, 8 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +13 -4 lines
++ swe/visualizer/ChangeLog	8 Mar 2009 15:14:36 -0000
2009-03-09  Wakaba  <wakaba@suika.fam.cx>

	* input.html, regexp.cgi: Added links to the document.

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3     use warnings;
4     use CGI::Carp qw(fatalsToBrowser);
5    
6 wakaba 1.3 use FindBin;
7     use lib qq[$FindBin::Bin/../lib];
8 wakaba 1.1 use lib q[/home/wakaba/work/manakai2/lib];
9 wakaba 1.4 use Message::CGI::Util qw/htescape/;
10     use Encode;
11    
12 wakaba 1.2 use Message::CGI::HTTP;
13     my $cgi = Message::CGI::HTTP->new;
14    
15 wakaba 1.4 my $regexp = decode 'utf-8', $cgi->get_parameter ('s') // '';
16 wakaba 1.1 $regexp = '(?:)' unless length $regexp;
17 wakaba 1.4 my $eregexp = htescape $regexp;
18 wakaba 1.1
19 wakaba 1.4 my $lang = $cgi->get_parameter ('l') // 'perl58';
20     my $class = $lang eq 'js'
21     ? 'Regexp::Parser::JavaScript'
22     : 'Regexp::Parser::Perl58';
23    
24     use UNIVERSAL::require;
25     $class->use or die $@;
26     my $parser = $class->new;
27 wakaba 1.1
28 wakaba 1.6 my $footer = q[
29     <footer>
30     [<a href="input">Input</a>]
31     [<a href="../doc/readme">Source</a>]
32     </footer>
33     ];
34    
35 wakaba 1.5 my @error;
36     $parser->onerror (sub {
37     my %args = @_;
38     my $r = '<li>';
39     if ($args{level} eq 'w') {
40     $r .= '<strong>Warning</strong>: ';
41     } else {
42     $r .= '<strong>Error</strong>: ';
43     }
44    
45     $r .= htescape sprintf $args{type}, @{$args{args}};
46    
47     $r .= ': <code>';
48     $r .= htescape substr ${$args{valueref}}, 0, $args{pos_start};
49     $r .= '<mark>';
50     $r .= htescape substr ${$args{valueref}},
51     $args{pos_start}, $args{pos_end} - $args{pos_start};
52     $r .= '</mark>';
53     $r .= htescape substr ${$args{valueref}}, $args{pos_end};
54     $r .= '</code></li>';
55    
56     push @error, $r;
57     });
58    
59 wakaba 1.3 eval {
60     $parser->parse ($regexp);
61     };
62 wakaba 1.2
63     if ($parser->errnum) {
64     binmode STDOUT, ':encoding(utf-8)';
65     print "Content-Type: text/html; charset=utf-8\n\n";
66     print q[<!DOCTYPE HTML><html lang=en>
67 wakaba 1.4 <title>Regular expression visualizer: ], $eregexp, q[</title>
68 wakaba 1.6 <link rel="stylesheet" href="/www/style/html/xhtml">
69    
70 wakaba 1.2 <h1>Regular expression visualizer</h1>
71    
72     <p>Input: <code>], $eregexp, q[</code></p>
73    
74 wakaba 1.5 <p>Error:
75     <ul>];
76     print join '', @error;
77     print q[</ul>];
78    
79 wakaba 1.6 print $footer;
80    
81 wakaba 1.2 exit;
82     }
83 wakaba 1.1
84 wakaba 1.3 require Regexp::Visualize::Simple;
85     my $v = Regexp::Visualize::Simple->new;
86     $v->push_regexp_node ($parser->root);
87    
88 wakaba 1.1 binmode STDOUT, ':encoding(utf-8)';
89     print "Content-Type: application/xhtml+xml; charset=utf-8\n\n";
90    
91 wakaba 1.2 print q[<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
92 wakaba 1.4 <head><title>Regular expression visualizer: ], $eregexp, q[</title>
93 wakaba 1.2 <link rel="stylesheet" href="/www/style/html/xhtml"/>
94 wakaba 1.1 </head>
95 wakaba 1.2 <body>
96     <h1>Regular expression visualizer</h1>
97    
98     <p>Input: <code>], $eregexp, q[</code></p>];
99 wakaba 1.5
100     if (@error) {
101     print q[<ul>];
102     print join '', @error;
103     print q[</ul>];
104     }
105 wakaba 1.1
106 wakaba 1.3 while ($v->has_regexp_node) {
107     my ($g, $index) = $v->next_graph;
108 wakaba 1.1
109 wakaba 1.2 print "<section><h2>Regexp #$index</h2>\n\n";
110 wakaba 1.1 print $g->as_svg;
111     print "</section>\n";
112     }
113    
114 wakaba 1.6 print $footer;
115    
116 wakaba 1.1 print q[</body></html>];

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24