source: syntax highlighting in global stylesheet
[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 </head>
19
20 <body id="source">
21 <:
22
23 my $source = $ENV{PATH_INFO};
24 $source =~ s{^/}{};
25 print "<h1>Source of $source</h1>\n";
26
27 if ($source =~ m{(?:/|^)\.}) {
28         die "File request not permitted\n";
29 }
30 elsif ($source =~ s{::}{/}g or !-e $source) {
31         $source .= '.pm';
32         for (0 .. $#{@INC}) {
33                 -e ($_ = "$INC[$_]/$source") or next;
34                 $source = $_;
35                 last;
36         }
37 }
38 -r $source or die "Requested file not found\n";
39
40 require Text::VimColor;
41 delete $Text::VimColor::SYNTAX_TYPE{Underlined};
42 my %TYPETAG = (
43         Statement => 'strong',
44         Error     => 'em',
45         Todo      => 'em',
46 );
47
48 my $hl = Text::VimColor->new(
49         file => $source,
50         vim_options => [@Text::VimColor::VIM_OPTIONS, '+:set enc=utf-8'],
51 );
52 my $parsed = $hl->marked;
53 print "<pre>\n";
54 foreach (@$parsed) {
55         my $tag = $_->[0] && ($TYPETAG{ $_->[0] } || 'span');
56         my $arg = '';
57         print "<$tag$arg class=\"sy-\l$_->[0]\">" if $tag;
58         if ($_->[0] eq 'Constant' and $_->[1] =~ s/^(')([a-z0-9_.]+\.plp?)(?=\1$)//) {
59                 printf '%s<a href="%s">%s</a>', $1, "/source/$2", $2;
60         }
61         if (!$_->[0] and $_->[1] =~ s/^([A-Z]\w+(?:::\w+)+)(?![^;\s])//) {
62                 printf '<a href="%s">%s</a>', "/source/$1", $1;
63         }
64         print Text::VimColor::_xml_escape($_->[1]);
65         print "</$tag>" if $tag;
66 }
67 print "</pre>\n";
68 :>
69 <p class="footer">
70         <a href="/" rel="home">sheet.shiar.nl</a>/source
71         <a href="git://git.shiar.nl/sheet" rel="vcs-git" title="Git repository"><:= $VERSION :></a>
72         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
73         <a title="Licensed under the GNU Affero General Public License, version 3" rel="copyright"
74            href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">AGPLv3</a>
75 </p>
76
77 </html>