This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
jsdoc.rb
288 lines (267 loc) · 8.83 KB
/
jsdoc.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/ruby
#
#
# Copyright (c) 2012 - present Adobe Systems Incorporated. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
#
# This script generates debugger documentation from Inspector.json
#
# Required Ruby Gems
# json date open-uri
require "rubygems"
require "json"
require "date"
require "open-uri"
#INSPECTOR_URL = "http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/inspector/Inspector.json"
INSPECTOR_URL = "Inspector.json"
OUTPUT = "inspector.html"
class NilClass
def empty?
true
end
def each
end
end
class String
def upcaseFirst
self[0, 1].upcase + self[1, self.length - 1]
end
end
class JSDoc
def typedef(domain, info)
return "[" + typedef(domain, info['items']) + "]" if info['items']
if info["$ref"]
ref = info["$ref"]
ref = domain + "." + ref unless ref =~ /\./
r = "<a href='##{ref}'>#{ref}</a>" if info["$ref"]
elsif info["enum"]
r = "( #{info['enum'].join " | "} )"
else
r = info["type"].upcaseFirst
end
r
end
def initialize(input, output)
@input = input
@output = output
end
def open
@in = JSON.parse(File.open(@input).read)
@in["domains"].sort! { |a,b| a["domain"] <=> b["domain"] }
@version = "#{@in['version']['major']}.#{@in['version']['minor']}"
File.open(@output, "w") do |out|
@out = out
yield
end
end
def run
open do
writeDocument do
writeTOC
@in["domains"].each { |domain| writeDomain domain }
end
end
end
def write(*args)
args.each { |line| @out.write line + "\n" }
end
def writeParams(domain, params, prefixLine = false)
return unless params
write prefixLine if prefixLine
write "<dl>"
params.each do |p|
name = p["name"]
description = " <span class='text'>#{p['description']}</span>" if p['description']
type = typedef domain, p
name += " (optional)" if p['optional']
write "<dt>#{name}</dt>"
write "<dd>#{type}#{description}</dd>"
end
write "</dl>"
end
def writeTOCLine(domain, info)
info["name"] ||= info["id"]
uid = "#{domain}.#{info['name']}"
description = info['description'] ? ": #{info['description'].gsub(/<[^>]*>/, "")}" : ""
write "<li><a href='##{uid}'>#{uid}</a>#{description}</li>"
end
def writeTOCDomain(info)
domain = info["domain"]
unless info["types"].empty?
write "<span class='label'>Type</span>", "<ul>"
info["types"].each { |p| writeTOCLine domain, p }
write "</ul>"
end
unless info["commands"].empty?
write "<span class='label label-success'>Command</span>", "<ul>"
info["commands"].each { |p| writeTOCLine domain, p }
write "</ul>"
end
unless info["events"].empty?
write "<span class='label label-info'>Event</span>", "<ul>"
info["events"].each { |p| writeTOCLine domain, p }
write "</ul>"
end
end
def writeDocument
@out.write <<-eos
<!DOCTYPE html>
<html>
<head>
<title>Inspector #{@version} Documentation</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-dropdown.js"></script>
<script>
$(function () {
var _domain = $(window.location.hash);
if (_domain.length === 0) _domain = $("#toc");
$(".domain").hide();
_domain.show();
$(".dropdown-toggle").dropdown();
$("a[href^=#]").click(function (e) {
e.preventDefault();
var domainAndName = e.currentTarget.hash.split(".");
window.location = domainAndName[0];
var symbol = $(domainAndName.join("_"));
var speed;
if (_domain !== domainAndName[0]) {
$(_domain).hide();
_domain = domainAndName[0];
$(_domain).show();
speed = 0;
} else {
speed = "fast";
}
$("html, body").animate({
scrollTop: symbol.offset().top - 70 + "px"
}, speed);
if (domainAndName.length > 1) symbol.effect("highlight", { color: "#ffc"}, 1000);
});
});
</script>
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<style>
body {padding: 70px 0 20px 0;}
h3, dl {font-family: Menlo, Monaco, "Courier New", monospace;}
dl {font-size: 12.025px;}
dl dd {margin-bottom: 6px;}
dl .text {margin-left:12px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;}
h3 {border-top: 1px solid #aaa; padding: 8px 0;}
h3 .label {float: left; margin: 6px;}
footer p {text-align: center; margin: 0;}
</style>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a href="#toc" class="brand">WebInspector v#{@version}</a>
<ul class="nav nav-pills">
<li class="dropdown" id="domain-menu">
<a class="dropdown-toggle" data-toggle="dropdown">Domains<b class="caret"></b></a>
<ul class="dropdown-menu">
eos
@in["domains"].each { |info| write "<li><a href='##{info['domain']}'>#{info['domain']}</a></li>" }
write "</ul>", "</li>", "</ul>", "</div>", "</div>", "</div>", "<div class='container'>"
yield
write "</div>", "<footer>"
write "<p>Generated from Inspector.json v#{@version} on #{DateTime.now.strftime '%F %T%z'}</p>"
write "<p>Implementation by <a href='mailto:[email protected]'>Jonathan Diehl</a></p>"
write "</footer>", "</body>", "</html>"
end
def writeTOC
write "<section id='toc' class='domain'>", "<h1>Table of Contents</h1>"
@in["domains"].each do |info|
domain = info["domain"]
write "<h3><a href=\"##{info['domain']}\">#{info['domain']}</a></h3>"
writeTOCDomain info
end
write "</section>"
end
def writeDomain(info)
domain, description = info["domain"], info["description"]
types, commands, events = info["types"], info["commands"], info["events"]
write "<section id='#{domain}' class='domain'>"
write "<h1>#{domain}</h1>"
write "<p>#{description}</p>"
writeTOCDomain info
if types
types.each { |info| writeType domain, info }
end
if commands
commands.each { |info| writeCommand domain, info }
end
if events
events.each { |info| writeEvent domain, info }
end
write "</section>"
end
def writeType(domain, info)
name, description = info["id"], info["description"]
type, enum, properties = info["type"], info["enum"], info["properties"]
write "<div id='#{domain}_#{name}' class='type'>"
write "<h3>#{domain}.#{name} <span class='label'>Type</span></h3>"
write "<p>#{description}</p>" if description
if type != "object"
write "<dl>"
write "<dd>#{typedef domain, info}</dd>"
write "</dl>"
end
writeParams domain, properties
write "</div>"
end
def writeCommand(domain, info)
name, description = info["name"], info["description"]
parameters, returns = info["parameters"], info["returns"]
write "<div id='#{domain}_#{name}' class='command'>"
write "<h3>#{domain}.#{name} <span class='label label-success'>Command</span></h3>"
write "<p>#{description}</p>" if description
writeParams domain, parameters
writeParams domain, returns, "<h4>Callback Parameters:</h4>"
write "<h4>Code Example:</h4>", "<pre>"
paramNames = parameters.collect { |p| p["name"] } if parameters
paramNames ||= []
if returns
returnNames = returns.collect { |p| p["name"] }
paramNames << "function callback(res) {\n\t// res = {#{returnNames.join ", "}}\n}"
end
write "// WebInspector Command: #{domain}.#{name}"
write "#{domain}.#{name}(#{paramNames.join ", "});"
write "</pre>", "</div>"
end
def writeEvent(domain, info)
name, description = info["name"], info["description"]
parameters = info["parameters"]
write "<div id='#{domain}_#{name}' class='command'>"
write "<h3>#{domain}.#{name} <span class='label label-info'>Event</span></h3>"
write "<p>#{description}</p>" if description
writeParams domain, parameters
write "<h4>Code Example:</h4>", "<pre>"
paramNames = parameters.collect { |p| p["name"] } if parameters
paramNames ||= []
write "// WebInspector Event: #{domain}.#{name}"
write "function on#{name.upcaseFirst}(res) {\n\t// res = {#{paramNames.join ", "}}\n}"
write "</pre>", "</div>"
end
end
jsdoc = JSDoc.new INSPECTOR_URL, OUTPUT
jsdoc.run