kakmap.rb (1837B)
1 #!/usr/bin/env ruby 2 3 # Generate a reference sheet for Kakoune's normal mode 4 # Use: ./kakmap.rb ../src/normal.cc 5 6 require 'markaby' 7 8 # Relies on the keymap HashMap assignment ending with }; 9 raw = ARGF.read.split( /const\s+HashMap<Key,\s*NormalCmd>\s+keymap\s*{/ ).last.split( /^};$/ ).first 10 11 commands = {} 12 13 raw.split( /\n+/ ).each{ |line| 14 # skip empty or comment line 15 line = line.strip 16 if line.empty? or /^\/\// =~ line 17 next 18 end 19 20 # match key mapping line 21 /^\{\s*\{(?<mdky>[^}]+)\}\s*,\s*\{\s*"(?<dsc>[^"]+)"/.match(line) do |m| 22 modAndKey = m['mdky'] 23 des = m['dsc'] 24 25 modAndKey.gsub!(/\s*\/\*[^*]+\*\/\s*/, '') # remove comment in key definition 26 27 # match key and modifier 28 /Key::(?<key>\w+)|(?<mod>alt|ctrl)\('\\?(?<key>.+?)'\)|'\\?(?<key>.+?)'$/.match(modAndKey) do |sm| 29 key = sm['key'] 30 mod = (sm['mod'] || 'none').to_sym 31 32 key = 'Space' if key == ' ' 33 commands[key] ||= {} 34 commands[key][mod] = des 35 end 36 end 37 } 38 39 # sort, showing single characters first, symbols next and spelled out keys last 40 commands = commands.sort_by{ |key, _| 41 case key 42 when /^\w$/ 43 key.upcase + key.swapcase 44 when /^\W$/ 45 '_' + key 46 else 47 '~~' + key 48 end 49 } 50 51 puts Markaby::Builder.new { 52 html do 53 head do 54 title "Kakoune default keymap" 55 end 56 body do 57 table :style => "border-collapse: collapse" do 58 thead do 59 tr do 60 th "Key" 61 th "Description" 62 th "ALT + key" 63 th "CTRL + key" 64 end 65 end 66 tbody do 67 for key, binding in commands 68 tr :style => "border-bottom: 1px solid #fbfbfb" do 69 th key 70 td binding[:none] 71 td binding[:alt] 72 td binding[:ctrl] 73 end 74 end 75 end 76 end 77 end 78 end 79 } 80