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
|
# File 'lib/lunr/LunrIndexer.rb', line 18
def generate(site, packages)
@js_dir = 'js'
ctx = V8::Context.new
ctx.load(Indexer.path_to_asset('bower_components/lunr.js/lunr.js'))
ctx['indexer'] = proc do |this|
this.ref('id')
this.field('name')
this.field('summary')
end
ctx.eval('builder = new lunr.Builder')
ctx.eval('builder.pipeline.add(lunr.trimmer, lunr.stopWordFilter)')
ctx.eval('indexer.call(builder, builder)')
@lunr_version = ctx.eval('lunr.version')
@docs = {}
Jekyll.logger.info "Lunr:", "Creating search index (lunr.js version #{@lunr_version})..."
@site = site
packages.each_with_index do |package, i|
package_name = package['id']
name = Indexer.content_from_xml(package, 'name')
icon = Indexer.content_from_xml(package, 'icon')
summary = Indexer.content_from_xml(package, 'summary')
doc = {
'id' => i,
'packageName' => package_name,
'icon' => icon,
'name' => name,
'summary' => summary
}
ctx['builder'].add(doc)
@docs[i] = doc
Jekyll.logger.debug "Lunr:", package_name
end
@index = ctx.eval('builder.build()')
FileUtils.mkdir_p(File.join(site.dest, @js_dir))
filename = File.join(@js_dir, 'index.json')
total = {
"docs" => @docs,
"index" => @index.to_hash
}
filepath = File.join(site.dest, filename)
File.open(filepath, "w") { |f| f.write(JSON.dump(total)) }
Jekyll.logger.info "Lunr:", "Index ready (lunr.js v#{@lunr_version})"
added_files = [filename]
site_js = File.join(site.dest, @js_dir)
= [
'assets/fdroid-search-autocomplete.js',
'bower_components/lunr.js/lunr.js',
'bower_components/mustache.js/mustache.min.js',
'bower_components/awesomplete/awesomplete.min.js',
'bower_components/awesomplete/awesomplete.css'
]
Jekyll.logger.info "Lunr:", "Added required assets to #{@js_dir}"
.each do |path|
src = Indexer.path_to_asset(path)
Jekyll.logger.debug "Lunr:", "Copying asset from #{src} to #{site_js}"
FileUtils.cp(src, site_js)
added_files.push(File.join(@js_dir, File.basename(src)))
end
added_files.each do |filename|
site.static_files << SearchIndexFile.new(site, site.dest, "/", filename)
end
end
|