termcol: divide list rows over columns with 8 rows each
[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 (defined( my $setstyle = $get{style} )) {
27                 $style = $styles{ $setstyle };
28                 eval {
29                         require CGI::Cookie;
30                         my $cookie = CGI::Cookie->new(
31                                 -name    => 'style',
32                                 -value   => $setstyle || '',
33                                 -path    => '/',  # site-wide; current page is confusing to most users
34                                 -expires => $setstyle ? '+5y' : '-1d',
35                         ) or die "empty object returned\n";
36                         AddCookie($cookie->as_string);
37                 } or warn "Unable to create style cookie: $@";
38         }
39
40         $style ||= exists $cookie{style} && $styles{ $cookie{style} } || $_[0];
41
42         return join "\n", map { sprintf(
43                 '<link rel="%s" type="text/css" media="all" href="%s" title="%s">',
44                 $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.8", $_
45         ) } @_;
46 }
47
48 sub checkmodified {
49         my $lastmod;
50         for (@_) {
51                 my $mod = stat $_ or next;
52                 $mod = $mod->mtime or next;
53                 $lastmod = $mod if $mod gt $lastmod;
54         }
55
56         for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
57                 next if str2time($_) < $lastmod;
58                 $header{status} = '304 Same old';
59                 exit;
60         }
61
62         $header{'Last-Modified'} = time2str($lastmod);
63 }
64
65 sub Html {
66         my ($meta) = @_;
67
68         # announce and check data modification
69         checkmodified(
70                 $ENV{SCRIPT_FILENAME},
71                 (grep { /\bShiar_/ } values %INC),
72                 $meta->{data} ? @{ $meta->{data} } : (),
73         );
74         $header{'Cache-Control'} = sprintf 'max-age: ', 24*60*60;
75
76         # default fallbacks
77         $meta->{stylesheet} ||= [qw'light dark circus mono red terse'];
78         $meta->{charset} ||= 'utf-8';
79
80         ref $_ eq 'ARRAY' or $_ = [$_] for $meta->{raw};
81
82         # optional amends
83         push @{ $meta->{raw} }, (
84                 '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
85                 '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
86                 !$showkeys ? '<style type="text/css"> .no {visibility:hidden} </style>'
87                         : $showkeys eq 'ghost' ? '<style type="text/css"> .no, .alias {opacity:.5} </style>'
88                         : (),
89                 '<script type="text/javascript" src="/keys.js?1.6"></script>',
90         ) if $meta->{keys};
91
92         # flatten arrays
93         ref $_ eq 'ARRAY' and $_ = join ' ',  @$_ for $meta->{description};
94         ref $_ eq 'ARRAY' and $_ = join ', ', @$_ for $meta->{keywords};
95         ref $_ eq 'ARRAY' and $_ = join "\n", @$_ for $meta->{raw};
96         ref $_ eq 'ARRAY' and $_ = stylesheet(@$_)."\n" for $meta->{stylesheet};
97
98         # other vars
99         my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
100
101         # leading output
102         $header{content_type} = "text/html; charset=$meta->{charset}";
103         print <<"EOT";
104 <!DOCTYPE html>
105 <html lang="en">
106
107 <head>
108 <meta http-equiv="content-type" content="$header{content_type}">
109 <title>$meta->{title}</title>
110 <meta name="description" content="$meta->{description}">
111 <meta name="keywords" content="$meta->{keywords}">
112 <meta name="viewport" content="width=device-width, initial-scale=1">
113 <link rel="icon" type="image/png" href="/clip.png">
114 EOT
115         print $_, "\n" for $meta->{stylesheet} || ();
116         print $_, "\n" for $meta->{raw} || ();
117         print qq{</head>\n\n<body id="$file">\n};
118
119         # dev indicator
120         printf '<p style="%s">beta</p>', join('; ',
121                 'position: fixed',
122                 'right: 1em',
123                 'opacity: .5',
124                 'border: 1ex solid red',
125                 'border-width: 1ex 0',
126                 'z-index: 1',
127                 'background: inherit',
128         ) if $ENV{HTTP_HOST} =~ /\bdev\./;
129
130         # prepare trailing output
131         PLP_END {
132                 print <<"EOT";
133 <p class="footer">
134         <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
135          rel="source" title="Written in Perl">plp</a>
136         version <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
137          rel="vcs-git" title="Git repository">$meta->{version}</a>
138         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
139         <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html" rel="license copyright"
140          title="Licensed under the GNU Affero General Public License, version 3">AGPLv3</a>
141 </p>
142
143 </html>
144 EOT
145         };
146 }
147