#!/usr/bin/env ruby lib = File.expand_path(File.dirname(__FILE__) + '/../lib') $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib) require 'docify' require 'optparse' # ------------------------------------------------------------------------------ # Default options # ------------------------------------------------------------------------------ options = { :format => nil, :output => nil, :html => true, :css => true } # ------------------------------------------------------------------------------ # Setup OptParser # ------------------------------------------------------------------------------ optparse = OptionParser.new do |opts| opts.banner = "Usage: docify [options] FILE" opts.on('-l', '--list', 'List of all formats') do $stdout.puts("Formats:") Docify::FORMATS.each { |f| $stdout.puts("- #{f}") } exit end opts.on('-f', '--format FORMAT', 'Render as format') do |f| unless Docify.valid_format?(f) $stderr.puts("Invalid format: #{f}") exit end options[:format] = f end opts.on('--no-html', 'Disable HTML') { options[:html] = false } opts.on('--no-css', 'Disable css styling') { options[:css] = false } opts.on('-o', '--output=PATH', 'Output file path') do |path| options[:output] = path end opts.on('-h', '--help', "Show this information") { $stdout.puts(opts.to_s) ; exit } end # ------------------------------------------------------------------------------ # Execute # ------------------------------------------------------------------------------ begin optparse.parse! file = ARGV.shift.to_s.strip unless file.empty? file = File.expand_path(file) begin doc = Docify::Document.new(file) doc.render(options) if options[:output].nil? $stdout.puts(doc.content) else doc.save_to(options[:output]) end rescue Exception => e $stderr.puts("Error: #{e.message}") end else $stderr.puts("Error: file required.") $stderr.puts(optparse.to_s) end rescue OptionParser::InvalidOption => e $stderr.puts("Error: #{e.message}") $stderr.puts(optparse.to_s) end