-
Notifications
You must be signed in to change notification settings - Fork 2
/
dictionary_generator.rb
executable file
·130 lines (108 loc) · 2.81 KB
/
dictionary_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'erb'
require 'rubygems'
require 'rdoc/rdoc'
require 'rdoc/generators/ri_generator'
@indent = ''
def get_template(file)
erb = ''
File.open("./templates/#{file}", 'r') { |f| erb = f.read }
ERB.new(erb)
end
def object_id(obj)
obj.full_name.downcase.gsub('::', '_')
end
def method_id(mthd)
mthd.full_name.gsub('::', '_').gsub('#', '-').gsub('<', '-lt-').gsub('&', '-amp-')
end
class_template = get_template('class.erb')
def render_class(cls)
tpl = get_template('class.erb')
@class = cls
@description = @reader.get_class(cls)
tpl.result(binding)
end
def render_method(mthd)
tpl = get_template('method.erb')
@method = mthd
@description = @reader.get_method(mthd)
tpl.result(binding)
end
def render_comment(item, prefix = @indent)
case item
when Array
item.collect{ |c| render_comment(c) }.join
when SM::Flow::P
"<p>#{item.body}</p>"
when SM::Flow::LI
"<li>#{item.body}</li>"
when SM::Flow::LIST
render_list(item)
when SM::Flow::VERB
"<pre>#{item.body}</pre>"
when SM::Flow::H
"<h#{item.level}>#{item.text}</h#{item.level}>"
when SM::Flow::RULE
"<hr/>"
else
end
end
def render_list(list)
case list.type
when SM::ListBase::BULLET
prefixer = proc { |ignored| @indent + "* " }
when SM::ListBase::NUMBER, SM::ListBase::UPPERALPHA, SM::ListBase::LOWERALPHA
start = case list.type
when SM::ListBase::NUMBER then 1
when SM::ListBase::UPPERALPHA then 'A'
when SM::ListBase::LOWERALPHA then 'a'
end
prefixer = proc do |ignored|
res = @indent + "#{start}.".ljust(4)
start = start.succ
res
end
when SM::ListBase::LABELED
prefixer = proc do |li|
li.label
end
when SM::ListBase::NOTE
longest = 0
list.contents.each do |item|
if item.kind_of?(SM::Flow::LI) && item.label.length > longest
longest = item.label.length
end
end
prefixer = proc do |li|
@indent + li.label.ljust(longest+1)
end
else
end
out = '<ul>'
list.contents.each do |item|
if item.kind_of? SM::Flow::LI
prefix = prefixer.call(item)
out << render_comment(item)
else
out << render_comment(item)
end
end
out << '</ul>'
out
end
puts "Building XML files from sources"
cache = RI::RiCache.new('./doc/rdoc')
@reader = RI::RiReader.new(cache)
@classes = Array.new
@methods = Array.new
def find_all_classes(parent)
parent.each do |com|
@classes << render_class(com)
com.methods_matching('', nil).each do |mthd|
@methods << render_method(mthd)
end
find_all_classes(com.classes_and_modules)
end
end
find_all_classes(cache.toplevel.classes_and_modules)
File.open('./Rails23.xml', 'w') { |file| file.puts get_template('dictionary.erb').result(binding) }
puts "Dictionary XML file generation complete"