Class: RDoc::Generator::JsonIndex (Ruby 2.2.4)
Description: Class : RDoc::Generator::JsonIndex - Ruby 2.2.4

- Home
- Core 2.2.4
- Std-lib 2.2.4
- Downloads

Support for the Ruby 2.2 series has ended. See here for reference.

### Home Classes Methods

### In Files

- rdoc/generator/json_index.rb

### Parent

Object

### Methods

- ::new
- #build_index
- #class_dir
- #debug_msg
- #file_dir
- #generate
- #generate_gzipped
- #index_classes
- #index_methods
- #index_pages
- #search_string

### Included Modules

- RDoc::Text

### Class/Module Index

Quicksearch

- RDoc
- RDoc::Alias
- RDoc::AnonClass
- RDoc::AnyMethod
- RDoc::Attr
- RDoc::ClassModule
- RDoc::CodeObject
- RDoc::Comment
- RDoc::Constant
- RDoc::Context
- RDoc::Context::Section
- RDoc::CrossReference
- RDoc::ERBIO
- RDoc::ERBPartial
- RDoc::Encoding
- RDoc::Error
- RDoc::Extend
- RDoc::Generator
- RDoc::Generator::Darkfish
- RDoc::Generator::JsonIndex
- RDoc::Generator::Markup
- RDoc::Generator::POT
- RDoc::Generator::POT::MessageExtractor
- RDoc::Generator::POT::PO
- RDoc::Generator::POT::POEntry
- RDoc::Generator::RI
- RDoc::GhostMethod
- RDoc::I18n
- RDoc::I18n::Locale
- RDoc::I18n::Text
- RDoc::Include
- RDoc::Markdown
- RDoc::Markdown::Literals
- RDoc::Markup
- RDoc::Markup::AttrChanger
- RDoc::Markup::AttrSpan
- RDoc::Markup::AttributeManager
- RDoc::Markup::Attributes
- RDoc::Markup::BlankLine
- RDoc::Markup::BlockQuote
- RDoc::Markup::Document
- RDoc::Markup::Formatter
- RDoc::Markup::FormatterTestCase
- RDoc::Markup::HardBreak
- RDoc::Markup::Include
- RDoc::Markup::IndentedParagraph
- RDoc::Markup::List
- RDoc::Markup::ListItem
- RDoc::Markup::Paragraph
- RDoc::Markup::Parser
- RDoc::Markup::Parser::Error
- RDoc::Markup::Parser::ParseError
- RDoc::Markup::PreProcess
- RDoc::Markup::Raw
- RDoc::Markup::Rule
- RDoc::Markup::Special
- RDoc::Markup::TextFormatterTestCase
- RDoc::Markup::ToAnsi
- RDoc::Markup::ToBs
- RDoc::Markup::ToHtml
- RDoc::Markup::ToHtmlCrossref
- RDoc::Markup::ToHtmlSnippet
- RDoc::Markup::ToJoinedParagraph
- RDoc::Markup::ToLabel
- RDoc::Markup::ToMarkdown
- RDoc::Markup::ToRdoc
- RDoc::Markup::ToTableOfContents
- RDoc::Markup::ToTest
- RDoc::Markup::ToTtOnly
- RDoc::Markup::Verbatim
- RDoc::MetaMethod
- RDoc::MethodAttr
- RDoc::Mixin
- RDoc::NormalClass
- RDoc::NormalModule
- RDoc::Options
- RDoc::Parser
- RDoc::Parser::C
- RDoc::Parser::ChangeLog
- RDoc::Parser::Markdown
- RDoc::Parser::RD
- RDoc::Parser::Ruby
- RDoc::Parser::RubyTools
- RDoc::Parser::Simple
- RDoc::Parser::Text
- RDoc::RD
- RDoc::RD::BlockParser
- RDoc::RD::Inline
- RDoc::RD::InlineParser
- RDoc::RDoc
- RDoc::RI
- RDoc::RI::Driver
- RDoc::RI::Driver::Error
- RDoc::RI::Driver::NotFoundError
- RDoc::RI::Error
- RDoc::RI::Paths
- RDoc::RI::Task
- RDoc::Require
- RDoc::RubyLex
- RDoc::RubyLex::Error
- RDoc::RubyToken
- RDoc::RubygemsHook
- RDoc::Servlet
- RDoc::SingleClass
- RDoc::Stats
- RDoc::Stats::Normal
- RDoc::Stats::Quiet
- RDoc::Stats::Verbose
- RDoc::Store
- RDoc::Store::Error
- RDoc::Store::MissingFileError
- RDoc::Task
- RDoc::TestCase
- RDoc::Text
- RDoc::TokenStream
- RDoc::TomDoc
- RDoc::TopLevel
- RDocTask
- Test

No matching classes.

# RDoc::Generator::JsonIndex

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 © 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.

### Constants

SEARCH_INDEX_FILE

  Where the search index lives in the generated output

### Public Class Methods

new(parent_generator, options) click to toggle source

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.

                   # File rdoc/generator/json_index.rb, line 90
    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

### Public Instance Methods

build_index() click to toggle source

Builds the JSON index as a Hash.

                   # File rdoc/generator/json_index.rb, line 106
    def build_index
      reset @store.all_files.sort, @store.all_classes_and_modules.sort

      index_classes
      index_methods
      index_pages

      { :index => @index }
    end

class_dir() click to toggle source

The directory classes are written to

                   # File rdoc/generator/json_index.rb, line 262
    def class_dir
      @parent_generator.class_dir
    end

debug_msg(*msg) click to toggle source

Output progress information if debugging is enabled

                   # File rdoc/generator/json_index.rb, line 119
    def debug_msg *msg
      return unless $DEBUG_RDOC
      $stderr.puts(*msg)
    end

file_dir() click to toggle source

The directory files are written to

                   # File rdoc/generator/json_index.rb, line 269
    def file_dir
      @parent_generator.file_dir
    end

generate() click to toggle source

Writes the JSON index to disk

                   # File rdoc/generator/json_index.rb, line 127
    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

generate_gzipped() click to toggle source

Compress the search_index.js file using gzip

                   # File rdoc/generator/json_index.rb, line 159
    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

index_classes() click to toggle source

Adds classes and modules to the index

                   # File rdoc/generator/json_index.rb, line 202
    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

index_methods() click to toggle source

Adds methods to the index

                   # File rdoc/generator/json_index.rb, line 221
    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

index_pages() click to toggle source

Adds pages to the index

                   # File rdoc/generator/json_index.rb, line 242
    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

search_string(string) click to toggle source

Removes whitespace and downcases `string`

                   # File rdoc/generator/json_index.rb, line 287
    def search_string string
      string.downcase.gsub(/\s/, '')
    end