Last active
December 21, 2015 18:09
-
-
Save tonini/6345606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'prawn' | |
class CabbageCheatSheet | |
EMACS_KEY_REGEX = /\".+\"|\[.+\]/ | |
EMACS_FUNCTION_REGEX = /'(.+)$/ | |
EMACS_KEYSTROKE_REGEX = /cabbage-global-set-key/ | |
def initialize | |
@pdf = Prawn::Document.new(:margin => 5) | |
end | |
def build! | |
@pdf.font("Courier", :size => 18) | |
@pdf.text 'Cabbage' | |
@pdf.table(table_header + keystrokes, | |
:width => @pdf.bounds.width, | |
:row_colors => ["FFFFFF", "EEEEEE"], | |
:cell_style => { | |
:size => 7, | |
:font => "Courier", | |
:padding => [0, 0, 5, 5], | |
:borders => [] | |
}) do | |
row(0).font_style = :bold | |
end | |
end | |
def save_as(filename) | |
build! | |
@pdf.render_file filename | |
end | |
def table_header | |
[['function name', 'keystroke'] * 2] | |
end | |
private :table_header | |
def keystrokes | |
make_two_strokes_per_row(parse_keystrokes) | |
end | |
private :keystrokes | |
def make_two_strokes_per_row(keystrokes) | |
rows = [] | |
(keystrokes.size / 2).times do |count| | |
rows << keystrokes.shift(2).flatten | |
end | |
rows | |
end | |
private :make_two_strokes_per_row | |
def parse_keystrokes | |
file_with_keys = File.expand_path('../../bundles/ergonomic/bundle.el', __FILE__) | |
keystrokes = [] | |
File.open(file_with_keys, 'r').each_line do |line| | |
if keystroke?(line) | |
key = extract_keystroke(line) | |
function = extract_function_name(line) | |
keystrokes << [function, key] | |
end | |
end | |
keystrokes | |
end | |
private :parse_keystrokes | |
def keystroke?(line) | |
line =~ EMACS_KEYSTROKE_REGEX | |
end | |
private :keystroke? | |
def extract_keystroke(line) | |
line[EMACS_KEY_REGEX].gsub(/[\[\]]/, '').gsub(/"+/, '') | |
end | |
private :extract_keystroke | |
def extract_function_name(line) | |
line[EMACS_FUNCTION_REGEX, 1].gsub(/\)+/, '') | |
end | |
private :extract_function_name | |
end | |
cheat_sheet = CabbageCheatSheet.new | |
cheat_sheet.save_as("cheat-sheet.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment