4 my $incname = qr{ [a-z][/a-z0-9_.-]* \.(?:plp?|css|js|txt) }x;
6 if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
7 # convert perl include to json construct
8 checkmodified($source);
10 my $data = do $source or die $@ || $! || 'read error';
12 my $converter = JSON->new;
13 $converter->indent->space_after->canonical;
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} // ();
23 $header{status} = '500 File unavailable';
24 $header{content_type} = 'text/plain';
25 print "Conversion failed: $@";
31 title => "$source source code",
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."
38 sheet cheat source code perl plp html agpl
40 stylesheet => [qw'light dark mono red'],
41 data => [$source =~ m{\A($incname)\z}],
46 if (not $source or -d $source) {
48 print "<h1>Source files</h1>";
51 if ($source and $source ne 'tools') {
52 Abort("Directory index not permitted", '403 source not allowed');
55 print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
57 for (glob($source ? "$source/*" : '*.plp')) {
58 say '<li>', showlink($_, "/source/$_");
63 my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
65 say "<h1>Source of $href</h1>";
68 if ($source =~ m{(?:/|^)\.}) {
69 Abort("File request not permitted", '403 source not allowed');
71 elsif ($source =~ s{::}{/}g or !-e $source) {
74 -e ($_ = "$INC[$_]/$source") or next;
79 -r $source or Abort("Requested file not found", '404 source not found');
80 my $size = (stat $source)->[7];
82 my $cachefile = "source/$source.html";
83 if (-e $cachefile and (stat $cachefile)->[9] >= (stat $source)->[9]) {
85 print ReadFile($cachefile);
89 open my $cache, '>', $cachefile
90 or Alert("Could not save cache", "Opening $cachefile failed: $!");;
93 $size < 32_768 or die 'large files take too long to parse';
94 require Text::VimColor;
95 Text::VimColor->VERSION(0.12)
96 or die 'early versions are buggy under FastCGI';
97 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
98 return Text::VimColor->new(
100 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
104 Statement => 'strong',
112 my ($type, $contents) = @{$_};
113 $contents = decode_utf8($contents);
114 my $tag = $type && ($TYPETAG{$type} || 'span');
115 my $line = Text::VimColor::_xml_escape($contents);
117 # link other page sources, stylesheets, and javascript
118 $line =~ s{ ^(['"]?) \K ($incname) (?=\1$) }{ showlink($2, "/source/$2") }xe
119 if !$type || $type eq 'Constant';
120 # link perl module names (Xx::Xx...)
121 $line =~ s{ ^\s* \K ([A-Z]\w+(?:::\w+)+) (?![^;\s]) }{ showlink($1, "/source/$1") }xe
123 # link generator scripts (by tools/...)
124 $line =~ s{ ^.*? by\ \K (tools/\S+) }{ showlink($1, "/source/$1") }xe
125 if $type && $type eq 'Comment';
127 $line = qq(<$tag class="sy-\l$type">$line</$tag>) if $tag;
129 print {$cache} $line if $cache;
135 print EscapeHTML(decode_utf8(ReadFile($source)));