-
Notifications
You must be signed in to change notification settings - Fork 10
/
create_ncl_completions.rb
executable file
·83 lines (72 loc) · 2.71 KB
/
create_ncl_completions.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
#!/usr/bin/env ruby -wU
if File.exist? 'NCL.sublime-completions'
print "[Warning]: NCL.sublime-completions exists, deleting it!\n"
File.delete 'NCL.sublime-completions'
end
completion_file = File.new 'NCL.sublime-completions', 'w+'
completion_file << "{\n"
completion_file << " \"scope\": \"source.ncl\",\n"
completion_file << " \"completions\":\n"
completion_file << " [\n"
url_prefix = 'http://www.ncl.ucar.edu'
print "[Notice]: Grabing function definitions from NCL website.\n"
page1 = `curl -s #{url_prefix}/Document/Functions/list_alpha.shtml`
categories = ['Functions/Built-in', 'Functions/Contributed', 'Functions/User_contributed', 'Functions/WRF_arw', 'Graphics/Interfaces']
categories.each do |category|
page1.scan(/^\s*<a href="\/Document\/#{category}\/\w*\.shtml/).each do |x|
func = x.match(/(\w+)(\.shtml)/)[1]
func_url = "#{url_prefix}#{x.match(/<a href="(.*)/)[1]}"
page2 = `curl -s #{func_url}`
puts "[Notice]: Creating completion for #{func}."
completion_file << " { \"trigger\": \"#{func}\", \"contents\": \"#{func}("
begin
prototype = page2.match(/(function|procedure) \w+ \(([^\)]*)\)$/m)[2].strip
rescue
print page2.match(/(function|procedure) \w+ \(([^\)]*)\)$/m)
print "[Error]: Failed to extract prototype for #{func}!"
# exit
next
end
i = 1
prototype.each_line do |line|
arg = line.split(" ")[0].strip
completion_file << "${#{i}:#{arg}}"
completion_file << ", " if i != prototype.lines.count
i += 1
end
completion_file << ")\" },\n"
end
end
print "[Notice]: Grabing graphics resources from NCL website.\n"
page1 = `curl -s #{url_prefix}/Document/Graphics/Resources/list_alpha_res.shtml`
resources = [] # There may be duplicate links in NCL graphics resources webpage.
page1.scan(/^<a name="\w+"><\/a><strong>/).each do |x|
res = x.match(/"(\w+)"></)[1]
# Also remove the trailing '_*' stuff.
res.gsub!(/_\w+/, '')
if not resources.include? res
completion_file << " \"#{res}\",\n"
resources << res
end
end
print "[Notice]: Grabing resource codes from NCL website.\n"
codes = []
page1.scan(/<code>\w+<\/code>/).each do |x|
code = x.match(/>(\w+)</)[1]
if not codes.include? code
completion_file << " \"#{code}\",\n"
codes << code
end
end
print "[Notice]: Grabing color tables from NCL website.\n"
page1 = `curl -s #{url_prefix}/Document/Graphics/color_table_gallery.shtml`
color_tables = []
page1.scan(/^<td>\w+<br>$/).each do |x|
color_table = x.match(/>(\w+)</)[1]
if not color_tables.include? color_table
completion_file << " \"#{color_table}\",\n"
color_tables << color_table
end
end
completion_file.write(" ]\n")
completion_file.write("}\n")