charset: ucd fallback for undigraphed characters
[sheet.git] / source.plp
1 <:
2 use utf8;
3 use strict;
4 use warnings;
5
6 our $VERSION = 'v1.0';
7
8 $header{content_type} = "text/html; charset=utf-8";
9
10 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
11  "http://www.w3.org/TR/html4/loose.dtd">
12 <html>
13
14 <head>
15 <meta http-equiv="content-type" content="<:= $header{content_type} :>">
16 <title>sheet page source code</title>
17 <link rel="stylesheet" type="text/css" media="all" href="/base.css"><:
18         my %styles = map {$_ => $_} qw(dark mono red);
19         our $style = exists $get{style} && $styles{$get{style}} || 'light';
20         printf(qq{\n<link rel="%s" type="text/css" media="all" href="%s" title="%s">},
21                 $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css", $_
22         ) for keys %styles;
23 :>
24 </head>
25
26 <body id="source">
27 <:
28
29 my $source = $ENV{PATH_INFO};
30 $source =~ s{^/}{};
31
32 if (not $source) {
33         print "<h1>Source files</h1>";
34
35         print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
36         print '<ul>'."\n";
37         for (glob '*.plp') {
38                 chomp;
39                 printf '<li><a href="/source/%s">%1$s</a></li>'."\n", Entity($_);
40         }
41         print "</ul>\n\n";
42 }
43 else {
44         print "<h1>Source of $source</h1>\n";
45
46         if ($source =~ m{(?:/|^)\.}) {
47                 die "File request not permitted\n";
48         }
49         elsif ($source =~ s{::}{/}g or !-e $source) {
50                 $source .= '.pm';
51                 for (0 .. $#{@INC}) {
52                         -e ($_ = "$INC[$_]/$source") or next;
53                         $source = $_;
54                         last;
55                 }
56         }
57         -r $source or die "Requested file not found\n";
58
59         require Text::VimColor;
60         delete $Text::VimColor::SYNTAX_TYPE{Underlined};
61         my %TYPETAG = (
62                 Statement => 'strong',
63                 Error     => 'em',
64                 Todo      => 'em',
65         );
66
67         my $hl = Text::VimColor->new(
68                 file => $source,
69                 vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
70         );
71         my $parsed = $hl->marked;
72         print "<pre>\n";
73         foreach (@$parsed) {
74                 my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
75                 my $arg = '';
76                 print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
77                 if ($_->[0] eq 'Constant'
78                 and $_->[1] =~ s{^(['"])(/?[a-z0-9_.]+\.(?:plp?|css|js))(?=\1$)}{}) {
79                         printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
80                 }
81                 if (!$_->[0] and $_->[1] =~ s/^(\s*)([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
82                         printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
83                 }
84                 print Text::VimColor::_xml_escape($_->[1]);
85                 print "</$tag>" if $tag;
86         }
87         print "</pre>\n";
88 }
89 :>
90 <p class="footer">
91         <a href="/" rel="home">sheet.shiar.nl</a>/source
92         <a href="git://git.shiar.nl/sheet" rel="vcs-git" title="Git repository"><:= $VERSION :></a>
93         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
94         <a title="Licensed under the GNU Affero General Public License, version 3" rel="copyright"
95            href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">AGPLv3</a>
96 </p>
97
98 </html>