digraphs: smart deprecation of xorg alternates
[sheet.git] / digraphs.plp
1 <(common.inc.plp)><:
2 use 5.010;  # state
3
4 Html({
5         title => 'digraph cheat sheet',
6         version => 'v1.0',
7         description => [
8                 "Complete table of digraph characters from RFC-1345.",
9         ],
10         keywords => [qw'
11                 digraph mnemonic compose composition pair
12                 character char glyph table unicode vim
13         '],
14         stylesheet => [qw'light'],
15         data => [qw( digraphs.inc.pl )],
16 });
17
18 :>
19 <h1>RFC-1345 Digraphs</h1>
20
21 <p>Character mnemonics
22 following composition key ⎄:
23 i^k in <a href="/vi">Vim</a>,
24 ^u^\ in <a href="/readline">Emacs</a>,
25 ^a^v in <a href="/screen">Screen</a>.
26 Also see <a href="/unicode">common Unicode</a>.</p>
27
28 <p class="aside">Unofficial <span class="u-prop ex">proposals</span>
29 are available as <a href="/digraphs.vim">ex commands</a>.</p>
30
31 <:
32 my $di = do 'digraphs.inc.pl'
33         or die "Error loading digraphs data: ", $@ // $!;
34
35 my @chars = (
36         [qw{! " % ' ( ) * + , - . /}],
37         ['0'..'9'], [qw{: ; < = > ?}],
38         ['A'..'M'], ['N'..'Z'],
39         ['a'..'m'], ['n'..'z'],
40 );
41 my @chars2 = (['_'], @chars);  # trailing character (extended set)
42 my @columns = !exists $get{split} ? \@chars2 :
43         ([@chars2[0, 1, 3, 4, 6]], [@chars2[2, 5, 7]]);
44
45 if (exists $get{xorg}) {
46         my $xorg = do 'data/digraphs-xorg.inc.pl'
47                 or die "Error loading Xorg data: ", $@ // $!;
48         $_ = [ord $_] for values %{$xorg};
49         $xorg->{$_}->[2] = # class = compatibility
50                 $di->{$_} ? $di->{$_}->[0] != $xorg->{$_}->[0] ? 'l1' :  # conflict
51                 $di->{$_}->[2] =~ /\bu-di\b/ ? 'l5' : 'l3' : 'l2'  # rfc|any|none
52                 for keys %{$xorg};
53
54         for my $cp (map {$_->[0]} values %{$xorg}) {
55                 next if (state $seen = {})->{$cp}++;  # List::MoreUtils::uniq
56
57                 # find multiple equivalent mnemonics
58                 my @equiv = grep {$cp eq $_->[0]}
59                         map {$xorg->{$_}} sort keys %{$xorg}; # values ordered by mnem.
60
61                 # search for the most compatible match
62                 my ($compat) = sort {
63                         $equiv[$b]->[2] cmp $equiv[$a]->[2]  # highest level
64                         || $b <=> $a  # fallback to last mnemonic
65                 } 0 .. $#equiv;
66
67                 # reclassify all but one as level 0 (omitted)
68                 splice @equiv, $compat // -1, 1, ();
69                 $_->[2] = 'l0 ex' for @equiv;
70         }
71
72         $chars2[0] = [qw( # ^ _ ` ~ )];
73         @chars = @chars2;
74         $di = $xorg;
75 }
76
77 for my $colchars (@columns) {
78 print '<table class="glyphs dimap"><col>';
79 print qq'<colgroup span="$_">' for map {scalar @$_} @{$colchars};
80 print "</colgroup><col>\n";
81 for my $section (qw{thead tfoot}) {
82         print "<$section><tr><th>↳";
83         print '<th>', EscapeHTML($_) for map {@$_} @{$colchars};
84         print "<th>&nbsp;\n";
85 }
86 for my $c1group (@chars) {
87         print '<tbody>';
88         for my $c1 (@$c1group) {
89                 print '<tr><th>', EscapeHTML($c1);
90                 for my $c2 (map {@$_} @$colchars) {
91                         my $mnem = $c1 . $c2;
92                         if (not defined $di->{$mnem}) {
93                                 print '<td>';
94                                 next;
95                         }
96                         if (ref $di->{$mnem} ne 'ARRAY') {
97                                 printf '<td class="X Xr" title="%s">', EscapeHTML($mnem);
98                                 next;
99                         }
100                         my ($codepoint, $name, $prop, $script, $string) = @{ $di->{$mnem} };
101
102                         my $glyph = $string || chr $codepoint;
103                         utf8::upgrade($glyph);  # prevent latin1 output
104                         my $desc = $mnem . ($name && " ($name)");
105                         my @class = ('X', grep {$_} $prop, $script);
106
107                         $glyph = EscapeHTML($glyph);
108                         $glyph = "<span>$glyph</span>" if $script =~ /\bZs\b/;
109
110                         printf "\n".'<td class="%s" title="%s">%s',
111                                 join(' ', @class), EscapeHTML($desc), $glyph;
112                 }
113                 print "\n<th>", EscapeHTML($c1), "\n";
114         }
115 }
116 print "</table>\n";
117 print '<hr>' if exists $get{split};
118 }
119 :>
120 <div class="legend">
121         <table class="glyphs"><tr>
122         <td class="X Cc">control
123         <td class="X Zs"><span>space</span>
124         <td class="X Mn">combining
125         <td class="X Sk">spacing&nbsp;modifier
126         <td class="X Pf">quote
127         <td class="X Po">punctuation
128         <td class="X So">symbol
129         <td class="X Sm">math
130         <td class="X Sc">currency
131         <td class="X No">numeric
132         <td class="X Greek">greek
133         <td class="X Cyrillic">cyrillic
134         <td class="X Latin">latin
135         <td class="X Hebrew">hebrew
136         <td class="X Arabic">arabic
137         <td class="X Hangul">korean
138         <td class="X Hiragana">japanese
139         <td class="X Bopomofo">chinese
140         </table>
141
142         <table class="glyphs"><tr>
143         <td class="X">unicode
144         <td class="X Xl">latin1
145         <td class="X Xa">ascii
146         <td class="X u-prop">vim extension
147         <td class="X u-prop ex">proposal
148         <td class="X ex">not in vim
149         </table>
150 </div>
151