Class: Peregrin::Outliner

Inherits:
Object
  • Object
show all
Defined in:
lib/peregrin/outliner.rb

Defined Under Namespace

Classes: Section, Utils

Constant Summary collapse

REGEXES =
{
  :section_root => /^BLOCKQUOTE|BODY|DETAILS|FIELDSET|FIGURE|TD$/i,
  :section_content => /^ARTICLE|ASIDE|NAV|SECTION$/i,
  :heading => /^H[1-6]|HGROUP$/i
}

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Outliner

Returns a new instance of Outliner.



92
93
94
# File 'lib/peregrin/outliner.rb', line 92

def initialize(doc)
  @document = doc
end

Instance Method Details

#enter_node(node) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/peregrin/outliner.rb', line 114

def enter_node(node)
  return  if Utils.heading?(@stack.last)

  if Utils.section_content?(node) || Utils.section_root?(node)
    @stack.push(@outlinee)  unless @outlinee.nil?
    @outlinee = node
    @section = Section.new(node)
    @outlines[@outlinee] = Section.new(node)
    @outlines[@outlinee].sections = [@section]
    return
  end

  return  if @outlinee.nil?

  if Utils.heading?(node)
    node_rank = Utils.heading_rank(node)
    if !@section.heading
      @section.heading = node
    elsif node_rank <= @outlines[@outlinee].sections.last.heading_rank
      @section = Section.new
      @section.heading = node
      @outlines[@outlinee].sections.push(@section)
    else
      candidate = @section
      while true
        if node_rank > candidate.heading_rank
          @section = Section.new
          candidate.append(@section)
          @section.heading = node
          break
        end
        candidate = candidate.container
      end
    end
    @stack.push(node)
  end
end

#exit_node(node) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/peregrin/outliner.rb', line 153

def exit_node(node)
  if Utils.heading?(@stack.last)
    @stack.pop  if @stack.last == node
    return
  end

  if Utils.section_content?(node) && !@stack.empty?
    @outlinee = @stack.pop
    @section = @outlines[@outlinee].sections.last
    @outlines[node].sections.each { |s| @section.append(s) }
    return
  end

  if Utils.section_root?(node) && !@stack.empty?
    @outlinee = @stack.pop
    @section = @outlines[@outlinee].sections.last
    while @section.sections.any?
      @section = @section.sections.last
    end
    return
  end

  if Utils.section_content?(node) || Utils.section_root?(node)
    @section = @outlines[@outlinee].sections.first
    return
  end
end

#process(from) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/peregrin/outliner.rb', line 97

def process(from)
  @outlinee = nil
  @outlines = {}
  @section = Section.new
  @stack = []
  walk(from)
end

#result_rootObject



200
201
202
# File 'lib/peregrin/outliner.rb', line 200

def result_root
  @outlines[@outlinee]
end

#to_htmlObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/peregrin/outliner.rb', line 182

def to_html
  curse = lambda { |section, is_root|
    below = section.sections.collect { |ch|
      ch_out = curse.call(ch, false).strip
      (ch_out.nil? || ch_out.empty?) ? "" : "<li>#{ch_out}</li>"
    }.join.strip
    below = (below.nil? || below.empty?) ? "" : "<ol>#{below}</ol>\n"
    if is_root
      below
    else
      heading = block_given? ? yield(section, below) : section.heading_text
      "#{heading}#{below}"
    end
  }
  curse.call(result_root, true)
end

#walk(node) ⇒ Object



106
107
108
109
110
111
# File 'lib/peregrin/outliner.rb', line 106

def walk(node)
  return  unless node
  enter_node(node)
  node.children.each { |ch| walk(ch) }
  exit_node(node)
end