PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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