a813db4a3cd03cc4c4899e4dd59abbb0d78abc8d
[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 </head>
22
23 <body id="source">
24 <:
25
26 if (not $source) {
27         print "<h1>Source files</h1>";
28
29         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
30         print '<ul>'."\n";
31         for (glob '*.plp') {
32                 chomp;
33                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
34         }
35         print "</ul>\n\n";
36 }
37 else {
38         print "<h1>Source of $source</h1>\n";
39
40         if ($source =~ m{(?:/|^)\.}) {
41                 die "File request not permitted\n";
42         }
43         elsif ($source =~ s{::}{/}g or !-e $source) {
44                 $source .= '.pm';
45                 for (0 .. $#{@INC}) {
46                         -e ($_ = "$INC[$_]/$source") or next;
47                         $source = $_;
48                         last;
49                 }
50         }
51         -r $source or die "Requested file not found\n";
52
53         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
54                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
55                 my %TYPETAG = (
56                         Statement => 'strong',
57                         Error     => 'em',
58                         Todo      => 'em',
59                 );
60
61                 my $hl = Text::VimColor->new(
62                         file => $source,
63                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
64                 );
65                 my $parsed = $hl->marked;
66                 print "<pre>\n";
67                 foreach (@$parsed) {
68                         my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
69                         my $arg = '';
70                         print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
71                         if (!$_->[0] || $_->[0] eq 'Constant'
72                         and $_->[1] =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
73                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
74                         }
75                         if (!$_->[0] and $_->[1] =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
76                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
77                         }
78                         print Text::VimColor::_xml_escape($_->[1]);
79                         print "</$tag>" if $tag;
80                 }
81                 print "</pre>\n";
82         }
83         else {
84                 print "<pre>\n", EscapeHTML(ReadFile($source)), "</pre>\n";
85         }
86 }
87 :>
88 <p class="footer">
89         <a href="/" rel="home">sheet.shiar.nl</a>/source.<a href="/source/source.plp"
90          rel="code" title="Written in Perl">plp</a>
91         <a href="http://git.shiar.nl/sheet.git/history/HEAD:/source.plp"
92          rel="vcs-git" title="Git repository"><:= $VERSION :></a>
93         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
94         <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html" rel="copyright"
95          title="Licensed under the GNU Affero General Public License, version 3">AGPLv3</a>
96 </p>
97
98 </html>