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