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