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