Class: OrigenDocHelpers::GuideIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/origen_doc_helpers/guide_index.rb

Overview

Provides an API to programatically construct an index hash as used by the Searchable Documents helper - origen-sdk.org/doc_helpers/helpers/searchable/intro/#The_Document_Index

Instance Method Summary collapse

Constructor Details

#initializeGuideIndex

Returns a new instance of GuideIndex.



6
7
8
9
10
11
# File 'lib/origen_doc_helpers/guide_index.rb', line 6

def initialize
  @index = {}
  @section_keys = {}
  @pending_sections = []
  @pending_pages = []
end

Instance Method Details

#force_pendingObject

Force any pending sections and pages into the index, these will be added at the end since their :after/:before reference is not valid



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/origen_doc_helpers/guide_index.rb', line 15

def force_pending
  @force_pending = true
  @pending_sections.each do |id, opts, blk|
    section(id, opts, &blk)
  end
  @pending_pages.each do |section_id, id, opts|
    @current_section_id = section_id
    @current_section = @index[section_key]
    page(id, opts)
  end

  @force_pending = nil
  self
end

#page(id, options = {}) ⇒ Object



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
# File 'lib/origen_doc_helpers/guide_index.rb', line 82

def page(id, options = {})
  unless @current_section
    fail 'page can only be called from within a section block!'
  end
  @current_topic_id = id
  value = options[:heading] || id
  value = value.to_s if value
  page_pending = false
  if options[:after] && !@force_pending
    if has_key?(:@current_section, topic_key(options[:after]))
      insert_after(:@current_section, topic_key, topic_key(options[:after]), value)
    else
      @pending_pages << [@current_section_id, id, options.dup] unless @no_page_pending
      page_pending = true
    end
  elsif options[:before] && !@force_pending
    if has_key?(:@current_section, topic_key(options[:before]))
      insert_before(:@current_section, topic_key, topic_key(options[:before]), value)
    else
      @pending_pages << [@current_section_id, id, options.dup] unless @no_page_pending
      page_pending = true
    end
  else
    @current_section[topic_key] = value
  end
  # Update the parent reference, required if before or after was used to create a new
  # @current_section hash
  @index[section_key] = @current_section unless page_pending

  # See if any pending pages can now be inserted
  unless @no_page_pending || @force_pending
    @pending_pages.each do |section_id, id, opts|
      @no_page_pending = true
      page(id, opts)
      @no_page_pending = false
    end
  end

  self
end

#section(id, options = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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
# File 'lib/origen_doc_helpers/guide_index.rb', line 30

def section(id, options = {}, &block)
  @current_section_id = id
  if options[:heading]
    # This is to handle the corner case where an id reference was originally supplied
    # without the heading, then a later reference added the heading
    if @section_keys[id]
      if @section_keys[id] != options[:heading]
        change_key(:@index, @section_keys[id], options[:heading])
        @section_keys[id] = options[:heading]
      end
    else
      @section_keys[id] = options[:heading]
    end
  else
    @section_keys[id] ||= id
  end
  @section_keys[id] ||= @section_keys[id].to_s if @section_keys[id]
  section_pending = false
  unless @index[section_key]
    if options[:after] && !@force_pending
      if has_key?(:@index, section_key(options[:after]))
        insert_after(:@index, section_key, section_key(options[:after]), {})
      else
        @pending_sections << [id, options.dup, block] unless @no_pending
        section_pending = true
      end
    elsif options[:before] && !@force_pending
      if has_key?(:@index, section_key(options[:before]))
        insert_before(:@index, section_key, section_key(options[:before]), {})
      else
        @pending_sections << [id, options.dup, block] unless @no_pending
        section_pending = true
      end
    else
      @index[section_key] = {}
    end
  end
  @current_section = @index[section_key]
  yield self unless section_pending
  @current_section = nil

  # See if any pending sections can now be inserted
  unless @no_pending || @force_pending
    @pending_sections.each do |id, opts, blk|
      @no_pending = true
      section(id, opts, &blk)
      @no_pending = false
    end
  end
  self
end

#to_hObject



123
124
125
# File 'lib/origen_doc_helpers/guide_index.rb', line 123

def to_h
  @index
end