forked from ttscoff/KeyBindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_keybindings.rb
executable file
·167 lines (140 loc) · 4.92 KB
/
document_keybindings.rb
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
160
161
162
163
164
165
166
167
#!/usr/bin/ruby
# encoding: utf-8
require 'rubygems'
require 'shellwords'
def class_exists?(class_name)
klass = Module.const_get(class_name)
return klass.is_a?(Class)
rescue NameError
return false
end
if class_exists? 'Encoding'
Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external')
Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal')
end
infile = 'DefaultKeyBinding.dict'
begin
input = IO.read(infile).force_encoding('utf-8')
rescue
input = IO.read(infile)
end
date = Time.now.strftime('%m/%d/%Y')
output = ''
style =<<STYLE
<style>
table { margin-bottom:20px; border:none; width: 100%; }
caption { text-align:left; padding:5px; font-weight:bold; border: dotted 1px #777;background:#eee; margin-bottom:10px }
td,th { font-weight:bold; padding:3px; border: solid 1px #ccc; padding:4px }
td:nth-child(1),td:nth-child(2),td:nth-child(3) { width:40px}
td:first-child {font-weight:bold !important}
td:last-child { font-weight:normal;width:auto }
</style>
STYLE
intro =<<INTRO
DefaultKeyBinding.dict file (`~/Library/KeyBindings/DefaultKeyBinding.dict`) for Mac OS X, created by [Brett Terpstra][] and based heavily on work done by [Lri][lrikeys].
Please note that these bindings won't work in all applications: TextWrangler and TextMate, for example, override these with their own settings.
See Lri's [gists][lrigists] and [website][lriweb] for more coding madness.
[lrikeys]: http://www.cs.helsinki.fi/u/lranta/keybindings/
[lriweb]: http://www.cs.helsinki.fi/u/lranta/
[lrigists]: https://gist.github.com/Lri
[Brett Terpstra]: http://brettterpstra.com
<b>Installation</b>: Copy the DefaultKeyBinding.dict file to the `~/Library/KeyBindings/` directory (create `KeyBindings` if it doesn't already exist).
Any open applications will need to be re-started before the key bindings will take effect -- or log out and log back in.
<b>Documentation</b> <i>(last updated #{date}.)</i>
*Grouped items begin with the groups shortcut (if exists), followed by a subgroup (if exists) followed by the keys specified.*
INTRO
outro =<<OUTRO
This documentation is generated automatically from the comments and commands in the DefaultKeyBinding.dict file. The script `document_keybindings.rb` is free for use, but it's specifically designed for use with my formatting in the bindings plist (i.e. it's a little finicky).
OUTRO
toplevel = []
level = 0
prefix = false
group_command = ''
group_desc = ''
subgroup_command = ''
subgroup_desc = ''
desc = ''
command = ''
skip = false
note = ''
def e_sh(str)
# rx = Regexp.new '(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])', nil, 'n'
# str.to_s.gsub(rx, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
Shellwords.escape(str)
end
def translate_command(str)
str = str.gsub(/~/,'⌥').gsub(/@/,'⌘').gsub(/\$/,'⇧').gsub(/\^/,'⌃')
str = str.gsub('\UF700','↑').gsub('\UF701','↓').gsub('\UF703','→').gsub('\UF702','←')
str = str.gsub('\U0009','⇥').gsub('\U000D','↩').gsub('\U001B','⎋').gsub('\U000A','␍')
str = str.gsub('\UF728','⌦').gsub('\177','⌫')
str = str.gsub('\040','␣')
str = str.gsub(/([\[\]|])/,'\\\\\1')
str = str.gsub(/([A-Z])/,'⇧\\1').downcase
str
end
# input.gsub!(/\\n/,'')
# input.gsub!(/```/,'\`\`\`')
input.split("\n").each {|line|
if line =~ /^\s*$/ || line =~ /^\s*\/\/\s*(TODO)/
next
elsif line =~ /^\s*\/\/\s*>\s*(.*)$/
note += " " + $1
elsif line =~ /^\s*\};\s*$/
level -= 1
if level == 1
subgroup_command = ''
subgroup_desc = ''
output += "|||||\n"
elsif level == 0
# output += "[ #{group_desc} ]\n\n"
group_command = ''
group_desc = ''
end
next
elsif line =~ /^\s*\/\/\s*(.*)/
desc = $1
next
elsif line =~ /^\s*"([^"]+)"\s*=\s*\{.*?\/\/\s*(.*)/
level += 1
if level == 1
group_command = translate_command($1)
group_desc = $2
output += "\n\n|#{group_desc} (#{group_command})||||\n|:----:|:----:|:----:|:----|\n"
elsif level == 2
subgroup_command = translate_command($1)
subgroup_desc = $2
output += "|#{subgroup_desc} (#{subgroup_command})||||\n"
else
prefix = $1
end
next
elsif line =~ /^\s*"([^"]+)"\s*=\s*\(/
command = translate_command($1)
note = "(#{note})" if note != ''
if level == 0
toplevel.push("|#{command}|#{desc} #{note}|\n")
else
command = prefix + "," + command if prefix
output += "|#{group_command} |#{subgroup_command} |#{command} |#{desc} #{note}|\n"
end
note = ''
end
}
topoutput = "|General Commands||\n|Key|Function|\n|:----:|:----|\n"
toplevel.each {|line|
topoutput += line
}
# topoutput += "[ General Commands ]\n\n"
# output = style + topoutput + output
output = topoutput + output
File.open('keybindings.md','w') {|f|
f.puts intro
f.puts output
f.puts outro
}
htmlout = %x{echo #{e_sh output}|/usr/local/bin/multimarkdown}
outfile = File.new('readme.md','w')
outfile.puts intro
outfile.puts htmlout
outfile.puts outro
outfile.close