unmodified declarations first in key maps
[sheet.git] / Shiar_Sheet / Keyboard.pm
1 package Shiar_Sheet::Keyboard;
2
3 use strict;
4 use warnings;
5 no  warnings 'uninitialized';  # save some useless checks for more legible code
6 use Carp;
7
8 our $VERSION = '1.02';
9
10 my @casedesc = (undef, qw/shift ctrl meta/, 'shift meta');
11 my @rowdesc = qw(numeric top home bottom);
12 my %keyrows = do 'keys.inc.pl';
13 # add first two cases of each row again with each char prepended by + (alt)
14 push @$_, map { [map {"+$_"} @$_] } @$_[0,1] for map {@$_} values %keyrows;
15
16 my %keytrans = qw(
17         ^@ NUL ^a SOH ^b STX ^c ETX  ^d EOT ^e ENQ ^f ACK ^g BEL
18         ^h BS  ^i tab ^j LF  ^k VT   ^l FF  ^m CR  ^n SO  ^o SI
19         ^p DLE ^q DC1 ^r DC2 ^s DC3  ^t DC4 ^u NAK ^v SYN ^w ETB
20         ^x CAN ^y EM  ^z SUB ^[ ESC  ^\ FS  ^] GS  ^^ RS  ^_ US
21         ^? DEL
22 );
23
24 sub new {
25         my $self = shift;
26         my ($keys) = @_;
27
28         croak 'Invalid key table specified' unless ref $keys eq 'HASH';
29         my $parent = (caller)[0];  # calling module
30         my $sign = do {
31                 no strict 'refs';  # temporarily allow variable references
32                 \%{ $parent.'::sign' };  # return %sign from parent
33         };
34         croak "%${parent}::sign not found" unless %$sign;
35
36         bless {sign => $sign, keys => $keys, map => 'qwerty'}, $self;
37 }
38
39 sub map {
40         my $self = shift;
41         my ($mapname) = @_;
42
43         return $self->{map} = $mapname if defined $keyrows{$mapname};
44         return undef;
45 }
46
47 sub escapeclass {
48         local $_ = shift;
49         s/\^/_c/g;
50         s/\+/_m/g;
51         s/\[/_sbo/g;
52         s/\]/_sbc/g;
53         s/^$/_/;
54         return $_;
55 }
56
57 sub escapehtml {
58         local $_ = shift;
59         s/</&lt;/g;
60         s/>/&gt;/g;
61         s/  / &nbsp;/g;
62         return $_;
63 }
64
65 sub keyunalias {
66         my $self = shift;
67         my ($key, $ancestry) = @_;
68
69         $key =~ s/(\S*?)(\+?\^?\S)($|\s.*)/$2/;
70         my $mode = $1;
71         my $keyinfo = $self->{keys}->{$mode}->{$key};
72
73         return [] unless defined $keyinfo;
74         return $keyinfo if ref $keyinfo;
75         return if $ancestry->{$key}++;  # endless loop failsafe
76         return $self->keyunalias($keyinfo, $ancestry);
77 }
78
79 sub print_key {
80         my $self = shift;
81         my ($mode, $key, $keyinfo) = @_;
82
83         $keyinfo = [ $self->{sign}->{alias}.$keyinfo, $self->keyunalias($keyinfo)->[1] . ' alias' ]
84                 if defined $keyinfo and not ref $keyinfo;  # alias
85         my ($desc, $flags, $mnem) = @$keyinfo if defined $keyinfo;
86         defined $desc or $flags = $key eq '^0' ? 'ni' : 'no';
87
88 #       $key = $keytrans{$key} if defined $keytrans{$key};
89         my $keytxt = $mode . escapehtml($key) if $key ne '^0';
90            $keytxt .= $self->{sign}->{$1} while $flags =~ s/(?:^| )(arg[a-ln-z]?)\b//;  # arguments
91            $keytxt .= "<small>$self->{sign}->{motion}</small>" if $flags =~ s/ ?\bargm\b//;  # motion argument
92            $keytxt =~ s{\^(?=.)}{<small>^</small>};  # element around ctrl-identifier
93         my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $self->{keys}{$1} && sprintf(
94                 ' onclick="setmode(%s)"',
95                 $1 eq '' ? '' : sprintf(q{'mode%s'}, escapeclass($1))
96         );
97         $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
98                 if $flags =~ s/ ?\blink(\S*)//;
99         my $keyhint = defined($mnem) && qq{ title="$mnem"};
100
101         print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
102         print ' ', $desc if defined $desc;
103         print "\n";
104 }
105
106 sub print_rows {
107         my $self = shift;
108         my $static = shift;
109         my @moderows = $static ? split(/\s+/, $static) : sort keys %{ $self->{keys} };
110
111         for (my $row = 0; $row <= $#{ $keyrows{$self->{map}} }; $row++) {
112                 my $keyrow = $keyrows{$self->{map}}->[$row];
113
114                 print qq{<li class="row row$row"><ul>\n};
115                 for my $modefull (@moderows) {
116                         my $mode = $modefull;
117                         my @showcase = $mode =~ s/(\d+)(?:-(\d+))?$//
118                                 ? (map {$_ - 1} split //, $row == 0 && $2 || $1) : (2, 1, 0);
119                         my $modekeys = $self->{keys}{$mode};
120
121                         for my $case (@showcase) {
122                                 my $keycase = $keyrow->[$case] or next;
123                                   @$keycase or next;
124
125                                 printf "\t<li%s>", $mode ne '' && sprintf(
126                                         ' class="%s"', ($static ? '' : 'mode ') . 'mode' . escapeclass($mode)
127                                 );
128                                 printf("<h3>%s<small>: %s</small></h3>\n", # XXX insert &nbsp; here to fix msie<=6
129                                                 $modekeys->{desc} || "mode $mode",
130                                                 "$rowdesc[$row] row $casedesc[$case]"
131                                 );
132                                 my $caseclass = 'keys';
133                                    $caseclass .= ' lead' if defined $modekeys->{lead};  # leading command key shown
134                                    $caseclass .= " $casedesc[$case]" if defined $casedesc[$case];
135                                 print qq{\t\t<ul class="$caseclass">\n};
136                                 $self->print_key($modekeys->{lead}, $_, $modekeys->{$_}) for @$keycase;
137                                 print qq{\t\t</ul>\n};
138                         } # case
139
140                 } # mode
141                 print qq{\t</ul>\n};
142         } # row
143 }
144
145 1;
146
147 =head1 NAME
148
149 Shiar_Sheet::Keyboard - Output HTML for key sheets
150
151 =head1 SYNOPSIS
152
153         our %sign = (alias => 'see: ');
154         
155         my $keys = Shiar_Sheet::Keyboard({
156                 'mode' => {
157                         desc => 'mode description',
158                         'A' => 'a', # alias
159                         'a' => ['description', 'classes', 'comments (on hover)'],
160                 },
161         });
162         $keys->map('dvorak') or die "Keyboard map not found";
163         
164         $keys->print_rows;
165
166 =head1 DESCRIPTION
167
168 Used by http://sheet.shiar.nl to display keyboard sheets.
169 Assumes specific stylesheets and javascript from this site,
170 so probably not of much use elsewhere.
171
172 =head1 AUTHOR
173
174 Mischa POSLAWSKY <perl@shiar.nl>
175
176 =head1 LICENSE
177
178 Licensed under the GNU Affero General Public License version 3.
179