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");
58 say "<h1>Source of $href</h1>";
60 if ($source =~ m{(?:/|^)\.}) {
61 die "File request not permitted\n";
63 elsif ($source =~ s{::}{/}g or !-e $source) {
66 -e ($_ = "$INC[$_]/$source") or next;
71 -r $source or die "Requested file not found\n";
72 my $size = (stat $source)->[7];
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(
82 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
86 Statement => 'strong',
93 my ($type, $contents) = @{$_};
94 $contents = decode_utf8($contents);
95 my $tag = $type && ($TYPETAG{$type} || 'span');
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");
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");
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");
112 print Text::VimColor::_xml_escape($contents);
113 print "</$tag>" if $tag;
119 print EscapeHTML(decode_utf8(ReadFile($source)));