-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.rb
137 lines (120 loc) · 3.84 KB
/
api.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
#!/usr/bin/ruby
require 'nokogiri'
require 'open-uri'
require 'json'
require 'time'
TProperty=Struct.new(:type, :name)
TParameter=Struct.new(:type, :name)
TType=Struct.new(:name, :props, :link)
TMethod=Struct.new(:fullname, :params, :return, :link, :doc)
TEvent=Struct.new(:fullname, :link)
def url(path)
'https://developer.chrome.com/extensions/' + path
end
index=Nokogiri(open(url('api_index')))
stables=index.xpath('//h2[@id="stable_apis"]')[0].next_element.next_element.xpath("./tr/td[1]/a/@href").map(&:text)
version=index.xpath('//h2[@id="stable_apis"]')[0].next_element.next_element.xpath("./tr/td[3]").map(&:text).map(&:to_i).sort.last
puts "Chrome version: #{version}"
defs=stables.map do |name|
#defs=stables.grep(/alarms/).map do |name|
pkgurl=url(name)
pkg=Nokogiri(open(pkgurl))
caution=pkg.css('p.caution')
if not caution.empty? and caution.map(&:text).join.include?("This API works only on Chrome OS")
puts caution.map(&:text)
puts "#{name} skip"
next
end
pkg.css("div.api-reference").xpath('./div').map do |api|
h3=api.xpath('./h3')[0]
id=h3.attributes['id'].text
link="#{pkgurl}##{id}"
case id
# when /^type-.*?Type/
# # ignore enum
when /^type-/
typename=h3.text.chomp
props=api.xpath("./table/tr[starts-with(@id, 'property-#{typename}-')]").map {|prop| prop.xpath('./td').map(&:text).map(&:strip).map{|s|s.gsub(/[\n\t ]{2,}/,' ')}}.map{|s| TProperty.new(s[0], s[1])}
TType.new(typename, props, link)
when /^method-/
summary=api.xpath("./div[@class='summary']")[0].text.strip.sub(/\(.*\)/, '').split(' ')
ret=summary.length==2 ? summary[0] : nil
fullname=summary[-1].split('.')
description=api.xpath("./div[@class='description']/p").map(&:text).map(&:strip).join.sub(/\A([^.]+\.).*/m, "\\1")
params=api.xpath("./div/table/tr[starts-with(@id, 'property-#{fullname[-1]}-')]").map {|prop| prop.xpath('./td').map(&:text).map(&:strip).map{|s|s.gsub(/[\n\t ]{2,}/,' ').sub(/\A([^.]+\.).*/m, "\\1")}}.map{|s| TParameter.new(s[0], s[1])}
TMethod.new(fullname, params, ret, link, description)
when /^event-/
fullname=api.css("div.summary").text.strip.sub(/\(.*\)/, '').split('.')
TEvent.new(fullname, link)
else
raise "unknown #{id} #{link}"
end
end
end.flatten
def _name(n)
if /\A\(optional\) (.+)\z/ =~ n
"#{$1}?"
else
n
end
end
def _type(t)
case t
when "double"
"number"
when "object", / or /, "any"
"?"
when "Window"
"window"
when /array of (\w+)/
"[#{_type($1)}]"
when TMethod
args=t.params.map{|pr|_name(pr.name)+": "+_type(pr.type)}.join(', ')
"fn(#{args})" +(t.return ? " -> #{_type(t.return)}":"")
else
t
end
end
r={
"!name" => "chrome-extension",
"!define" => {},
"chrome" => {}
}
defs.select{|d| d.is_a?(TType)}.each do |d|
r['!define'][d.name] = d.props.map{|pr|
{
pr.name.sub(/\A\(optional\) (.+)\z/,"\\1") => {
"!type" => _type(pr.type),
"!doc" => pr.name
}
}
}
end
defs.select{|d| d.is_a?(TMethod)}.each do |d|
m=d.fullname.inject(r){|r,n| r[n]||={}}
m["!type"]=_type(d)
m["!url"]=d.link
m["!doc"]=d.doc
end
defs.select{|d| d.is_a?(TEvent)}.each do |d|
m=d.fullname.inject(r){|r,n| r[n]||={}}
m["!type"]="fn(callback: ?)"
m["!url"]=d.link
end
open("chrome-extension.js","w"){|f|
f.write(DATA.read.sub(/DEFS/, JSON.generate(r,indent:'')))
}
__END__
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
return mod(require("tern/lib/infer"), require("tern/lib/tern"));
if (typeof define == "function" && define.amd) // AMD
return define([ "tern/lib/infer", "tern/lib/tern" ], mod);
mod(tern, tern);
})(function(infer, tern) {
"use strict";
tern.registerPlugin("chrome-extension", function(server, options) {
return { defs : defs };
});
var defs = DEFS;
});