-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerator.rb
executable file
·263 lines (243 loc) · 6.49 KB
/
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
require 'rubygems'
require 'json'
require 'pp'
fileout = ARGV[0] || 'docs/methods.html'
fileout_html = File.open(fileout, 'r').read if File.exists?(fileout)
@modules = {}
def get_property(prop, s, multiline = false)
if multiline
r = Regexp.new('@'+prop.to_s+'[\s*]+\*\n(.+)\*\n', Regexp::MULTILINE)
match = s.scan(r)
match[0][0].split(/\n/) if match[0]
else
match = s.match(Regexp.new('@'+prop.to_s+' (.*)'))
match[1] if match
end
end
def get_html_parameters(str)
return nil if !str
str.gsub!(/<(.+?)>/, '<span class="monospace required parameter">\1</span>')
str.gsub!(/\[(.+?)\]/, '<span class="monospace optional parameter">\1</span>')
str.gsub!(/%(.+?)%/, '<span class="code">\1</span>')
end
def get_method(s)
raw = get_property(:method, s)
match = raw.match(/(.+\.)?(.+)\((.+)?\)/)
method = { :name => match[2] }
if !!match[1]
method[:class_method] = true
end
params = []
accepts_unlimited_params = false
if match[3]
p = match[3].split(/,(?!')/)
if p.last =~ /\.\.\./
p.delete_at(-1)
method[:accepts_unlimited_params] = true
end
params = p.to_enum(:each_with_index).map do |f,i|
required = !!f.match(/<.+>/)
name = f.match(/[<\[](.+)[>\]]/)[1]
default = f.match(/ = (.+)/)
css = ''
css << 'required ' if required
css << 'parameter'
if default
d = default[1]
if d =~ /['"].*['"]/
type = :string
d.gsub!(/(['"])(.+)(['"])/, '\\1<span class="code">\\2</span>\\3')
elsif d =~ /\d+/
type = :number
elsif d =~ /\/.+\//
type = :regexp
elsif d =~ /^null$/
type = :null
elsif d =~ /true|false/
type = :boolean
elsif d =~ /\{\}/
type = :object
end
d = '<span class="' + type.to_s + ' code value">' + d + '</span>'
end
{
:name => name,
:type => type,
:required => required,
:default => default ? default[1] : nil
}
end
else
params = []
end
method[:params] = params
method
end
def get_examples(s, name)
lines = get_property(:example, s, true)
return nil if !lines
examples = []
func = ''
force_result = false
lines.each do |l|
l.gsub!(/^[\s*]+/, '')
l.gsub!(/\s+->.+$/, '')
if l =~ /^\s*\+/
force_result = true
l.gsub!(/\+/, '')
end
if l =~ /function/ && l !~ /isFunction/
func << l
elsif l =~ /^[^\(]+\);$/
func << "\n" + l.gsub(/\s+->.+$/, '')
examples << { :multi_line => true, :force_result => force_result, :html => func }
func = ''
elsif func.length > 0
func << "\n" + l
elsif !l.empty?
examples << {
:multi_line => false,
:force_result => force_result,
:html => l.gsub(/\s+->.+$/, '').gsub(/\\n/, '_NL_')
}
end
end
examples.each do |ex|
clean(ex)
end
examples
end
def clean(m)
m.each do |key, value|
if value.nil? || value == false
m.delete(key)
end
end
end
def extract_docs(package)
@modules = {
:apiconnect => []
}
@current_module = @modules[:apiconnect]
File.open("lib/apiconnect.js", 'r') do |f|
i = 0
pos = 0
f.read.scan(/\*\*\*.+?(?:\*\*\*\/|(?=\*\*\*))/m) do |b|
if mod = b.match(/(\w+) module/)
name = mod[1]
if !@modules[name]
@modules[name] = []
end
@current_module = @modules[name]
@current_module_name = name
else
method = get_method(b)
method[:package] = package
method[:returns] = get_property(:returns, b)
method[:short] = get_property(:short, b)
method[:set] = get_property(:set, b)
method[:extra] = get_property(:extra, b)
method[:examples] = get_examples(b, method[:name])
method[:alias] = get_property(:alias, b)
method[:module] = @current_module_name
get_html_parameters(method[:short])
get_html_parameters(method[:extra])
@current_module << method
if method[:alias]
method.delete_if { |k,v| v.nil? || (v.is_a?(Array) && v.empty?) }
method[:short] = "Alias for <span class=\"code\">#{method[:alias]}</span>."
end
if method[:set]
method[:pos] = pos
pos += 1
else
pos = 0
end
if method[:name] =~ /\[/
method[:set_base] = method[:name].gsub(/\w*\[(\w+?)\]\w*/, '\1')
method[:name].gsub!(/[\[\]]/, '')
end
clean(method)
end
end
end
end
#puts pp @modules
extract_docs(:core)
@modules.each do |name, mod|
mod.sort! do |a,b|
if a[:class_method] == b[:class_method]
a[:name] <=> b[:name]
else
a[:class_method] ? -1 : 1
end
end
end
def examples_html(examples)
return '' if examples.nil?
tmp = []
arr = []
examples.each do |example|
if example[:multi_line]
count = 0
match = example[:html].scan(/\n/)
tmp << example[:html].gsub(/\n/) do
space = count == match.length - 1 ? '' : ' '
count += 1
"\n" + space
end
else
arr << tmp.join("\n") if tmp.length > 0
arr << example[:html]
tmp = []
end
end
arr << tmp.join("\n") if tmp.length > 0
html = arr.map do |el|
%Q(<p class="single_code_example">#{el.strip}</p>)
end.join("\n")
%Q(<div class="code_example">#{html}</div>)
end
def method_html(method)
args = method[:params].map do |p|
if p[:required]
%Q(<span class="monospace required parameter" title="Required argument">#{p[:name]}</span>)
else
%Q(<span class="monospace optional parameter" title="Optional argument">#{p[:name]}</span>)
end
end.join(', ')
name = %Q(#{method[:name]}<span class="arguments">(#{args})</span>)
<<-HTML
<div class="method" id="method_#{method[:name]}">
<h3 class="monospace name">#{name}</h3>
<p class="description">
<span class="short">#{method[:short]}</span>
<span class="extra">#{method[:extra]}</span>
</p>
<p class="returns">
<span class="label">Returns:</span>
<span class="value">#{method[:returns]}</span>
</p>
#{examples_html(method[:examples])}
</div>
HTML
end
def legend_html
legend = @modules[:apiconnect].map do |method|
%Q(<li><a href="#method_#{method[:name]}">#{method[:name]}</a></li>)
end.join("\n")
<<-HTML
<div id="legend">
<ul>
<li><a href="#constructor">Constructor</a></li>
#{legend}
</ul>
</div>
HTML
end
File.open(fileout, 'w') do |f|
f.puts legend_html
@modules[:apiconnect].each do |method|
f.puts method_html(method)
end
end