style/mono: support browser and unicode pages
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $ENV{PATH_INFO};
4 $source =~ s{^/}{};
5
6 Html({
7         title => "$source source code",
8         version => 'v1.0',
9         description => !$source ? 'Index of source files for this site.' : [
10                 "Source code of the $source file at this site,",
11                 "with syntax highlighted and references linked."
12         ],
13         keywords => [qw'
14                 sheet cheat source code perl plp html agpl
15         '],
16         stylesheet => [qw'light dark mono red'],
17 });
18
19 print "\n";
20
21 if (not $source) {
22         print "<h1>Source files</h1>";
23
24         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
25         print '<ul>'."\n";
26         for (glob '*.plp') {
27                 chomp;
28                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
29         }
30         print "</ul>\n\n";
31 }
32 else {
33         print "<h1>Source of $source</h1>\n";
34
35         if ($source =~ m{(?:/|^)\.}) {
36                 die "File request not permitted\n";
37         }
38         elsif ($source =~ s{::}{/}g or !-e $source) {
39                 $source .= '.pm';
40                 for (0 .. $#{@INC}) {
41                         -e ($_ = "$INC[$_]/$source") or next;
42                         $source = $_;
43                         last;
44                 }
45         }
46         -r $source or die "Requested file not found\n";
47
48         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
49                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
50                 my %TYPETAG = (
51                         Statement => 'strong',
52                         Error     => 'em',
53                         Todo      => 'em',
54                 );
55
56                 my $hl = Text::VimColor->new(
57                         file => $source,
58                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
59                 );
60                 my $parsed = $hl->marked;
61                 print "<pre>\n";
62                 foreach (@$parsed) {
63                         my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
64                         my $arg = '';
65                         print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
66                         if (!$_->[0] || $_->[0] eq 'Constant'
67                         and $_->[1] =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
68                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
69                         }
70                         if (!$_->[0] and $_->[1] =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
71                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
72                         }
73                         print Text::VimColor::_xml_escape($_->[1]);
74                         print "</$tag>" if $tag;
75                 }
76                 print "</pre>\n";
77         }
78         else {
79                 print "<pre>\n", EscapeHTML(ReadFile($source)), "</pre>\n";
80         }
81
82         print "\n";
83 }
84