common module FormatChar to show character tables
[sheet.git] / Shiar_Sheet / Keyboard.pm
index f56437f7e6160830f650567aabbe632c036b67fd..cca8333547d448d2dfbe98a34289d38bbf08da8c 100644 (file)
@@ -5,11 +5,13 @@ use warnings;
 no  warnings 'uninitialized';  # save some useless checks for more legible code
 use Carp;
 
-our $VERSION = '1.00';
+our $VERSION = 'v1.05';
 
-my @casedesc = qw(ctrl shift);
+my @casedesc = (undef, qw/shift ctrl meta/, 'shift meta');
 my @rowdesc = qw(numeric top home bottom);
 my %keyrows = do 'keys.inc.pl';
+# add first two cases of each row again with each char prepended by + (alt)
+push @$_, map { [map {"+$_"} @$_] } @$_[0,1] for map {@$_} values %keyrows;
 
 my %keytrans = qw(
        ^@ NUL ^a SOH ^b STX ^c ETX  ^d EOT ^e ENQ ^f ACK ^g BEL
@@ -45,6 +47,7 @@ sub map {
 sub escapeclass {
        local $_ = shift;
        s/\^/_c/g;
+       s/\+/_m/g;
        s/\[/_sbo/g;
        s/\]/_sbc/g;
        s/^$/_/;
@@ -63,7 +66,7 @@ sub keyunalias {
        my $self = shift;
        my ($key, $ancestry) = @_;
 
-       $key =~ s/(\S*?)(\^?\S)($|\s.*)/$2/;
+       $key =~ s/(\S*?)(\+?\^?\S)($|\s.*)/$2/;
        my $mode = $1;
        my $keyinfo = $self->{keys}->{$mode}->{$key};
 
@@ -84,47 +87,57 @@ sub print_key {
 
 #      $key = $keytrans{$key} if defined $keytrans{$key};
        my $keytxt = $mode . escapehtml($key) if $key ne '^0';
-          $keytxt .= $self->{sign}->{arg} while $flags =~ s/ ?\barg\b//;  # argument
+          $keytxt .= $self->{sign}->{$1} while $flags =~ s/(?:^| )(arg[a-ln-z]?)\b//;  # arguments
           $keytxt .= "<small>$self->{sign}->{motion}</small>" if $flags =~ s/ ?\bargm\b//;  # motion argument
           $keytxt =~ s{\^(?=.)}{<small>^</small>};  # element around ctrl-identifier
+          $keytxt =~ s{\+(?=.)}{<small>+</small>};  # meta
+       my $keyhint = defined($mnem) && qq{ title="$mnem"};
+          $keytxt  = "<b$keyhint>$keytxt</b>";
+          $keytxt .= ' '.$desc if defined $desc;
+          $keytxt = qq{<a href="/$1">$keytxt</a>} if $flags =~ s/ ?\blink(\S*)//;
        my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $self->{keys}{$1} && sprintf(
                ' onclick="setmode(%s)"',
                $1 eq '' ? '' : sprintf(q{'mode%s'}, escapeclass($1))
        );
-       $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
-               if $flags =~ s/ ?\blink(\S*)//;
-       my $keyhint = defined($mnem) && qq{ title="$mnem"};
+       $flags .= ' chr'.ord(substr $key, -1) if $key ne '^0';
 
-       print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
-       print ' ', $desc if defined $desc;
+       print qq{\t\t<li class="$flags"$onclick>$keytxt};
        print "\n";
 }
 
 sub print_rows {
        my $self = shift;
-       my $static = shift;
-       my @moderows = $static ? split(/\s+/, $static) : sort keys %{ $self->{keys} };
+       my %moderows = (
+               -DEFAULT => !@_ ? '' : split(/(?:\s*([^=\s]*)=\s*)/, shift),
+               # plus specific mode overrides prefixed by '='
+       );
+       my $defrows = shift || [2, 1, 0];
+       my @modes = sort keys %{ $self->{keys} };
 
        for (my $row = 0; $row <= $#{ $keyrows{$self->{map}} }; $row++) {
                my $keyrow = $keyrows{$self->{map}}->[$row];
-               my @caserows = 0 .. $#$keyrow;
 
                print qq{<li class="row row$row"><ul>\n};
-               for my $modefull (@moderows) {
-                       my $mode = $modefull;
-                       my @showcase = $mode =~ s/(\d+)(?:-(\d+))?$//
-                               ? (map {3 - $_} split //, $row == 0 && $2 || $1) : @caserows;
+               for my $basemode (@modes) {
+                       my @moderows = split /\s+/,
+                               defined $moderows{$basemode} ? $moderows{$basemode} : $moderows{-DEFAULT};
+
+               for my $submode (@moderows ? @moderows : '') {
+                       my $mode = $basemode . $submode;
+                       my @caserows = $mode =~ s/(\d+)(?:-(\d+))?$//
+                               ? (map {$_ - 1} split //, $row == 0 && $2 || $1)  # user override
+                               : @$defrows;  # default
                        my $modekeys = $self->{keys}{$mode};
 
-                       for my $case (@showcase) {
+                       for my $case (@caserows) {
                                my $keycase = $keyrow->[$case] or next;
                                  @$keycase or next;
 
-                               printf "\t<li%s>", $mode ne '' && sprintf(
-                                       ' class="%s"', ($static ? '' : 'mode ') . 'mode' . escapeclass($mode)
+                               printf "\t<li%s>", $basemode ne '' && sprintf(
+                                       ' class="%s"', 'mode mode' . escapeclass($basemode)
                                );
                                printf("<h3>%s<small>: %s</small></h3>\n", # XXX insert &nbsp; here to fix msie<=6
-                                               $modekeys->{desc} || "mode $mode",
+                                               $modekeys->{desc} || "mode $basemode",
                                                "$rowdesc[$row] row $casedesc[$case]"
                                );
                                my $caseclass = 'keys';
@@ -135,7 +148,9 @@ sub print_rows {
                                print qq{\t\t</ul>\n};
                        } # case
 
-               } # mode
+               } # submode
+
+               } # basemode
                print qq{\t</ul>\n};
        } # row
 }
@@ -163,13 +178,13 @@ Shiar_Sheet::Keyboard - Output HTML for key sheets
 
 =head1 DESCRIPTION
 
-Used by http://sheet.shiar.net to display keyboard sheets.
+Used by http://sheet.shiar.nl to display keyboard sheets.
 Assumes specific stylesheets and javascript from this site,
 so probably not of much use elsewhere.
 
 =head1 AUTHOR
 
-Mischa POSLAWSKY <perl@shiar.org>
+Mischa POSLAWSKY <perl@shiar.nl>
 
 =head1 LICENSE