-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts.perl
159 lines (103 loc) · 3.29 KB
/
shortcuts.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
my $arg1 = $ARGV[1] or die "Please specify 'full' or 'common'\n";
my $fullTable = ($arg1 eq "full");
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
sub formatKeys {
my $field = trim $_[0];
my @formatKeys;
my @shortcutRange = split " - ", $field;
for my $i (0 .. $#shortcutRange) {
my $shortcutItem = trim($shortcutRange[$i]);
my @rawKeys = split "\\+" , $shortcutItem;
$formatKeys[$i] = join "", "<kbd><kbd>", join("</kbd>+<kbd>", @rawKeys), "</kbd></kbd>";
$formatKeys[$i] =~ s/\+/+/g;
}
return join " – ", @formatKeys;
};
my $inTable = 0;
while (my $rawLine = <$data>) {
my $line = $rawLine;
chomp $line;
# parse line
if (substr($line, 0, 1) eq "*") {
# line not "important"
if (!$fullTable) {
next;
}
$line = substr($line, 1);
}
if (!length $line) {
# this line is empty
print "\n";
next;
}
my @fields = split(", " , $line);
if (scalar @fields == 1) {
# this line is plain text
$line =~ s/\,/,/g;
if ($inTable) {
# leave table
$inTable = 0;
}
if (substr($line, 0, 1) eq "#") {
# markdown heading
print $line;
# add anchor info (non-standard)
#my $anchor = lc $line;
#$anchor =~ s/#+ //;
#$anchor =~ s/[,.;\'\"\!\\\/]//g;
#$anchor =~ s/ /-/g;
#print " {#", $anchor, "}";
print "\n";
} else {
# other text
print $line, "\n";
}
} else {
# this line is a command
if (!$inTable) {
# generate headings
# headings for combined shortcut columns
#print "Action | Shortcut\n---|---\n";
# headings for separate shortcut columns
print "Action | Windows/Linux | macOS\n---|---|---\n";
$inTable = 1;
}
my $command = trim($fields[0]);
my $shortcut = trim($fields[1]);
my $formatKeysRange = formatKeys($shortcut);
my $formatKeysRangeMac = "";
if (scalar @fields == 2) {
# no explicit macOS shortcut given; try to derive automatically
my $shortcutMac = $shortcut;
$shortcutMac =~ s/Ctrl/Cmd/g;
$shortcutMac =~ s/Alt/Option/g;
$shortcutMac =~ s/F\d+/Fn+$&/g;
$shortcutMac =~ s/Home/Fn+Left/g;
$shortcutMac =~ s/End/Fn+Right/g;
$shortcutMac =~ s/PgUp/Fn+Up/g;
$shortcutMac =~ s/PgDn/Fn+Down/g;
#if ($shortcut ne $shortcutMac) {
$formatKeysRangeMac = formatKeys($shortcutMac);
#}
} else {
# use explicit macOS shortcut
$formatKeysRangeMac = formatKeys($fields[2]);
}
# create final output
my $outputFormatCombined = "%s | %s\n";
my $outputFormatSeparate = "%s | %s | %s\n";
my $formatKeysRangeCombo = $formatKeysRange;
if ($formatKeysRangeMac) {
$formatKeysRangeCombo = join "", $formatKeysRangeCombo, " (macOS: ", $formatKeysRangeMac, ")";
}
# show shortcuts for Windows/Linux and macOS combined in one column
#printf $outputFormatCombined, $command, $formatKeysCombo;
# show shortcuts for Windows/Linux and macOS in separate columns
printf $outputFormatSeparate, $command, $formatKeysRange, $formatKeysRangeMac;
}
}