browser: define known 0% score markers
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $ENV{PATH_INFO};
4 $source =~ s{^/}{};
5
6 if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
7         # convert perl include to json construct
8         checkmodified($source);
9         eval {
10                 my $data = do $source or die $@ || $! || 'read error';
11                 require JSON;
12                 my $converter = JSON->new;
13                 $converter->utf8->indent->space_after->canonical;
14
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} // ();
20                 return 1;
21         } or do {
22                 $header{status} = '500 File unavailable';
23                 $header{content_type} = 'text/plain';
24                 print "Conversion failed: $@";
25         };
26         exit;
27 }
28
29 Html({
30         title => "$source source code",
31         version => '1.1',
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."
35         ],
36         keywords => [qw'
37                 sheet cheat source code perl plp html agpl
38         '],
39         stylesheet => [qw'light dark mono red'],
40 });
41
42 print "\n";
43
44 if (not $source) {
45         print "<h1>Source files</h1>";
46
47         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
48         print '<ul>'."\n";
49         for (glob '*.plp') {
50                 chomp;
51                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", EscapeHTML($_);
52         }
53         print "</ul>\n\n";
54 }
55 else {
56         print "<h1>Source of $source</h1>\n";
57
58         if ($source =~ m{(?:/|^)\.}) {
59                 die "File request not permitted\n";
60         }
61         elsif ($source =~ s{::}{/}g or !-e $source) {
62                 $source .= '.pm';
63                 for (0 .. $#INC) {
64                         -e ($_ = "$INC[$_]/$source") or next;
65                         $source = $_;
66                         last;
67                 }
68         }
69         -r $source or die "Requested file not found\n";
70
71         require Encode;
72         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
73                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
74                 my %TYPETAG = (
75                         Statement => 'strong',
76                         Error     => 'em',
77                         Todo      => 'em',
78                 );
79
80                 my $hl = Text::VimColor->new(
81                         file => $source,
82                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
83                 );
84                 my $parsed = $hl->marked;
85                 print "<pre>\n";
86                 foreach (@$parsed) {
87                         my ($type, $contents) = @{$_};
88                         $contents = Encode::decode_utf8($contents);
89                         my $tag = $type && ($TYPETAG{$type} || 'span');
90                         my $arg = '';
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;
96                         }
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;
100                         }
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;
105                         }
106                         print Text::VimColor::_xml_escape($contents);
107                         print "</$tag>" if $tag;
108                 }
109                 print "</pre>\n";
110         }
111         else {
112                 print "<pre>\n";
113                 print EscapeHTML(Encode::decode_utf8(ReadFile($source)));
114                 print "</pre>\n";
115         }
116
117         print "\n";
118 }
119