src/myrurema.rb in myrurema-0.0.4 vs src/myrurema.rb in myrurema-0.1.0

- old
+ new

@@ -1,8 +1,9 @@ require 'optparse' require 'pathname' require 'shellwords' +require 'tmpdir' class Pathname; alias / +; end class Options def initialize(argv) @@ -10,11 +11,10 @@ @open_browser = false @port = nil @dry_run = false @ruremadir = Pathname("~/.rurema").expand_path @rubyver = RUBY_VERSION - @query = "" @optionparser = OptionParser.new{|o| o.on("--init", "initialize rurema"){ @command = :init @@ -25,17 +25,23 @@ } o.on("--server", "start web server"){ @command = :server } + o.on("--preview", + "render a reference as HTML"){ + @command = :preview + } + o.on("----"){} + o.on("--port=N", "port number of the web browser (only meaningful with --server)"){|n| @port = n.to_i } o.on("--browser", - "open web browser (only meaningful with --server)"){ + "open web browser (only meaningful with --server or --preview)"){ @open_browser = true } o.on("--dry-run", "show commands only"){ @dry_run = true @@ -46,10 +52,13 @@ } o.on("--rubyver=STR", "specify Ruby version (default: #{@rubyver})"){|str| @rubyver = str } + + o.on("-----"){} + o.on("--version", "show version of myrurema"){ puts MyRurema::VERSION exit } @@ -57,16 +66,14 @@ "show this message"){ puts o exit } } - @query, @num = @optionparser.parse(argv) - @query = "" if @query.nil? - @num = @num.to_i if @num + @rest_args = @optionparser.parse(argv) end attr_accessor :dry_run, :ruremadir, :rubyver, :open_browser - attr_accessor :command, :query, :num, :port + attr_accessor :command, :port, :rest_args def ruremadir=(dir) @ruremadir = Pathname(dir) end @@ -82,19 +89,22 @@ def initialize(opt=Options.new(ARGV)) @opt = opt end def run - case - when @opt.command + if @opt.command send(@opt.command) - when @opt.query.empty? - @opt.usage - when @opt.num - search_num(@opt.query, @opt.num, @opt.rubyver) else - search(@opt.query, @opt.rubyver) + query, num = *@opt.rest_args + case + when query && num + search_num(query, num.to_i, @opt.rubyver) + when query + search(query, @opt.rubyver) + else + @opt.usage + end end end def init sh "svn co -rHEAD #{SVN_URL}/doctree/trunk #{doctree_path}" @@ -142,10 +152,32 @@ cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open" sh "#{cmd} http://localhost:#{port}/view/" end th.join end + + TMP_FILE = Pathname(Dir.tmpdir)/'rurema_preview.html' + def preview + file, target = *@opt.rest_args + + if file + error "file not found: #{file}" unless File.exist?(file) + + result = sh "#{bitclust_path/'tools/bc-tohtml.rb'}" + + " #{file}" + + (target ? " --target=#{target}" : "") + + " --ruby=#{@opt.rubyver}" + + " > #{TMP_FILE}" + + if result && @opt.open_browser + cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open" + sh "#{cmd} #{TMP_FILE}" + end + else + sh "cd #{doctree_path/'refm/api/src'}" + end + end private def default_port(ver) "10" + ver.scan(/\d/).join @@ -199,7 +231,8 @@ system cmd unless @opt.dry_run end def error(msg) $stderr.puts msg + exit end end