Class: HeaderIdGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/devcenter-parser/header_id_generator.rb

Overview

Generates header -> ids in the given doc, calculating the ids from the heading text and ensuring that there are no duplicated ids

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ HeaderIdGenerator

Returns a new instance of HeaderIdGenerator.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/devcenter-parser/header_id_generator.rb', line 9

def initialize(doc)
  @doc = doc
  @header_nodes = @doc.css("h2,h3,h4,h5,h6").to_a

  # { node -> id } hash
  @nodes_ids = @header_nodes.inject({}){ |hash, node| hash[node] = nil; hash }

  add_default_ids
  resolve_conflicts
  @nodes_ids.each{ |node, id| node['id'] = id }
end

Class Method Details

.apply!(doc) ⇒ Object



5
6
7
# File 'lib/devcenter-parser/header_id_generator.rb', line 5

def self.apply!(doc)
  self.new(doc)
end