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
|
require 'json'
require 'zlib'
##
# The JsonIndex generator is designed to complement an HTML generator and
# produces a JSON search index. This generator is derived from sdoc by
# Vladimir Kolesnikov and contains verbatim code written by him.
#
# This generator is designed to be used with a regular HTML generator:
#
# class RDoc::Generator::Darkfish
# def initialize options
# # ...
# @base_dir = Pathname.pwd.expand_path
#
# @json_index = RDoc::Generator::JsonIndex.new self, options
# end
#
# def generate
# # ...
# @json_index.generate
# end
# end
#
# == Index Format
#
# The index is output as a JSON file assigned to the global variable
# +search_data+. The structure is:
#
# var search_data = {
# "index": {
# "searchIndex":
# ["a", "b", ...],
# "longSearchIndex":
# ["a", "a::b", ...],
# "info": [
# ["A", "A", "A.html", "", ""],
# ["B", "A::B", "A::B.html", "", ""],
# ...
# ]
# }
# }
#
# The same item is described across the +searchIndex+, +longSearchIndex+ and
# +info+ fields. The +searchIndex+ field contains the item's short name, the
# +longSearchIndex+ field contains the full_name (when appropriate) and the
# +info+ field contains the item's name, full_name, path, parameters and a
# snippet of the item's comment.
#
# == LICENSE
#
# Copyright (c) 2009 Vladimir Kolesnikov
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class RDoc::Generator::JsonIndex
include RDoc::Text
##
# Where the search index lives in the generated output
SEARCH_INDEX_FILE = File.join 'js', 'search_index.js'
attr_reader :index # :nodoc:
##
# Creates a new generator. +parent_generator+ is used to determine the
# class_dir and file_dir of links in the output index.
#
# +options+ are the same options passed to the parent generator.
def initialize parent_generator, options
@parent_generator = parent_generator
@store = parent_generator.store
@options = options
@template_dir = File.expand_path '../template/json_index', __FILE__
@base_dir = @parent_generator.base_dir
@classes = nil
@files = nil
@index = nil
end
##
# Builds the JSON index as a Hash.
def build_index
reset @store.all_files.sort, @store.all_classes_and_modules.sort
index_classes
index_methods
index_pages
{ :index => @index }
end
##
# Output progress information if debugging is enabled
def debug_msg *msg
return unless $DEBUG_RDOC
$stderr.puts(*msg)
end
##
# Writes the JSON index to disk
def generate
debug_msg "Generating JSON index"
debug_msg " writing search index to %s" % SEARCH_INDEX_FILE
data = build_index
return if @options.dry_run
out_dir = @base_dir + @options.op_dir
index_file = out_dir + SEARCH_INDEX_FILE
FileUtils.mkdir_p index_file.dirname, :verbose => $DEBUG_RDOC
index_file.open 'w', 0644 do |io|
io.set_encoding Encoding::UTF_8 if Object.const_defined? :Encoding
io.write 'var search_data = '
JSON.dump data, io, 0
end
Dir.chdir @template_dir do
Dir['**/*.js'].each do |source|
dest = File.join out_dir, source
FileUtils.install source, dest, :mode => 0644, :verbose => $DEBUG_RDOC
end
end
end
##
# Compress the search_index.js file using gzip
def generate_gzipped
debug_msg "Compressing generated JSON index"
out_dir = @base_dir + @options.op_dir
search_index_file = out_dir + SEARCH_INDEX_FILE
outfile = out_dir + "#{search_index_file}.gz"
debug_msg "Reading the JSON index file from %s" % search_index_file
search_index = search_index_file.read
debug_msg "Writing gzipped search index to %s" % outfile
Zlib::GzipWriter.open(outfile) do |gz|
gz.mtime = File.mtime(search_index_file)
gz.orig_name = search_index_file.to_s
gz.write search_index
gz.close
end
# GZip the rest of the js files
Dir.chdir @template_dir do
Dir['**/*.js'].each do |source|
dest = out_dir + source
outfile = out_dir + "#{dest}.gz"
debug_msg "Reading the original js file from %s" % dest
data = dest.read
debug_msg "Writing gzipped file to %s" % outfile
Zlib::GzipWriter.open(outfile) do |gz|
gz.mtime = File.mtime(dest)
gz.orig_name = dest.to_s
gz.write data
gz.close
end
end
end
end
##
# Adds classes and modules to the index
def index_classes
debug_msg " generating class search index"
documented = @classes.uniq.select do |klass|
klass.document_self_or_methods
end
documented.each do |klass|
debug_msg " #{klass.full_name}"
record = klass.search_record
@index[:searchIndex] << search_string(record.shift)
@index[:longSearchIndex] << search_string(record.shift)
@index[:info] << record
end
end
##
# Adds methods to the index
def index_methods
debug_msg " generating method search index"
list = @classes.uniq.map do |klass|
klass.method_list
end.flatten.sort_by do |method|
[method.name, method.parent.full_name]
end
list.each do |method|
debug_msg " #{method.full_name}"
record = method.search_record
@index[:searchIndex] << "#{search_string record.shift}()"
@index[:longSearchIndex] << "#{search_string record.shift}()"
@index[:info] << record
end
end
##
# Adds pages to the index
def index_pages
debug_msg " generating pages search index"
pages = @files.select do |file|
file.text?
end
pages.each do |page|
debug_msg " #{page.page_name}"
record = page.search_record
@index[:searchIndex] << search_string(record.shift)
@index[:longSearchIndex] << ''
record.shift
@index[:info] << record
end
end
##
# The directory classes are written to
def class_dir
@parent_generator.class_dir
end
##
# The directory files are written to
def file_dir
@parent_generator.file_dir
end
def reset files, classes # :nodoc:
@files = files
@classes = classes
@index = {
:searchIndex => [],
:longSearchIndex => [],
:info => []
}
end
##
# Removes whitespace and downcases +string+
def search_string string
string.downcase.gsub(/\s/, '')
end
end
|