termcol: fix index numbers of 88-colour greyscale ramp
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2         our $VERSION = 'v1.0';
3
4         my $source = $ENV{PATH_INFO};
5         $source =~ s{^/}{};
6
7 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
8  "http://www.w3.org/TR/html4/loose.dtd">
9 <html lang="en">
10
11 <head>
12 <meta http-equiv="content-type" content="<:= $header{content_type} :>">
13 <title><:= $source :> source code</title>
14 <meta name="description" content="<:=
15         $source ? "Source code of local $source file"
16                   . ", with syntax highlighted and references linked."
17                 : 'Index of source files for this site.'
18 :>">
19 <meta name="keywords" content="sheet, cheat, source, code, perl, plp, html, agpl">
20 <:= stylesheet(qw'light dark mono red') :>
21 <link rel="icon" type="image/png" href="/clip.png">
22 </head>
23
24 <body id="source">
25 <:
26
27 if (not $source) {
28         print "<h1>Source files</h1>";
29
30         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
31         print '<ul>'."\n";
32         for (glob '*.plp') {
33                 chomp;
34                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
35         }
36         print "</ul>\n\n";
37 }
38 else {
39         print "<h1>Source of $source</h1>\n";
40
41         if ($source =~ m{(?:/|^)\.}) {
42                 die "File request not permitted\n";
43         }
44         elsif ($source =~ s{::}{/}g or !-e $source) {
45                 $source .= '.pm';
46                 for (0 .. $#{@INC}) {
47                         -e ($_ = "$INC[$_]/$source") or next;
48                         $source = $_;
49                         last;
50                 }
51         }
52         -r $source or die "Requested file not found\n";
53
54         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
55                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
56                 my %TYPETAG = (
57                         Statement => 'strong',
58                         Error     => 'em',
59                         Todo      => 'em',
60                 );
61
62                 my $hl = Text::VimColor->new(
63                         file => $source,
64                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
65                 );
66                 my $parsed = $hl->marked;
67                 print "<pre>\n";
68                 foreach (@$parsed) {
69                         my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
70                         my $arg = '';
71                         print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
72                         if (!$_->[0] || $_->[0] eq 'Constant'
73                         and $_->[1] =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
74                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
75                         }
76                         if (!$_->[0] and $_->[1] =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
77                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
78                         }
79                         print Text::VimColor::_xml_escape($_->[1]);
80                         print "</$tag>" if $tag;
81                 }
82                 print "</pre>\n";
83         }
84         else {
85                 print "<pre>\n", EscapeHTML(ReadFile($source)), "</pre>\n";
86         }
87 }
88 :>
89 <p class="footer">
90         <a href="/" rel="home">sheet.shiar.nl</a>/source.<a href="/source/source.plp"
91          rel="code" title="Written in Perl">plp</a>
92         <a href="http://git.shiar.nl/sheet.git/history/HEAD:/source.plp"
93          rel="vcs-git" title="Git repository"><:= $VERSION :></a>
94         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
95         <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html" rel="copyright"
96          title="Licensed under the GNU Affero General Public License, version 3">AGPLv3</a>
97 </p>
98
99 </html>