-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
126 lines (110 loc) · 3.04 KB
/
Rakefile
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
require 'sqlite3'
require 'nokogiri'
FILE_NAME = "reference-1.4.0.html"
DOC_URL = "http://leafletjs.com/#{FILE_NAME}"
task :default => [
:clear_existing_docs,
:fetch_docs,
:remove_annoying_parts_from_docs,
:fetch_icon,
:index,
:add_info_plist,
:tar
]
task :clear_existing_docs do
FileUtils.rm_rf(Dir.getwd + '/dist')
end
task :fetch_docs do
target_dir = Dir.getwd + '/dist/leaflet.docset/Contents/Resources/Documents'
system "wget --convert-links --page-requisites --directory-prefix=#{target_dir} #{DOC_URL}"
end
task :remove_annoying_parts_from_docs do
doc_to_modify = doc.clone
doc_to_modify.css('.ext-links').remove
doc_to_modify.css('.nav').remove
doc_to_modify.css('#toc-copy').remove
doc_to_modify.xpath('//script[@src="docs/js/docs.js"]').remove # no TOC script
File.open html_file_path, 'w' do |f|
f.puts doc_to_modify.to_html
end
end
task :fetch_icon do
target_file = Dir.getwd + '/dist/leaflet.docset/icon.png'
system "wget http://leafletjs.com/docs/images/favicon.ico -O #{target_file}"
end
task :index do
db_path = Dir.getwd + '/dist/leaflet.docset/Contents/Resources/docSet.dsidx'
db = SQLite3::Database.new db_path
create_docset_table(db)
parse_doc_into_db(doc, db)
end
task :add_info_plist do
info_plist = Dir.getwd + '/Info.plist'
FileUtils.cp info_plist, docset_contents_path
end
task :tar do
system "cd dist && tar --exclude='.DS_Store' -cvzf Leaflet.tgz leaflet.docset"
end
private
def docset_contents_path
Dir.getwd + '/dist/leaflet.docset/Contents/'
end
def html_file_path
docset_contents_path + "Resources/Documents/leafletjs.com/#{FILE_NAME}"
end
def open_html_file
file ||= File.open(html_file_path)
end
def doc
doc = Nokogiri::HTML(open_html_file)
open_html_file.close
doc
end
def create_docset_table(db)
db.execute <<-SQL
CREATE TABLE IF NOT EXISTS searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);
CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);
SQL
end
def parse_doc_into_db(doc, db)
file_name = "leafletjs.com/#{FILE_NAME}"
doc.css('h2').each do |heading2|
name = heading2.content
type = determine_type(heading2)
path = "#{file_name}##{heading2.attr('id')}"
db.execute <<-SQL
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('#{name}', '#{type}', '#{path}');
SQL
end
end
def determine_type(heading)
# NOTE: This mapping is somewhat arbitrary; it could use improvement.
case heading
when /Event methods/
'Method'
when /Event objects/
'Object'
when /L.Browser/
'Namespace'
when /Util/
'Function'
when /L.DomEvent/
'Function'
when /IHandler/
'Interface'
when /ILayer/
'Interface'
when /IControl/
'Interface'
when /IProjection/
'Object'
when /Global Switches/
'Property'
when /L.noConflict()/
'Method'
when /L.version/
'Constant'
else
'Class'
end
end