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