source: delay header output until after errors
[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|txt) }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.2',
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         my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
58         PLP_START {
59                 say "<h1>Source of $href</h1>";
60         };
61
62         if ($source =~ m{(?:/|^)\.}) {
63                 Abort("File request not permitted", '403 source not allowed');
64         }
65         elsif ($source =~ s{::}{/}g or !-e $source) {
66                 $source .= '.pm';
67                 for (0 .. $#INC) {
68                         -e ($_ = "$INC[$_]/$source") or next;
69                         $source = $_;
70                         last;
71                 }
72         }
73         -r $source or Abort("Requested file not found", '404 source not found');
74         my $size = (stat $source)->[7];
75
76         if (my $hl = eval {
77                 $size < 32_768 or die 'large files take too long to parse';
78                 require Text::VimColor;
79                 Text::VimColor->VERSION(0.12)
80                         or die 'early versions are buggy under FastCGI';
81                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
82                 return Text::VimColor->new(
83                         file => $source,
84                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
85                 )->marked;
86         }) {
87                 my %TYPETAG = (
88                         Statement => 'strong',
89                         Error     => 'em',
90                         Todo      => 'em',
91                 );
92
93                 say '<pre>';
94                 foreach (@{$hl}) {
95                         my ($type, $contents) = @{$_};
96                         $contents = decode_utf8($contents);
97                         my $tag = $type && ($TYPETAG{$type} || 'span');
98                         my $arg = '';
99                         print "<$tag$arg class=\"sy-\l$type\">" if $tag;
100                         if (!$type || $type eq 'Constant'
101                         and $contents =~ s{^(['"]?)($incname)(?=\1$)}{}) {
102                                 # link other page sources, stylesheets, and javascript
103                                 print $1 . showlink($2, "/source/$2");
104                         }
105                         if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
106                                 # link perl module names (Xx::Xx...)
107                                 print $1 . showlink($2, "/source/$2");
108                         }
109                         if ($type && $type eq 'Comment'
110                         and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
111                                 # link generator scripts (by tools/...)
112                                 print $1 . showlink($2, "/source/$2");
113                         }
114                         print Text::VimColor::_xml_escape($contents);
115                         print "</$tag>" if $tag;
116                 }
117                 say '</pre>';
118         }
119         else {
120                 say '<pre>';
121                 print EscapeHTML(decode_utf8(ReadFile($source)));
122                 say '</pre>';
123         }
124
125         say '';
126 }
127