HyperSpec辞書引きLocal CGI

http://d.hatena.ne.jp/mitsygh/20070620/1182365173

id:mitsyghさんのエントリ。同じようなこと考える人はいるものだ。

EmacsだとSLIMEで存在する。HyperSpecのデータベースはelisp側が持っているので外部プログラム側がそれを使いたければgnudoit経由で取り出すしかない。

これはw3mのLocal CGIスクリプトhyperspec.cgi

#!/usr/bin/env ruby
## from mylib.rb
class << IO
  # Redirect stdout to STDOUT and executes the block.
  def redirect(stdout)
    begin
      stdout_sv = STDOUT.dup
      STDOUT.reopen(stdout)
      yield
    ensure
      STDOUT.flush
      STDOUT.reopen(stdout_sv)
    end
  end
end

require 'tmpdir'
require 'fileutils'
@@__system_to_string_count__ = 0
def system_to_string(*args)
  begin
    tmpf = File.join(Dir.tmpdir, "#{$$}-#{@@__system_to_string_count__}")
    @@__system_to_string_count__ += 1
    ret = nil
    open(tmpf,"w") do |f|
      IO.redirect(f) {
        system *args
      }
    end
    File.read(tmpf)
  ensure
    FileUtils.rm_f tmpf
  end
end

## /mylib.rb

func = ENV['QUERY_STRING'].sub(/hyperspec:/, '')

lisp = <<'XXX'
(let* (tmpurl
       (browse-url-browser-function (lambda (x &rest args) (setq tmpurl x))))
  (common-lisp-hyperspec "%s")
  tmpurl)
XXX

url = system_to_string "gnudoit", lisp % func
print "Location: #{url}\r\n\r\n"