Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created January 22, 2014 23:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/ff01ad3b3cc4839afbca to your computer and use it in GitHub Desktop.
Save ttscoff/ff01ad3b3cc4839afbca to your computer and use it in GitHub Desktop.
Convert MultiMarkdown tables into dasheets format for Dash
#!/usr/bin/ruby
require 'yaml'
if RUBY_VERSION.to_f > 1.9
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
def show_usage
puts "#{File.basename(__FILE__,'.rb')}: Convert MultiMarkdown document to dasheets format}"
puts "Usage: #{__FILE__} filename.md"
Process.exit
end
if ARGV[0] == "debug"
input =<<ENDINPUT
title: Brett Test Docset
name: Brett
intro: Just trying this thing out to see what happens
---
|name|command|note|
|----|-------|----|
|Do something|⌘B|This does something|
|Something else|^⌘K||
|Nasty|⌘⇧⌥^S|this is nasty
a name|a command|
[General Commands]
---
|name|command|note|
|----|-------|----|
|Do something else|⌘B|This does something\\n* with a\\n* multiline note|
|Something worse|^⌘K||
Crazy Fox|⌘⇧⌥^S|this is crazy
[Secondary Commands]
ENDINPUT
else
if ARGV.length > 0
infile = File.expand_path(ARGV[0])
if File.exists?(infile)
input = IO.read(infile)
else
puts "File not found: #{infile}"
Process.exit 1
end
elsif STDIN.stat.size > 0
input = STDIN.read
if RUBY_VERSION.to_f > 1.9
input = input.force_encoding('utf-8')
end
else
show_usage
end
end
class String
def esc
self.gsub(/(')/,"\\\\'")
end
end
def convert_table(input)
category = {}
commands = []
in_body = false
input.split(/[\n\r]/).each { |line|
if in_body
case line.strip
when /^\|?(.*?)\|(.*?)\|(.*?)\|?\s*$/smu
commands.push({ 'name' => $1, 'command' => $2, 'note' => $3 })
when /^\[(.*?)\]$/
category['id'] = $1
end
else
in_body = true if line =~ /(\|?\s*[:\-]+\s*\|?)/
end
}
category['commands'] = commands
return false unless category['id'] && category['commands'].length > 0
category
end
categories = []
sections = input.split(/^\-{3,}\s*$/)
if sections[0] =~ /^\s*$/s
yaml = sections[1]
sections = sections[2..-1]
else
yaml = sections[0]
sections = sections[1..-1]
end
headers = YAML.load(yaml.strip)
sections.each {|section|
/^\s{0,3}(\|?\s*name\s*\|\s*command\s*\|\s*note\s*\|?.*?\[.*?\])\s*$/ismu.match(section.strip) {|m|
categories.push(convert_table(m[1].strip))
}
}
output = "cheatsheet do\n\ttitle '#{headers['title'].esc}'\n\tshort_name '#{headers['name'].esc}'\n\tintroduction '#{headers['intro'].esc}'\n"
categories.each {|cat|
next unless cat
output += "\tcategory do\n\t\tid '#{cat['id'].esc}'\n"
cat['commands'].each do |cmd|
note = cmd['note'] == "" ? "" : %Q{\t\t\tnotes %(#{cmd['note'].esc})\n}
output += %Q{\t\tentry do\n\t\t\tname '#{cmd['name'].esc}'\n\t\t\tcommand '#{cmd['command'].esc}'\n#{note}\t\tend\n}
end
output += "\tend\n"
}
output += "end"
title = headers['title'] + ".cheatsheet.txt"
File.open(File.expand_path(title), 'w+') do |f|
f.puts output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment