index: link apl page
[sheet.git] / common.inc.plp
1 <:
2 use utf8;
3 use strict;
4 use warnings;
5 no  warnings 'qw';  # you know what you doing
6 no  warnings 'uninitialized';  # save some useless checks for more legible code
7 use open ':std' => ':utf8';
8
9 use File::stat 'stat';
10 use HTTP::Date;
11
12 $PLP::ERROR = sub {
13         my ($text, $html) = @_;
14         print '<p class="error"><strong>Fatal error</strong>: '.$html."</p>\n\n";
15 };
16
17 our $style;
18 our $showkeys = !exists $get{keys} ? undef :
19         ($get{keys} ne '0' && ($get{keys} || 'always'));
20
21 $header{content_type} = 'text/html; charset=utf-8';
22
23 sub stylesheet {
24         my %styles = map {$_ => $_} @_;
25
26         if (exists $get{style}) {
27                 $style = $styles{ $get{style} };
28                 require CGI::Cookie;
29                 if (my $cookie = CGI::Cookie->new(
30                         -name    => 'style',
31                         -value   => $style,
32                         -path    => '/',  # site-wide; current page is confusing to most users
33                         -expires => $style ? '+5y' : '-1d',
34                 )) {
35                         AddCookie($cookie->as_string);
36                 }
37         }
38
39         $style ||= exists $cookie{style} && $styles{ $cookie{style} } || $_[0];
40
41         return join "\n", map { sprintf(
42                 '<link rel="%s" type="text/css" media="all" href="%s" title="%s">',
43                 $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.6", $_
44         ) } @_;
45 }
46
47 sub checkmodified {
48         my $lastmod;
49         for (@_) {
50                 my $mod = stat $_ or next;
51                 $mod = $mod->mtime or next;
52                 $lastmod = $mod if $mod gt $lastmod;
53         }
54
55         for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
56                 next if str2time($_) < $lastmod;
57                 $header{status} = '304 Same old';
58                 exit;
59         }
60
61         $header{'Last-Modified'} = time2str($lastmod);
62 }
63
64 sub Html {
65         my ($meta) = @_;
66
67         # announce and check data modification
68         checkmodified(
69                 $ENV{SCRIPT_FILENAME},
70                 (grep { /\bShiar_/ } values %INC),
71                 $meta->{data} ? @{ $meta->{data} } : (),
72         );
73         $header{'Cache-Control'} = sprintf 'max-age: ', 24*60*60;
74
75         # default fallbacks
76         $meta->{stylesheet} ||= [qw'light dark circus mono red terse'];
77         $meta->{charset} ||= 'utf-8';
78
79         # optional amends
80         push @{ $meta->{raw} }, (
81                 '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
82                 '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
83                 !$showkeys ? '<style type="text/css"> .no {visibility:hidden} </style>'
84                         : $showkeys eq 'ghost' ? '<style type="text/css"> .no, .alias {opacity:.5} </style>'
85                         : (),
86                 '<script type="text/javascript" src="/keys.js?1.6"></script>',
87         ) if $meta->{keys};
88
89         # flatten arrays
90         ref $_ eq 'ARRAY' and $_ = join ' ',  @$_ for $meta->{description};
91         ref $_ eq 'ARRAY' and $_ = join ', ', @$_ for $meta->{keywords};
92         ref $_ eq 'ARRAY' and $_ = join "\n", @$_ for $meta->{raw};
93         ref $_ eq 'ARRAY' and $_ = stylesheet(@$_)."\n" for $meta->{stylesheet};
94
95         # other vars
96         my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
97
98         # leading output
99         $header{content_type} = "text/html; charset=$meta->{charset}";
100         print <<"EOT";
101 <!DOCTYPE html>
102 <html lang="en">
103
104 <head>
105 <meta http-equiv="content-type" content="$header{content_type}">
106 <title>$meta->{title}</title>
107 <meta name="description" content="$meta->{description}">
108 <meta name="keywords" content="$meta->{keywords}">
109 <link rel="icon" type="image/png" href="/clip.png">
110 EOT
111         print $_, "\n" for $meta->{stylesheet} || ();
112         print $_, "\n" for $meta->{raw} || ();
113         print qq{</head>\n\n<body id="$file">\n};
114
115         # prepare trailing output
116         PLP_END {
117                 print <<"EOT";
118 <p class="footer">
119         <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
120          rel="source" title="Written in Perl">plp</a>
121         <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
122          rel="vcs-git" title="Git repository">$meta->{version}</a>
123         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
124         <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html" rel="license copyright"
125          title="Licensed under the GNU Affero General Public License, version 3">AGPLv3</a>
126 </p>
127
128 </html>
129 EOT
130         };
131 }
132