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