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}],
47 print "<h1>Source files</h1>";
49 print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
53 say '<li>', showlink($_, "/source/$_");
58 my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
60 say "<h1>Source of $href</h1>";
63 if ($source =~ m{(?:/|^)\.}) {
64 Abort("File request not permitted", '403 source not allowed');
66 elsif ($source =~ s{::}{/}g or !-e $source) {
69 -e ($_ = "$INC[$_]/$source") or next;
74 -r $source or Abort("Requested file not found", '404 source not found');
75 my $size = (stat $source)->[7];
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(
85 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
89 Statement => 'strong',
96 my ($type, $contents) = @{$_};
97 $contents = decode_utf8($contents);
98 my $tag = $type && ($TYPETAG{$type} || 'span');
100 print "<$tag$arg class=\"sy-\l$type\">" if $tag;
101 if (!$type || $type eq 'Constant'
102 and $contents =~ s{^(['"]?)($incname)(?=\1$)}{}) {
103 # link other page sources, stylesheets, and javascript
104 print $1 . showlink($2, "/source/$2");
106 if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
107 # link perl module names (Xx::Xx...)
108 print $1 . showlink($2, "/source/$2");
110 if ($type && $type eq 'Comment'
111 and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
112 # link generator scripts (by tools/...)
113 print $1 . showlink($2, "/source/$2");
115 print Text::VimColor::_xml_escape($contents);
116 print "</$tag>" if $tag;
122 print EscapeHTML(decode_utf8(ReadFile($source)));