Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created June 7, 2014 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/f52c48e2455a75374dc8 to your computer and use it in GitHub Desktop.
Save michaelfeathers/f52c48e2455a75374dc8 to your computer and use it in GitHub Desktop.
Audiobook scripting utility
# Accepts program file names on the command line and renders them to stdout as a sequence of spoken words
token_map = {
"[" => "left-square-bracket",
"]" => "right-square-bracket",
"!" => "exclamation-point",
"\\" => "backslash",
"#" => "pound sign",
"\"" => "double-quote",
"$" => "dollar-sign",
"%" => "percent-sign",
"&" => "ampersand",
"'" => "single-quote",
"(" => "left-paren",
")" => "right-paren",
"*" => "star",
"+" => "plus-sign",
"," => "comma",
"." => "dot",
"/" => "forward-slash",
":" => "colon",
";" => "semicolon",
"<" => "less-than sign",
"=" => "equals sign",
">" => "greater-than sign",
"?" => "question mark",
"@" => "at sign",
"^" => "caret",
"_" => "underscore",
"`" => "back tic",
"{" => "left curly-brace",
"|" => "vertical bar",
"}" => "right curly-brace",
"~" => "tilde",
"-" => "dash",
" " => "space",
"\n" => "new line",
"\t" => "tab",
}
puts ARGF.read
.chars
.chunk {|ch| ch =~ /[[:alnum:]]/ ? :alone : true }
.map {|key,values| key == :alone ? values.join : values }
.flatten(1)
.zip((0...Float::INFINITY))
.map {|token,line_no| "#{line_no}: #{token_map[token] || token}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment