digits: include (common) class legend
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $Request;
4
5 if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
6         # convert perl include to json construct
7         checkmodified($source);
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 => '1.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 say '';
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         say '<ul>';
48         for (glob '*.plp') {
49                 chomp;
50                 say '<li>', showlink($_, "/source/$_");
51         }
52         say "</ul>\n";
53 }
54 else {
55         say "<h1>Source of $source</h1>";
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         my $size = (stat $source)->[7];
70
71         if (my $hl = eval {
72                 $size < 32_768 or die 'large files take too long to parse';
73                 require Text::VimColor;
74                 Text::VimColor->VERSION(0.12)
75                         or die 'early versions are buggy under FastCGI';
76                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
77                 return Text::VimColor->new(
78                         file => $source,
79                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
80                 )->marked;
81         }) {
82                 my %TYPETAG = (
83                         Statement => 'strong',
84                         Error     => 'em',
85                         Todo      => 'em',
86                 );
87
88                 say '<pre>';
89                 foreach (@{$hl}) {
90                         my ($type, $contents) = @{$_};
91                         $contents = decode_utf8($contents);
92                         my $tag = $type && ($TYPETAG{$type} || 'span');
93                         my $arg = '';
94                         print "<$tag$arg class=\"sy-\l$type\">" if $tag;
95                         if (!$type || $type eq 'Constant'
96                         and $contents =~ s{^(['"]?)([/a-z0-9_.-]+\.(?:plp?|css|js))(?=\1$)}{}) {
97                                 # link other page sources, stylesheets, and javascript
98                                 print $1 . showlink($2, "/source/$2");
99                         }
100                         if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
101                                 # link perl module names (Xx::Xx...)
102                                 print $1 . showlink($2, "/source/$2");
103                         }
104                         if ($type && $type eq 'Comment'
105                         and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
106                                 # link generator scripts (by tools/...)
107                                 print $1 . showlink($2, "/source/$2");
108                         }
109                         print Text::VimColor::_xml_escape($contents);
110                         print "</$tag>" if $tag;
111                 }
112                 say '</pre>';
113         }
114         else {
115                 say '<pre>';
116                 print EscapeHTML(decode_utf8(ReadFile($source)));
117                 say '</pre>';
118         }
119
120         say '';
121 }
122