-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRakefile
More file actions
341 lines (281 loc) · 9.48 KB
/
Rakefile
File metadata and controls
341 lines (281 loc) · 9.48 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
require 'bundler/setup'
require 'shellwords'
require 'json'
require 'uri'
Bundler.require :default
docset_root = "dist/Ionic.docset"
resources_path = "#{docset_root}/Contents/Resources"
class Generator
class Link
MAPPING = {
"service" => 'Service',
"directive" => 'Directive',
"controller" => 'Object',
"provider" => 'Provider',
"utility" => 'Global',
"object" => 'Constant',
"page" => 'Component'
}
attr_accessor :href, :text
def initialize(href, text, type=nil)
@href = href
@text = text
@type = type unless type.nil?
end
def type
return @type if @type
type = href.match(/^.*?\/api\/(.*?)\/.*/)
@type = MAPPING[type[1]] if type
end
end
def initialize(database_path, file_path, section_selector, link_selector, default_type=nil)
@file_path = file_path
@page = Nokogiri::HTML(open(file_path))
@section_selector = section_selector
@link_selector = link_selector
@default_type = default_type
@db = SQLite3::Database.new database_path
setup_database
end
def setup_database
rows = @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);
CREATE UNIQUE INDEX IF NOT EXISTS anchor ON searchIndex (name, type, path);
SQL
end
def parse
categories = collect_links(@section_selector, "Guide")
links = collect_links(@link_selector, @default_type)
link_collection = categories + links
link_collection.each { |link| create_entry(link.text, link.type, link.href) }
end
def create_css_entry
create_entry("Stylesheets", "Section", "docs/components/index.html")
end
def build_css_tree
page = Nokogiri::HTML(open("compiled/docs/components/index.html"))
sections = page.css("section.docs-section")
sections.each do |node|
main_section = node.css("> h2").first
unless main_section.nil?
add_docset_link(page, main_section, main_section.text)
end
section = node.css("h3.title").first
unless section.nil?
add_docset_link(page, section, section.text)
end
end
File.open("compiled/docs/components/index.html", "w") do |f|
f.write(page.to_html)
end
end
private
def add_docset_link(page, node, title)
docset_link = Nokogiri::XML::Node.new "a", page
docset_link["class"] = "dashAnchor"
docset_link["name"] = "//apple_ref/cpp/Section/#{URI.escape(title)}"
node.add_next_sibling(docset_link)
end
def create_entry(name, type, path)
return if /\/?#/.match(path)
path.gsub!("/#", "/index.html#")
path.gsub!(/^\//, '')
@db.execute "INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ( ?, ?, ?)", [name, type, URI.unescape(path)]
end
def collect_links(selector, type=nil)
links = @page.css(selector)
links.inject([]) do |collection, link_node|
text = link_node.text.strip
href = link_node.attributes["href"].value
collection << Link.new(href, text, type)
collection
end
end
end
class PathFix
def initialize(root_path)
@root_path = root_path
end
def make_paths_relative
paths = Dir["#{@root_path}/**/*.html"]
root_layers = @root_path.split("/").size
paths.each do |path|
layers = path.split("/").size - root_layers - 1
content = File.read(path)
content.gsub!(/href="\/\//, "href=\"http://")
content.gsub!(/src="\/\//, "src=\"http://")
content.gsub!(/value="\/\//, "value=\"http://")
content.gsub!(/href="\//, "href=\"#{"../" * layers}")
content.gsub!(/src="\//, "src=\"#{"../" * layers}")
content.gsub!(/value="\//, "value=\"#{"../" * layers}")
content.gsub!(/href=".*?\/#/, "href=\"#")
File.open(path, "w+") do |f|
f.write content
end
end
end
def make_paths_relative_for_root_path(prefix="")
content = File.read(@root_path)
content.gsub!('href="/', "href=\"#{prefix}")
content.gsub!('src="/', "src=\"#{prefix}")
content.gsub!('value="/', "value=\"#{prefix}")
content.gsub!("url('/", "url('#{prefix}")
content.gsub!(/url\(\/(.*?)\)/, "url('#{prefix}" + '\1' + "')")
content.gsub!('url("/', "url(\"#{prefix}")
File.open(@root_path, "w+") do |f|
f.write content
end
end
def complete_folder_references
paths = Dir["#{@root_path}/**/*.html"]
paths.each do |path|
content = File.read(path)
content.gsub!(/\/"/, "\/index.html\"")
File.open(path, "w+") do |f|
f.write content
end
end
end
def remove_codepen_references
paths = Dir["#{@root_path}/**/*.html"]
paths.each do |path|
page = Nokogiri::HTML(open(path))
page.css(".phone-case").remove
File.open(path, "w+") do |f|
f.write page.to_html
end
end
end
def cdn_to_statics(mapping)
paths = Dir["#{@root_path}/**/*.html"]
root_layers = @root_path.split("/").size
mapping.each_pair do |cdn_path, static_path|
paths.each do |path|
layers = path.split("/").size - root_layers - 1
content = File.read(path)
content.gsub!(cdn_path, "#{"../" * layers}#{static_path}")
File.open(path, "w+") do |f|
f.write content
end
end
end
end
end
namespace :docset do
task :generate => [ :cleanup,
:update_ionic_site,
:compile_ionic_site,
:create,
:complete_folder_references,
:remove_codepen_references,
:generate_docset_database,
:fix_href_in_main_index,
:copy_ionic_docs,
:make_paths_relative,
:cdn_to_static,
:fix_site_js,
:copy_static_files
]
desc "cleanup"
task :cleanup do
FileUtils.rm_rf("./compiled")
FileUtils.rm_rf("./dist")
end
desc "checkout ionic-site"
task :update_ionic_site do
remote_repo = "https://github.com/driftyco/ionic-site.git"
if File.exists?("./src")
FileUtils.cd('./src') do
`git pull`
end
else
`git clone --depth=1 #{remote_repo} ./src`
end
end
desc "compile with jekyll"
task :compile_ionic_site do
`jekyll build --layouts ./src/_layouts --source ./src --destination ./compiled`
end
desc "creates the docset"
task :create do
FileUtils.mkdir_p("#{resources_path}/Documents")
end
desc "copies the bare ionic html documentation"
task :copy_ionic_docs do
dist = "#{resources_path}/Documents"
FileUtils.mkdir_p("#{dist}/img")
{
"index.html" => "",
"css" => "",
"data" => "",
"js" => "",
"fonts" => "",
"img/docs" => "img",
"img/getting-started" => "img",
"img/input-types" => "img",
"img/*.png" => "img",
"img/*.jpg" => "img",
"img/*.svg" => "img",
"img/testimonials" => "img",
"img/homepage" => "img",
"getting-started" => "",
"docs" => "",
}.each_pair do |src,dest|
FileUtils.cp_r(Dir.glob("compiled/#{src}"), "#{dist}/#{dest}")
end
Dir["compiled/img/*.png", "compiled/img/*.svg"].each do |path|
FileUtils.cp_r(path, "#{dist}/img/.")
end
end
desc "copies Info.plist into the docset"
task :copy_static_files do
FileUtils.cp("Info.plist", "#{docset_root}/Contents/")
FileUtils.cp("icon.png", "#{docset_root}")
end
desc "fixes the css and image paths"
task :make_paths_relative do
PathFix.new("#{resources_path}/Documents").make_paths_relative
end
desc "completes href references to folders with index.html"
task :complete_folder_references do
PathFix.new("compiled").complete_folder_references
end
desc "completes href references to folders with index.html"
task :remove_codepen_references do
PathFix.new("compiled").remove_codepen_references
end
desc "fix href references in main index.html"
task :fix_href_in_main_index do
PathFix.new("compiled/index.html").make_paths_relative_for_root_path
PathFix.new("compiled/css/site.css").make_paths_relative_for_root_path("../")
end
desc "replaces cdn files with local files"
task :cdn_to_static do
FileUtils.cp_r("cdn", "#{resources_path}/Documents")
PathFix.new("#{resources_path}/Documents").cdn_to_statics({
'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js' => 'cdn/jquery.min.js',
'http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js' => 'cdn/bootstrap.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/Cookies.js/0.4.0/cookies.min.js' => 'cdn/cookies.min.js',
'"/js/site.js' => '"js/site.js',
})
end
desc "fixes the site.js script"
task :fix_site_js do
site_js = File.read("#{resources_path}/Documents/js/site.js")
pathname = "window.location.pathname.replace(/^.*?Contents\\/Resources\\/Documents/, '')"
site_js.gsub!("[href=\"' + window.location.pathname", "[href*=\"' + #{pathname}")
File.open("#{resources_path}/Documents/js/site.js", "w+") do |f|
f.write site_js
end
end
desc "create the sqlite3 index"
task :generate_docset_database do
sections_selector = ".menu-section a.api-section"
link_selector = ".menu-section ul a"
js_generator = Generator.new("#{resources_path}/docSet.dsidx", "compiled/docs/nightly/index.html", sections_selector, link_selector)
js_generator.parse
js_generator.create_css_entry
js_generator.build_css_tree
end
end
task :default => ["docset:generate"]