source: prepare highlighted lines before output
[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->indent->space_after->canonical;
14
15                 $header{content_type} = 'application/json';
16                 $header{'Access-Control-Allow-Origin'} = '*';
17                 $header{content_type} = 'text/plain' if exists $get{debug};
18                 print $_, '(' for $get{callback} // ();
19                 print $converter->encode($data);
20                 print     ')' for $get{callback} // ();
21                 return 1;
22         } or do {
23                 $header{status} = '500 File unavailable';
24                 $header{content_type} = 'text/plain';
25                 print "Conversion failed: $@";
26         };
27         exit;
28 }
29
30 Html({
31         title => "$source source code",
32         version => '1.2',
33         description => !$source ? 'Index of source files for this site.' : [
34                 "Source code of the $source file at this site,",
35                 "with syntax highlighted and references linked."
36         ],
37         keywords => [qw'
38                 sheet cheat source code perl plp html agpl
39         '],
40         stylesheet => [qw'light dark mono red'],
41         data => [$source =~ m{\A($incname)\z}],
42 });
43
44 say '';
45
46 if (not $source) {
47         print "<h1>Source files</h1>";
48
49         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
50         say '<ul>';
51         for (glob '*.plp') {
52                 chomp;
53                 say '<li>', showlink($_, "/source/$_");
54         }
55         say "</ul>\n";
56 }
57 else {
58         my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
59         PLP_START {
60                 say "<h1>Source of $href</h1>";
61         };
62
63         if ($source =~ m{(?:/|^)\.}) {
64                 Abort("File request not permitted", '403 source not allowed');
65         }
66         elsif ($source =~ s{::}{/}g or !-e $source) {
67                 $source .= '.pm';
68                 for (0 .. $#INC) {
69                         -e ($_ = "$INC[$_]/$source") or next;
70                         $source = $_;
71                         last;
72                 }
73         }
74         -r $source or Abort("Requested file not found", '404 source not found');
75         my $size = (stat $source)->[7];
76
77         if (my $hl = eval {
78                 $size < 32_768 or die 'large files take too long to parse';
79                 require Text::VimColor;
80                 Text::VimColor->VERSION(0.12)
81                         or die 'early versions are buggy under FastCGI';
82                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
83                 return Text::VimColor->new(
84                         file => $source,
85                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
86                 )->marked;
87         }) {
88                 my %TYPETAG = (
89                         Statement => 'strong',
90                         Error     => 'em',
91                         Todo      => 'em',
92                 );
93
94                 say '<pre>';
95                 foreach (@{$hl}) {
96                         my ($type, $contents) = @{$_};
97                         $contents = decode_utf8($contents);
98                         my $tag = $type && ($TYPETAG{$type} || 'span');
99                         my $line = Text::VimColor::_xml_escape($contents);
100
101                         # link other page sources, stylesheets, and javascript
102                         $line =~ s{ ^(['"]?) \K ($incname) (?=\1$) }{ showlink($2, "/source/$2") }xe
103                                 if !$type || $type eq 'Constant';
104                         # link perl module names (Xx::Xx...)
105                         $line =~ s{ ^\s* \K ([A-Z]\w+(?:::\w+)+) (?![^;\s]) }{ showlink($1, "/source/$1") }xe
106                                 if !$type;
107                         # link generator scripts (by tools/...)
108                         $line =~ s{ ^.*? by\  \K (tools/\S+) }{ showlink($1, "/source/$1") }xe
109                                 if $type && $type eq 'Comment';
110
111                         $line = qq(<$tag class="sy-\l$type">$line</$tag>) if $tag;
112                         print $line;
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