Class: DocuBot::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/docubot/index.rb

Overview

The index links keywords to a particular page.

Keywords are added to the index by:

* Having a "keywords" entry in the metasection, e.g.
  keywords: Rigid Bodies, Dynamic, Physical Mesh
* Surrounding a word or phrase on the page with two @ characters, e.g.
  The purpose of a @@physical mesh@@ is to...
* Having an "index: headings" entry in the metasection, causing each
  heading on the page to be added to the index.
* Having an "index: definitions" entry in the metasection, causing each
  <dt>...</dt> on the page to be added to the index.
  (May be combined with the above as "index: headings definitions".)

As shown above, terms may be referenced in title or lowercase. Names with capital letters will be favored over lowercase in the index.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle) ⇒ Index

Returns a new instance of Index.



20
21
22
23
24
25
# File 'lib/docubot/index.rb', line 20

def initialize( bundle )
  @bundle    = bundle
  @entries   = Hash.new{|h,k|h[k]=[]} # key points to array of DocuBot::Pages

  @downcased = {}
  #TODO: support links to sub-sections instead of just pages

end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



19
20
21
# File 'lib/docubot/index.rb', line 19

def entries
  @entries
end

Instance Method Details

#add(term, page) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docubot/index.rb', line 44

def add( term, page )
  term.strip!
  term.gsub!(/<[^>]+>/,'')
  down = term.downcase
  if existing = @downcased[ down ]
    # The existing entry might be early-arriving all-lowercase.

    # If the new term has more capital letters, it wins.

    if term.scan(/[A-Z]/).length > existing.scan(/[A-Z]/).length
      @downcased[ down ] = term
      @entries[ term ] = @entries[ existing ]
      @entries.delete( existing )
    else
      term = existing
    end
  end
  @entries[ term ] << page
  @entries[ term ].uniq!
  @downcased[ down ] = term
end

#eachObject



64
65
66
# File 'lib/docubot/index.rb', line 64

def each
  @entries.each{ |term, pages| yield term, pages }
end

#process_page(page) ⇒ Object

Run through the ‘keywords’ and ‘index’ meta attribute for a page and add entries Note: in-content @@keyword@@ marks are processed by snippets/index_entries.rb



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/docubot/index.rb', line 29

def process_page( page )
  page.keywords.split(/,\s*/).each{ |key| add( key, page ) } if page.keywords?

  # FIXME: This is substantially slower (but way more correct) than regexp only.

  unless page['no-index'] && page['no-index'].include?( 'headings' )
    %w[h1 h2 h3 h4 h5 h6].each do |hn|
      page.nokodoc.css(hn).each{ |head| add( head.inner_text, page ) }
    end
  end

  unless page['no-index'] && page['no-index'].include?( 'definitions' )
    page.nokodoc.css("dt").each{ |defn| add( defn.inner_text, page ) }
  end
end