File tree Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -246,6 +246,7 @@ def generate
246
246
generate_file_files
247
247
generate_table_of_contents
248
248
@json_index . generate
249
+ @json_index . generate_gzipped
249
250
250
251
copy_static
251
252
Original file line number Diff line number Diff line change 1
1
require 'json'
2
+ require 'zlib'
2
3
3
4
##
4
5
# The JsonIndex generator is designed to complement an HTML generator and
@@ -152,6 +153,49 @@ def generate
152
153
end
153
154
end
154
155
156
+ ##
157
+ # Compress the search_index.js file using gzip
158
+
159
+ def generate_gzipped
160
+ debug_msg "Compressing generated JSON index"
161
+ out_dir = @base_dir + @options . op_dir
162
+
163
+ search_index_file = out_dir + SEARCH_INDEX_FILE
164
+ outfile = out_dir + "#{ search_index_file } .gz"
165
+
166
+ debug_msg "Reading the JSON index file from %s" % search_index_file
167
+ search_index = search_index_file . read
168
+
169
+ debug_msg "Writing gzipped search index to %s" % outfile
170
+
171
+ Zlib ::GzipWriter . open ( outfile ) do |gz |
172
+ gz . mtime = File . mtime ( search_index_file )
173
+ gz . orig_name = search_index_file . to_s
174
+ gz . write search_index
175
+ gz . close
176
+ end
177
+
178
+ # GZip the rest of the js files
179
+ Dir . chdir @template_dir do
180
+ Dir [ '**/*.js' ] . each do |source |
181
+ dest = out_dir + source
182
+ outfile = out_dir + "#{ dest } .gz"
183
+
184
+ debug_msg "Reading the original js file from %s" % dest
185
+ data = dest . read
186
+
187
+ debug_msg "Writing gzipped file to %s" % outfile
188
+
189
+ Zlib ::GzipWriter . open ( outfile ) do |gz |
190
+ gz . mtime = File . mtime ( dest )
191
+ gz . orig_name = dest . to_s
192
+ gz . write data
193
+ gz . close
194
+ end
195
+ end
196
+ end
197
+ end
198
+
155
199
##
156
200
# Adds classes and modules to the index
157
201
Original file line number Diff line number Diff line change @@ -136,6 +136,61 @@ def test_generate
136
136
assert_equal expected , index
137
137
end
138
138
139
+ def test_generate_gzipped
140
+ require 'zlib'
141
+ @g . generate
142
+ @g . generate_gzipped
143
+
144
+ assert_file 'js/searcher.js'
145
+ assert_file 'js/searcher.js.gz'
146
+ assert_file 'js/navigation.js'
147
+ assert_file 'js/navigation.js.gz'
148
+ assert_file 'js/search_index.js'
149
+ assert_file 'js/search_index.js.gz'
150
+
151
+ gzip = File . open 'js/search_index.js.gz'
152
+ json = Zlib ::GzipReader . new ( gzip ) . read
153
+
154
+ json =~ /\A var search_data = /
155
+
156
+ assignment = $&
157
+ index = $'
158
+
159
+ refute_empty assignment
160
+
161
+ index = JSON . parse index
162
+
163
+ info = [
164
+ @klass . search_record [ 2 ..-1 ] ,
165
+ @nest_klass . search_record [ 2 ..-1 ] ,
166
+ @meth . search_record [ 2 ..-1 ] ,
167
+ @nest_meth . search_record [ 2 ..-1 ] ,
168
+ @page . search_record [ 2 ..-1 ] ,
169
+ ]
170
+
171
+ expected = {
172
+ 'index' => {
173
+ 'searchIndex' => [
174
+ 'c' ,
175
+ 'd' ,
176
+ 'meth()' ,
177
+ 'meth()' ,
178
+ 'page' ,
179
+ ] ,
180
+ 'longSearchIndex' => [
181
+ 'c' ,
182
+ 'c::d' ,
183
+ 'c#meth()' ,
184
+ 'c::d#meth()' ,
185
+ '' ,
186
+ ] ,
187
+ 'info' => info ,
188
+ } ,
189
+ }
190
+
191
+ assert_equal expected , index
192
+ end
193
+
139
194
def test_generate_utf_8
140
195
skip "Encoding not implemented" unless Object . const_defined? :Encoding
141
196
You can’t perform that action at this time.
0 commit comments