3 my $source = $ENV{PATH_INFO};
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'],
45 print "<h1>Source files</h1>";
47 print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
51 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
56 print "<h1>Source of $source</h1>\n";
58 if ($source =~ m{(?:/|^)\.}) {
59 die "File request not permitted\n";
61 elsif ($source =~ s{::}{/}g or !-e $source) {
64 -e ($_ = "$INC[$_]/$source") or next;
69 -r $source or die "Requested file not found\n";
72 if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
73 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
75 Statement => 'strong',
80 my $hl = Text::VimColor->new(
82 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
84 my $parsed = $hl->marked;
87 my ($type, $contents) = @{$_};
88 $contents = Encode::decode_utf8($contents);
89 my $tag = $type && ($TYPETAG{$type} || 'span');
91 print "<$tag$arg class=\"sy-\l$type\">" if $tag;
92 if (!$type || $type eq 'Constant'
93 and $contents =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
94 # link other page sources, stylesheets, and javascript
95 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
97 if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
98 # link perl module names (Xx::Xx...)
99 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
101 if ($type && $type eq 'Comment'
102 and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
103 # link generator scripts (by tools/...)
104 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
106 print Text::VimColor::_xml_escape($contents);
107 print "</$tag>" if $tag;
113 print EscapeHTML(Encode::decode_utf8(ReadFile($source)));