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