Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created May 10, 2011 17:05
Show Gist options
  • Save thinkerbot/964901 to your computer and use it in GitHub Desktop.
Save thinkerbot/964901 to your computer and use it in GitHub Desktop.
Generate madlibs
*/*.txt
*.sh
*.gz
#! /usr/bin/env ruby
require 'optparse'
require 'fileutils'
require 'digest/sha1'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: generate.rb [options] FILE"
opts.separator %q{
Generates the madlib files. Inputs a file with entries in the format:
%
Verb
Verb
Yoda: No. $1 not. $2... or $2 not. There is no $1.
}
opts.separator "Options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
inputs, outputs = [], []
current = inputs
ARGV.each do |file|
basename = file.chomp(File.extname(file))
madlib_dir = "#{basename}"
madlib_sh = "#{basename}.sh"
File.open(file) do |io|
if File.exists? madlib_dir
FileUtils.rm_r madlib_dir
end
FileUtils.mkdir madlib_dir
File.open(madlib_sh, "w") do |sh|
io.each_line do |line|
case line.strip
when '%'
next if inputs.empty?
output = outputs.join("\n").strip
sha1 = Digest::SHA1.hexdigest(output)[0,8]
File.open("#{madlib_dir}/#{sha1}.txt", "w") do |io|
io.puts "# Fill in the blanks"
inputs.each do |input|
io.print input.strip.chomp(':')
io.puts ': '
end
end
sh.puts %{madlib#{sha1} () { echo "#{output}"; }}
inputs.clear
outputs.clear
current = inputs
when ''
current = outputs
else
current << line
end
end
end
end
system "gzip -f #{madlib_sh}"
File.open(madlib_sh, "w") do |io|
io.puts <<-SCRIPT
#! /bin/sh
# Generated by: https://gist.github.com/964901
usage="usage: %s [INPUT_FILES...]\\n"
option=" %s %s\\n"
while getopts "ph" opt
do
case $opt in
p ) gzip -c -d "#{madlib_sh}.gz"
exit 0 ;;
h ) printf "$usage" $0
printf "$option" "-p" "prints the madlib functions"
printf "$option" "-h" "prints this help"
exit 0 ;;
\? ) printf "$usage" $0
exit 2 ;;
esac
done
shift $(($OPTIND - 1))
# Manually set all madlib inputs by default.
if [ $# -eq 0 ]
then set #{madlib_dir}/*
fi
# Eval madlib functions so they are available to the loop.
eval "$(gzip -c -d "#{madlib_sh}.gz")"
# Loop through and print madlibs!
for inputfile in "$@"; do
madlib$(basename $inputfile .txt) $(grep -o ":.*" $inputfile | tr ':' ' ' | xargs)
echo
done
SCRIPT
end
end
%
Verb:
Verb:
Yoda: No. $1 not. $2... or $2 not. There is no $1.
%
Name:
Noun:
Name:
Darth Vader: [having cornered $1 during their $2 battle] You are beaten. It is useless to resist. Don't let yourself be destroyed as $5 did.
%
Noun:
Adjective:
Noun:
Luke: I want my $1 back. I'm gonna need it to get out of this $2 $3.
Yoda: $3? $2? My home this is!
%
Noun:
Noun:
C-3PO: Don't blame me. I'm an $1. I'm not supposed to know a $2 from a computer terminal.
%
Adjective:
Noun:
Echo Base Officer: Echo station 3-T-8, we have spotted $1 $2.
%
Adjective:
Noun:
Yoda: [points to a cave opening beneath a large tree] That place... is strong with the $1 side of the Force. A domain of $2 it is. In you must go.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment