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->utf8->indent->space_after->canonical;
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} // ();
22 $header{status} = '500 File unavailable';
23 $header{content_type} = 'text/plain';
24 print "Conversion failed: $@";
30 title => "$source source code",
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."
37 sheet cheat source code perl plp html agpl
39 stylesheet => [qw'light dark mono red'],
40 data => [$source =~ m{\A($incname)\z}],
46 print "<h1>Source files</h1>";
48 print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
52 say '<li>', showlink($_, "/source/$_");
57 my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
59 say "<h1>Source of $href</h1>";
62 if ($source =~ m{(?:/|^)\.}) {
63 Abort("File request not permitted", '403 source not allowed');
65 elsif ($source =~ s{::}{/}g or !-e $source) {
68 -e ($_ = "$INC[$_]/$source") or next;
73 -r $source or Abort("Requested file not found", '404 source not found');
74 my $size = (stat $source)->[7];
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(
84 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
88 Statement => 'strong',
95 my ($type, $contents) = @{$_};
96 $contents = decode_utf8($contents);
97 my $tag = $type && ($TYPETAG{$type} || 'span');
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");
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");
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");
114 print Text::VimColor::_xml_escape($contents);
115 print "</$tag>" if $tag;
121 print EscapeHTML(decode_utf8(ReadFile($source)));