1 package Shiar_Sheet::Keyboard;
5 no warnings 'uninitialized'; # save some useless checks for more legible code
10 my @casedesc = qw(ctrl shift);
11 my @rowdesc = qw(numeric top home bottom);
12 my %keyrows = do 'keys.inc.pl';
15 ^@ NUL ^a SOH ^b STX ^c ETX ^d EOT ^e ENQ ^f ACK ^g BEL
16 ^h BS ^i tab ^j LF ^k VT ^l FF ^m CR ^n SO ^o SI
17 ^p DLE ^q DC1 ^r DC2 ^s DC3 ^t DC4 ^u NAK ^v SYN ^w ETB
18 ^x CAN ^y EM ^z SUB ^[ ESC ^\ FS ^] GS ^^ RS ^_ US
26 croak 'Invalid key table specified' unless ref $keys eq 'HASH';
27 my $parent = (caller)[0]; # calling module
29 no strict 'refs'; # temporarily allow variable references
30 \%{ $parent.'::sign' }; # return %sign from parent
32 croak "%${parent}::sign not found" unless %$sign;
34 bless {sign => $sign, keys => $keys, map => 'qwerty'}, $self;
41 return $self->{map} = $mapname if defined $keyrows{$mapname};
64 my ($key, $ancestry) = @_;
66 $key =~ s/(\S*?)(\^?\S)($|\s.*)/$2/;
68 my $keyinfo = $self->{keys}->{$mode}->{$key};
70 return [] unless defined $keyinfo;
71 return $keyinfo if ref $keyinfo;
72 return if $ancestry->{$key}++; # endless loop failsafe
73 return $self->keyunalias($keyinfo, $ancestry);
78 my ($mode, $key, $keyinfo) = @_;
80 $keyinfo = [ $self->{sign}->{alias}.$keyinfo, $self->keyunalias($keyinfo)->[1] . ' alias' ]
81 if defined $keyinfo and not ref $keyinfo; # alias
82 my ($desc, $flags, $mnem) = @$keyinfo if defined $keyinfo;
83 defined $desc or $flags = $key eq '^0' ? 'ni' : 'no';
85 # $key = $keytrans{$key} if defined $keytrans{$key};
86 my $keytxt = $mode . escapehtml($key) if $key ne '^0';
87 $keytxt .= $self->{sign}->{arg} while $flags =~ s/ ?\barg\b//; # argument
88 $keytxt .= "<small>$self->{sign}->{motion}</small>" if $flags =~ s/ ?\bargm\b//; # motion argument
89 $keytxt =~ s{\^(?=.)}{<small>^</small>}; # element around ctrl-identifier
90 my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $self->{keys}{$1} && sprintf(
91 ' onclick="setmode(%s)"',
92 $1 eq '' ? '' : sprintf(q{'mode%s'}, escapeclass($1))
94 $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
95 if $flags =~ s/ ?\blink(\S*)//;
96 my $keyhint = defined($mnem) && qq{ title="$mnem"};
98 print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
99 print ' ', $desc if defined $desc;
106 my @moderows = $static ? split(/\s+/, $static) : sort keys %{ $self->{keys} };
108 for (my $row = 0; $row <= $#{ $keyrows{$self->{map}} }; $row++) {
109 my $keyrow = $keyrows{$self->{map}}->[$row];
110 my @caserows = 0 .. $#$keyrow;
112 print qq{<li class="row row$row"><ul>\n};
113 for my $modefull (@moderows) {
114 my $mode = $modefull;
115 my @showcase = $mode =~ s/(\d+)(?:-(\d+))?$//
116 ? (map {3 - $_} split //, $row == 0 && $2 || $1) : @caserows;
117 my $modekeys = $self->{keys}{$mode};
119 for my $case (@showcase) {
120 my $keycase = $keyrow->[$case] or next;
123 printf "\t<li%s>", $mode ne '' && sprintf(
124 ' class="%s"', ($static ? '' : 'mode ') . 'mode' . escapeclass($mode)
126 printf("<h3>%s<small>: %s</small></h3>\n", # XXX insert here to fix msie<=6
127 $modekeys->{desc} || "mode $mode",
128 "$rowdesc[$row] row $casedesc[$case]"
130 my $caseclass = 'keys';
131 $caseclass .= ' lead' if defined $modekeys->{lead}; # leading command key shown
132 $caseclass .= " $casedesc[$case]" if defined $casedesc[$case];
133 print qq{\t\t<ul class="$caseclass">\n};
134 $self->print_key($modekeys->{lead}, $_, $modekeys->{$_}) for @$keycase;
135 print qq{\t\t</ul>\n};
147 Shiar_Sheet::Keyboard - Output HTML for key sheets
151 our %sign = (alias => 'see: ');
153 my $keys = Shiar_Sheet::Keyboard({
155 desc => 'mode description',
157 'a' => ['description', 'classes', 'comments (on hover)'],
160 $keys->map('dvorak') or die "Keyboard map not found";
166 Used by http://sheet.shiar.net to display keyboard sheets.
167 Assumes specific stylesheets and javascript from this site,
168 so probably not of much use elsewhere.
172 Mischa POSLAWSKY <perl@shiar.org>
176 Licensed under the GNU Affero General Public License version 3.