Skip to content

Commit

Permalink
added --preivew
Browse files Browse the repository at this point in the history
  • Loading branch information
yhara committed Sep 1, 2010
1 parent ac2aad7 commit 148e594
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
29 changes: 28 additions & 1 deletion README.mkd
Expand Up @@ -87,8 +87,35 @@ Rubyリファレンス刷新計画では、最新のRubyに完全対応したリ

$ rurema --update

リファレンスの書き方
====================

myruremaは、リファレンスを書くための機能も少し備えています。

リファレンスの原稿は、~/.rurema/doctree/refm/api/src/ 以下に置かれています。

### プレビュー

--previewオプションを指定すると、特定のメソッドをHTMLファイルにコンパイル
します。

rurema --preview _builtin/Array Array#pop

コンパイル結果は/tmp/rurema_preview.htmlに保存されます。
--browserオプションを指定すると、コンパイル結果をブラウザで開きます。

rurema --preview _builtin/Array Array#pop --browser

うまく書けたら、svn diffコマンドで差分を表示し、
るりまの[Issue Tracker](http://redmine.ruby-lang.org/projects/rurema/issues)
にチケットを作って、パッチを貼り付けてください。
内容が問題なければ、るりまコミッターがコミットしてくれます。

パッチがたくさん採用されれば、そのうちに、あなたもるりまコミッターの
一員に勧誘されるでしょう。 :-)

その他
------
======

ライセンスはbitclustのものに準じます。

Expand Down
17 changes: 17 additions & 0 deletions spec/commands.rb
Expand Up @@ -45,6 +45,23 @@ def sh(cmd, opt={})
%r{standalone},
%r{(start|open) http://localhost:}
],

# "rurema --edit" => [
# %r{cd .*doctree/refm/api/src},
# ],

"rurema --preview _builtin/Array" => [
%r{bc-tohtml.*_builtin/Array .*rurema_preview.html},
],

"rurema --preview _builtin/Array Array#pop" => [
%r{bc-tohtml.*_builtin/Array --target=Array#pop.*rurema_preview.html},
],

"rurema --preview Array --browser" => [
%r{bc-tohtml},
%r{(start|open) .*rurema_preview.html}
],
}

describe MyRurema do
Expand Down
52 changes: 39 additions & 13 deletions src/myrurema.rb
@@ -1,6 +1,7 @@
require 'optparse'
require 'pathname'
require 'shellwords'
require 'tmpdir'

class Pathname; alias / +; end

Expand All @@ -12,7 +13,6 @@ def initialize(argv)
@dry_run = false
@ruremadir = Pathname("~/.rurema").expand_path
@rubyver = RUBY_VERSION
@query = ""

@optionparser = OptionParser.new{|o|
o.on("--init",
Expand All @@ -27,13 +27,17 @@ def initialize(argv)
"start web server"){
@command = :server
}
o.on("--preview",
"render a reference as HTML"){
@command = :preview
}

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",
Expand All @@ -59,12 +63,10 @@ def initialize(argv)
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)
Expand All @@ -84,15 +86,18 @@ def initialize(opt=Options.new(ARGV))
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

Expand Down Expand Up @@ -144,6 +149,26 @@ def server
end
th.join
end

TMP_FILE = Pathname(Dir.tmpdir)/'rurema_preview.html'
def preview
file, target = *@opt.rest_args

unless File.exist?(file)
error "file not found: #{file}"
end

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
end

private

Expand Down Expand Up @@ -201,5 +226,6 @@ def sh(cmd, opt={})

def error(msg)
$stderr.puts msg
exit
end
end

0 comments on commit 148e594

Please sign in to comment.