Skip to content

JsonIndex#generate_gzipped #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def generate
generate_file_files
generate_table_of_contents
@json_index.generate
@json_index.generate_gzipped

copy_static

Expand Down
44 changes: 44 additions & 0 deletions lib/rdoc/generator/json_index.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'json'
require 'zlib'

##
# The JsonIndex generator is designed to complement an HTML generator and
Expand Down Expand Up @@ -152,6 +153,49 @@ def generate
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

Expand Down
55 changes: 55 additions & 0 deletions test/test_rdoc_generator_json_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,61 @@ def test_generate
assert_equal expected, index
end

def test_generate_gzipped
require 'zlib'
@g.generate
@g.generate_gzipped

assert_file 'js/searcher.js'
assert_file 'js/searcher.js.gz'
assert_file 'js/navigation.js'
assert_file 'js/navigation.js.gz'
assert_file 'js/search_index.js'
assert_file 'js/search_index.js.gz'

gzip = File.open 'js/search_index.js.gz'
json = Zlib::GzipReader.new(gzip).read

json =~ /\Avar search_data = /

assignment = $&
index = $'

refute_empty assignment

index = JSON.parse index

info = [
@klass.search_record[2..-1],
@nest_klass.search_record[2..-1],
@meth.search_record[2..-1],
@nest_meth.search_record[2..-1],
@page.search_record[2..-1],
]

expected = {
'index' => {
'searchIndex' => [
'c',
'd',
'meth()',
'meth()',
'page',
],
'longSearchIndex' => [
'c',
'c::d',
'c#meth()',
'c::d#meth()',
'',
],
'info' => info,
},
}

assert_equal expected, index
end

def test_generate_utf_8
skip "Encoding not implemented" unless Object.const_defined? :Encoding

Expand Down