PageRenderTime 101ms CodeModel.GetById 20ms app.highlight 50ms RepoModel.GetById 23ms app.codeStats 1ms

/bundles/plugins-trunk/RubyPlugin/ri/rdoc_to_java.rb

#
Ruby | 198 lines | 166 code | 31 blank | 1 comment | 20 complexity | be9af5a8e44e2937c154b5af3929c7ed MD5 | raw file
  1#!/usr/local/bin/ruby
  2require 'yaml'
  3require 'erb'
  4require 'rexml/document'
  5require 'rdoc/ri/ri_descriptions'
  6require 'rdoc/markup/simple_markup/to_flow'
  7
  8include RI
  9
 10class JavaXmlSerializer
 11
 12  def initialize(template_dir, result_dir)
 13    template_dir = template_dir + '/' if template_dir[template_dir.size-1] != '/'
 14    result_dir = result_dir + '/' if result_dir[result_dir.size-1] != '/'
 15
 16    @template_dir = template_dir
 17    @result_dir = result_dir
 18  end
 19
 20  def convert_dir(base_dir, dir)
 21    @base_dir = base_dir
 22    dir = dir.gsub(%[\\], %[/])
 23    Struct.new("File", :type, :path, :name, :dir)
 24    list = find_class_descriptions(dir)
 25    list.each do |file|
 26      convert(file)
 27    end
 28  end
 29
 30  def find_class_descriptions(dir)
 31    list = Array.new
 32
 33    Dir.foreach(@base_dir + dir) do |entry|
 34      if entry != "." and entry != ".."
 35        path = dir + "/" + entry
 36        if File.directory? @base_dir + path
 37          list << find_class_descriptions(path)
 38        elsif entry =~ /(cdesc-)(.*)(\.yaml)/
 39          list << Struct::File.new($2, @base_dir + path, entry, dir)
 40        end
 41      end
 42    end
 43
 44    list.flatten!
 45    list
 46  end
 47
 48  def convert(file)
 49    d = YAML.load_file(file.path)
 50    d.namespace = get_namespace(file.dir)
 51    d = add_method_descriptions(@base_dir + file.dir, d)
 52    puts file.name
 53    erb = ERB.new(IO.read(@template_dir + "cdesc.erb"))
 54    result = erb.result(binding)
 55    result_dir = create_dirs(file)
 56
 57    File.open(%Q[#{result_dir}/#{file.type}.xml], "w") do |file|
 58      file.puts result
 59    end
 60  end
 61
 62  def normalize(text)
 63    text.gsub!('&lt;', '|lt;')
 64    REXML::Text.normalize(text)
 65  end
 66
 67  def get_method_description(description, dir, method, type)
 68    name = ''
 69    index = 0
 70    method.name.each_byte do |b|
 71      if b < 48 or (57 < b and b < 65) or (90 < b and b < 97 and b != 95) or 122 < b
 72        name << '%' + b.to_s(16)
 73      else
 74        name << method.name[index,1].to_s
 75      end
 76      index = index + 1
 77    end
 78
 79    d = YAML.load_file(%Q[#{dir}/#{name}-#{type}.yaml])
 80    d.html_comment = convert_comment(d.comment)
 81
 82    d.full_name = normalize(d.full_name)
 83    d.namespace = if description.namespace
 84                    description.namespace + "::" + description.name
 85                  else
 86                    description.name
 87                  end
 88    d.name = normalize(d.name)
 89    d.params = normalize(d.params) if d.params
 90    d.block_params = normalize(d.block_params) if d.block_params
 91    d.aliases.each do |a|
 92      a.name = normalize(a.name)
 93    end if d.aliases
 94
 95    d
 96  end
 97
 98  def convert_element(html,part)
 99      if part.instance_of? SM::Flow::P
100        html << %Q[<p>#{part.body}</p>]
101      elsif part.instance_of? SM::Flow::VERB
102        html << %Q[<pre>#{part.body}</pre>]
103      elsif part.instance_of? SM::Flow::H
104        level = part.level
105        text = part.text
106        html << %Q[<h#{level}>#{text}</h#{level}>]
107      elsif part.instance_of? SM::Flow::LI
108        html << %Q[<li>#{part.body}<li>]
109      elsif part.instance_of? SM::Flow::RULE
110        html << '<hr />'
111      else
112        html << '<ul>'
113        part.contents.each do |item|
114          convert_element(html,item)
115        end
116        html << '</ul>'
117      end
118  end
119
120  def convert_comment(comment)
121    html = ""
122    comment.each do |part|
123      convert_element(html,part)
124    end if comment
125    normalize(html)
126  end
127
128  def add_method_descriptions(dir, description)
129    description.i_methods = description.instance_methods.collect do |method|
130                              get_method_description(description, dir, method, 'i')
131                            end
132    description.c_methods = description.class_methods.collect do |method|
133                              get_method_description(description, dir, method, 'c')
134                            end
135    description.html_comment = convert_comment(description.comment)
136
137    description.attributes.each do |a|
138      a.html_comment = convert_comment(a.comment)
139    end if description.attributes
140
141    description.constants.each do |c|
142      c.html_comment = convert_comment(c.comment)
143      c.value = normalize(c.value).gsub('???','')
144    end if description.constants
145
146    description
147  end
148
149  def create_dir(dir)
150    if !File.exist? dir
151      Dir.mkdir(dir)
152    end
153  end
154
155  def create_dirs(file)
156    dir = @result_dir
157    create_dir(dir)
158    file.dir.each("/") do |part|
159      dir = dir + "/" + part
160      create_dir(dir)
161    end
162    dir
163  end
164
165  def get_namespace(dir)
166    parts = dir.split('/')
167    if parts.size > 3
168      parts = parts[2..(parts.size-2)]
169      return parts.join("::")
170    else
171      return nil
172    end
173  end
174
175end
176
177class NamedThing
178  attr_accessor :html_comment
179end
180
181class Description
182  attr_accessor :html_comment
183  attr_accessor :namespace
184end
185
186class ModuleDescription
187  attr_accessor :i_methods
188  attr_accessor :c_methods
189end
190
191class Constant
192  attr_writer :value
193end
194
195class AliasName
196  attr_writer :name
197end
198