Created
July 18, 2010 18:16
-
-
Save rchowe/480590 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 | |
=begin | |
> An interactive novel | |
> This program parses formatted text files and turns them into an interactive | |
> novel. Currently, the text files support two types of lines: display lines, or | |
> lines beginning with a greater than sign. Those lines will be printed out to | |
> the console. This program also supports prompt lines. These lines will take | |
> user input and store it in a variable. All other lines are ignored. This file | |
> can be run as an interactive novel. For completeness, please enter your name | |
> (to test prompting): | |
prompt ${name} | |
> Variables can be included on display lines. Just use a dollar sign and then | |
> curly braces. For example, your name is ${name}. | |
> | |
> By RC Howe (rchowe) | |
=end | |
if ARGV.length != 1 | |
puts "Usage: #{$0} novel" | |
Process.exit | |
end | |
begin | |
file = File.open( ARGV[0], File::RDONLY ) | |
rescue Errno::ENOENT | |
puts "Novel file does not exist." | |
Process.exit | |
end | |
lines = [] | |
variables = {} | |
file.each do |line| | |
next if line.nil? | |
if line[0].chr == ">" | |
lines.push( Proc.new { l = line[1..(line.length)].lstrip.chomp.gsub(/\$\{(.*?)\}/) { |m| variables[m]}; "#{l} " } ) | |
elsif line.match(/^prompt \$\{(.*)\}/i) | |
a = String.new $1 | |
p = Proc.new { if $last_line.nil? or $last_line.empty? then print "Sorry, I didn't catch that: "; $last_line = $stdin.gets.chomp; p.call; else variables["${#{a}}"] = $last_line; end; nil } | |
lines.push p | |
end | |
end | |
lines.each do |line| | |
l = line.call line | |
unless l.nil? | |
print l | |
$last_line = $stdin.gets.chomp | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment