Skip to content

Commit

Permalink
refactored command line interface
Browse files Browse the repository at this point in the history
Now the command-line interface...actually exists, to be honest. It uses
optparse to accept -t, -s, and -o options, as well as display help.
Hooray for progress!
  • Loading branch information
leafstorm committed Dec 26, 2011
1 parent 8090ca1 commit 30a14e7
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions railgen.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.


require 'optparse'
require 'haml' require 'haml'
require 'yaml' require 'yaml'


Expand Down Expand Up @@ -255,23 +256,51 @@ def each_line




class RenderContext class RenderContext
def initialize (network) def initialize (network, stylesheet)
@network = network @network = network
@stylesheet = "rail-style.css" @stylesheet = stylesheet
end end
end end


def render_template(template_name, network)
def render_template(template_name, network, stylesheet)
haml = Haml::Engine.new(File.read(template_name)) haml = Haml::Engine.new(File.read(template_name))
haml.render RenderContext.new(network) haml.render RenderContext.new(network, stylesheet)
end end



if __FILE__ == $0 if __FILE__ == $0
options = {:template => "templates/listing.haml", :output => nil,
:style => "rail-style.css"}
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: ruby railgen.rb [OPTIONS] DATAFILE"

opts.on("-t", "--template FILE", "Use this Haml template") do |t|
options[:template] = t
end
opts.on("-o", "--output FILE", "Write output to this HTML file") do |f|
options[:output] = f
end
opts.on("-s", "--stylesheet LINK", "Use this stylesheet reference") do |s|
options[:style] = s
end
opts.on_tail("-h", "--help", "Display help") do
puts opts
exit
end
end
option_parser.parse!

data = ARGV[0] data = ARGV[0]
html = ARGV[1] unless data
puts option_parser
exit
end
template = options[:template]
output = (options[:output] or File.basename(template).gsub("haml", "html"))

network = RailNetwork.from_file(data) network = RailNetwork.from_file(data)
File.open(html, 'w') do |io| File.open(output, 'w') do |io|
io.write render_template("templates/listing.haml", network) io.write render_template(template, network, options[:style])
end end
end end

0 comments on commit 30a14e7

Please sign in to comment.