Class: Wiki::Api::PageHeadline

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki/api/page_headline.rb

Overview

Headline for a page (class=“mw-healine”)

Constant Summary collapse

LEVEL =
["text", "h1", "h2", "h3", "h4", "h5", "h6"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PageHeadline

Returns a new instance of PageHeadline.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wiki/api/page_headline.rb', line 13

def initialize options={}
  self.name = options[:name] if options.include? :name
  self.parent = options[:parent] if options.include? :parent
  self.level = options[:level] if options.include? :level
  options[:headlines] ||= []
  self.headlines ||= {}

  # store elements in a block
  self.block = PageBlock.new parent: self
  if options[:headlines].include? self.name
    options[:headlines][self.name].each do |element|
      self.block << element
    end
  end

  # collect nested headlines
  headlines = options[:headlines]
  # remove self from list
  headlines.delete self.name
  nested_headlines = self.nested_headlines headlines, self.name, self.level

  # iterate nested headlines, and call recursive
  nested_headlines.each do |headline_name, value|
    level = LEVEL.index value.first.first.previous.name
    self.headlines[headline_name] = (PageHeadline.new parent: self, name: headline_name, headlines: headlines, level: level)
  end
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.


11
12
13
# File 'lib/wiki/api/page_headline.rb', line 11

def block
  @block
end

#headlinesObject

Returns the value of attribute headlines.


11
12
13
# File 'lib/wiki/api/page_headline.rb', line 11

def headlines
  @headlines
end

#levelObject

Returns the value of attribute level.


11
12
13
# File 'lib/wiki/api/page_headline.rb', line 11

def level
  @level
end

#nameObject

Returns the value of attribute name.


11
12
13
# File 'lib/wiki/api/page_headline.rb', line 11

def name
  @name
end

#parentObject

Returns the value of attribute parent.


11
12
13
# File 'lib/wiki/api/page_headline.rb', line 11

def parent
  @parent
end

Instance Method Details

#elementsObject


41
42
43
# File 'lib/wiki/api/page_headline.rb', line 41

def elements
  self.block.elements
end

#has_headline?(name) ⇒ Boolean

headline exists for current headline

Returns:

  • (Boolean)

73
74
75
76
77
78
79
# File 'lib/wiki/api/page_headline.rb', line 73

def has_headline? name
  name = name.downcase.gsub(" ", "_")
  self.headlines.each do |k,v|
    return true if k.downcase.start_with?(name)
  end
  false
end

#headline(name) ⇒ Object

get headline by name


50
51
52
53
54
55
# File 'lib/wiki/api/page_headline.rb', line 50

def headline name
  name = name.downcase.gsub(" ", "_")
  self.headlines.reject do |k,v| 
    !k.downcase.start_with?(name)
  end.values()
end

#to_hashObject


81
82
83
84
85
86
87
# File 'lib/wiki/api/page_headline.rb', line 81

def to_hash
  ret = {name: self.name, headlines: [], type: self.type}
  self.headlines.each do |headline_name, headline|
    ret[:headlines] << headline.to_hash
  end
  ret
end

#to_pretty_jsonObject


89
90
91
# File 'lib/wiki/api/page_headline.rb', line 89

def to_pretty_json
  JSON.pretty_generate self.to_hash
end

#typeObject


45
46
47
# File 'lib/wiki/api/page_headline.rb', line 45

def type
  self.block.elements.first.first.previous.name
end