931b3dc90556aba1f6a6a28c682a4d8274e8574c
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $Request;
4 my $incname = qr{ [a-z][/a-z0-9_.-]* \.(?:plp?|css|js) }x;
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 => '1.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         data => [$source =~ m{\A($incname)\z}],
41 });
42
43 say '';
44
45 if (not $source) {
46         print "<h1>Source files</h1>";
47
48         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
49         say '<ul>';
50         for (glob '*.plp') {
51                 chomp;
52                 say '<li>', showlink($_, "/source/$_");
53         }
54         say "</ul>\n";
55 }
56 else {
57         say "<h1>Source of $source</h1>";
58
59         if ($source =~ m{(?:/|^)\.}) {
60                 die "File request not permitted\n";
61         }
62         elsif ($source =~ s{::}{/}g or !-e $source) {
63                 $source .= '.pm';
64                 for (0 .. $#INC) {
65                         -e ($_ = "$INC[$_]/$source") or next;
66                         $source = $_;
67                         last;
68                 }
69         }
70         -r $source or die "Requested file not found\n";
71         my $size = (stat $source)->[7];
72
73         if (my $hl = eval {
74                 $size < 32_768 or die 'large files take too long to parse';
75                 require Text::VimColor;
76                 Text::VimColor->VERSION(0.12)
77                         or die 'early versions are buggy under FastCGI';
78                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
79                 return Text::VimColor->new(
80                         file => $source,
81                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
82                 )->marked;
83         }) {
84                 my %TYPETAG = (
85                         Statement => 'strong',
86                         Error     => 'em',
87                         Todo      => 'em',
88                 );
89
90                 say '<pre>';
91                 foreach (@{$hl}) {
92                         my ($type, $contents) = @{$_};
93                         $contents = decode_utf8($contents);
94                         my $tag = $type && ($TYPETAG{$type} || 'span');
95                         my $arg = '';
96                         print "<$tag$arg class=\"sy-\l$type\">" if $tag;
97                         if (!$type || $type eq 'Constant'
98                         and $contents =~ s{^(['"]?)($incname)(?=\1$)}{}) {
99                                 # link other page sources, stylesheets, and javascript
100                                 print $1 . showlink($2, "/source/$2");
101                         }
102                         if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
103                                 # link perl module names (Xx::Xx...)
104                                 print $1 . showlink($2, "/source/$2");
105                         }
106                         if ($type && $type eq 'Comment'
107                         and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
108                                 # link generator scripts (by tools/...)
109                                 print $1 . showlink($2, "/source/$2");
110                         }
111                         print Text::VimColor::_xml_escape($contents);
112                         print "</$tag>" if $tag;
113                 }
114                 say '</pre>';
115         }
116         else {
117                 say '<pre>';
118                 print EscapeHTML(decode_utf8(ReadFile($source)));
119                 say '</pre>';
120         }
121
122         say '';
123 }
124