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