unicode: various table selection fixes and improvements
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $ENV{PATH_INFO};
4 $source =~ s{^/}{};
5
6 if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
7         # convert perl include to json construct
8         checkmodified($source);
9         eval {
10                 my $data = do $source or die $@ || $! || 'read error';
11                 require JSON;
12                 my $converter = JSON->new;
13                 $converter->utf8->indent->space_after->canonical;
14
15                 $header{content_type} = 'application/json';
16                 $header{content_type} = 'text/plain' if exists $get{debug};
17                 print $_, '(' for $get{callback} // ();
18                 print $converter->encode($data);
19                 print     ')' for $get{callback} // ();
20                 return 1;
21         } or do {
22                 $header{status} = '500 File unavailable';
23                 $header{content_type} = 'text/plain';
24                 print "Conversion failed: $@";
25         };
26         exit;
27 }
28
29 Html({
30         title => "$source source code",
31         version => 'v1.1',
32         description => !$source ? 'Index of source files for this site.' : [
33                 "Source code of the $source file at this site,",
34                 "with syntax highlighted and references linked."
35         ],
36         keywords => [qw'
37                 sheet cheat source code perl plp html agpl
38         '],
39         stylesheet => [qw'light dark mono red'],
40 });
41
42 print "\n";
43
44 if (not $source) {
45         print "<h1>Source files</h1>";
46
47         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
48         print '<ul>'."\n";
49         for (glob '*.plp') {
50                 chomp;
51                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
52         }
53         print "</ul>\n\n";
54 }
55 else {
56         print "<h1>Source of $source</h1>\n";
57
58         if ($source =~ m{(?:/|^)\.}) {
59                 die "File request not permitted\n";
60         }
61         elsif ($source =~ s{::}{/}g or !-e $source) {
62                 $source .= '.pm';
63                 for (0 .. $#INC) {
64                         -e ($_ = "$INC[$_]/$source") or next;
65                         $source = $_;
66                         last;
67                 }
68         }
69         -r $source or die "Requested file not found\n";
70
71         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
72                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
73                 my %TYPETAG = (
74                         Statement => 'strong',
75                         Error     => 'em',
76                         Todo      => 'em',
77                 );
78
79                 my $hl = Text::VimColor->new(
80                         file => $source,
81                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
82                 );
83                 my $parsed = $hl->marked;
84                 print "<pre>\n";
85                 foreach (@$parsed) {
86                         my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
87                         my $arg = '';
88                         print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
89                         if (!$_->[0] || $_->[0] eq 'Constant'
90                         and $_->[1] =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
91                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
92                         }
93                         if (!$_->[0] and $_->[1] =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
94                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
95                         }
96                         print Text::VimColor::_xml_escape($_->[1]);
97                         print "</$tag>" if $tag;
98                 }
99                 print "</pre>\n";
100         }
101         else {
102                 require Encode;
103                 print "<pre>\n";
104                 print EscapeHTML(Encode::decode_utf8(ReadFile($source)));
105                 print "</pre>\n";
106         }
107
108         print "\n";
109 }
110