unicode: change preliminary characters to unicode 6.0 specs
[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", $_
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
68         # default fallbacks
69         $meta->{stylesheet} ||= [qw'light dark circus mono red terse'];
70         $meta->{charset} ||= 'utf-8';
71
72         # optional amends
73         push @{ $meta->{raw} }, (
74                 '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
75                 '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
76                 !$showkeys ? '<style type="text/css"> .no {visibility:hidden} </style>'
77                         : $showkeys eq 'ghost' ? '<style type="text/css"> .no, .alias {opacity:.5} </style>'
78                         : (),
79                 '<script type="text/javascript" src="/keys.js"></script>',
80         ) if $meta->{keys};
81
82         # flatten arrays
83         ref $_ eq 'ARRAY' and $_ = join ' ',  @$_ for $meta->{description};
84         ref $_ eq 'ARRAY' and $_ = join ', ', @$_ for $meta->{keywords};
85         ref $_ eq 'ARRAY' and $_ = join "\n", @$_ for $meta->{rawstyle}, $meta->{raw};
86         ref $_ eq 'ARRAY' and $_ = stylesheet(@$_)."\n" for $meta->{stylesheet};
87
88         # other vars
89         my $sep = $meta->{charset} eq 'utf-8' ? '•' : ' -- ';
90         my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
91
92         # leading output
93         $header{content_type} = "text/html; charset=$meta->{charset}";
94         print <<"EOT";
95 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
96  "http://www.w3.org/TR/html4/loose.dtd">
97 <html lang="en">
98
99 <head>
100 <meta http-equiv="content-type" content="$header{content_type}">
101 <title>$meta->{title}</title>
102 <meta name="description" content="$meta->{description}">
103 <meta name="keywords" content="$meta->{keywords}">
104 <link rel="icon" type="image/png" href="/clip.png">
105 EOT
106         print $_, "\n" for $meta->{stylesheet} || ();
107         print $_, "\n" for $meta->{raw} || ();
108         print qq{</head>\n\n<body id="$file">\n};
109
110         # prepare trailing output
111         PLP_END {
112                 print <<"EOT";
113 <p class="footer">
114         <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
115          rel="code" title="Written in Perl">plp</a>
116         <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
117          rel="vcs-git" title="Git repository">$meta->{version}</a>
118         created by <a href="http://shiar.nl/" rel="author">Shiar</a> $sep
119         <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html" rel="copyright"
120          title="Licensed under the GNU Affero General Public License, version 3">AGPLv3</a>
121 </p>
122
123 </html>
124 EOT
125         };
126 }
127