rdparser.rb

Rubyで書いてるサイト管理システムからスピンアウト。
RDtoolのライブラリを利用して、RD->HTMLパーサを作る。
まさに人のふんどしによる相撲。

#depend installed RDtool.

require "cgi"
require "rd/rdfmt"
require "rd/visitor"
require "rd/version"
require "rd/rdvisitor"
require "rd/rd2html-lib"


#improve RDtool library, it makes only <body>-<body> content
#without <body> and </body>.
#like `hikidoc -f'
module RD
  class RD2HTMLVisitor < RDVisitor
    def apply_to_DocumentElement(element, content)
      html_body(content) + "\n"
    end
    
    def html_body(contents)
      content = contents.join("\n")
      foottext = make_foottext
      %Q|#{content}\n#{foottext}|
    end
    private :html_body
  end
end



module Worg
  class RDParser
    def initialize
      @visitor = RD::RD2HTMLVisitor.new
      @include_paths = []
    end
    attr_reader :include_paths
    
    def parse(src)
      src = src.split(/\n/).map{|x| x + "\n" }
      if src.find{|x| /\S/ === x } and !src.find{|x| /^=begin\b/ === x }
        src.unshift("=begin\n").push("=end\n")
      end
      tree = RD::RDTree.new(src, @include_paths, nil)
      tree.parse
      @visitor.visit(tree)
    end
  end
end



if __FILE__ == $0
  parser = Worg::RDParser.new
  print parser.parse(ARGF.read)
end