charset: move html link function to common include
[sheet.git] / source.plp
1 <(common.inc.plp)><:
2
3 my $source = $Request;
4
5 if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
6         # convert perl include to json construct
7         checkmodified($source);
8         eval {
9                 my $data = do $source or die $@ || $! || 'read error';
10                 require JSON;
11                 my $converter = JSON->new;
12                 $converter->utf8->indent->space_after->canonical;
13
14                 $header{content_type} = 'application/json';
15                 $header{content_type} = 'text/plain' if exists $get{debug};
16                 print $_, '(' for $get{callback} // ();
17                 print $converter->encode($data);
18                 print     ')' for $get{callback} // ();
19                 return 1;
20         } or do {
21                 $header{status} = '500 File unavailable';
22                 $header{content_type} = 'text/plain';
23                 print "Conversion failed: $@";
24         };
25         exit;
26 }
27
28 Html({
29         title => "$source source code",
30         version => '1.1',
31         description => !$source ? 'Index of source files for this site.' : [
32                 "Source code of the $source file at this site,",
33                 "with syntax highlighted and references linked."
34         ],
35         keywords => [qw'
36                 sheet cheat source code perl plp html agpl
37         '],
38         stylesheet => [qw'light dark mono red'],
39 });
40
41 say '';
42
43 if (not $source) {
44         print "<h1>Source files</h1>";
45
46         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
47         say '<ul>';
48         for (glob '*.plp') {
49                 chomp;
50                 say sprintf '<li><a href="/source/%s">%1$s</a></li>', EscapeHTML($_);
51         }
52         say "</ul>\n";
53 }
54 else {
55         say "<h1>Source of $source</h1>";
56
57         if ($source =~ m{(?:/|^)\.}) {
58                 die "File request not permitted\n";
59         }
60         elsif ($source =~ s{::}{/}g or !-e $source) {
61                 $source .= '.pm';
62                 for (0 .. $#INC) {
63                         -e ($_ = "$INC[$_]/$source") or next;
64                         $source = $_;
65                         last;
66                 }
67         }
68         -r $source or die "Requested file not found\n";
69
70         if (eval { require Text::VimColor and Text::VimColor->VERSION(0.12) }) {
71                 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
72                 my %TYPETAG = (
73                         Statement => 'strong',
74                         Error     => 'em',
75                         Todo      => 'em',
76                 );
77
78                 my $hl = Text::VimColor->new(
79                         file => $source,
80                         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
81                 );
82                 my $parsed = $hl->marked;
83                 say '<pre>';
84                 foreach (@$parsed) {
85                         my ($type, $contents) = @{$_};
86                         $contents = decode_utf8($contents);
87                         my $tag = $type && ($TYPETAG{$type} || 'span');
88                         my $arg = '';
89                         print "<$tag$arg class=\"sy-\l$type\">" if $tag;
90                         if (!$type || $type eq 'Constant'
91                         and $contents =~ s{^(['"]?)(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
92                                 # link other page sources, stylesheets, and javascript
93                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
94                         }
95                         if (!$type and $contents =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
96                                 # link perl module names (Xx::Xx...)
97                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
98                         }
99                         if ($type && $type eq 'Comment'
100                         and $contents =~ s{^(.*? by )(tools/\S+)}{}) {
101                                 # link generator scripts (by tools/...)
102                                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
103                         }
104                         print Text::VimColor::_xml_escape($contents);
105                         print "</$tag>" if $tag;
106                 }
107                 say '</pre>';
108         }
109         else {
110                 say '<pre>';
111                 print EscapeHTML(decode_utf8(ReadFile($source)));
112                 say '</pre>';
113         }
114
115         say '';
116 }
117