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 =
%w[text h1 h2 h3 h4 h5 h6].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ 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
40
# 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?(name)
    options[:headlines][name].each do |element|
      block << element
    end
  end

  # collect nested headlines
  headlines = options[:headlines]
  # remove self from list
  headlines.delete(name)
  nested_headlines = self.nested_headlines(headlines, name, 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:, 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



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

def elements
  block.elements
end

#has_headline?(name) ⇒ Boolean

headline exists for current headline



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

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

#headline(name) ⇒ Object

get headline by name



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

def headline(name)
  name = name.downcase.gsub(' ', '_')
  self.headlines.select do |k, _v|
    k.downcase.start_with?(name)
  end.values
end

#headline_in_depth(name, depth = 1) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wiki/api/page_headline.rb', line 58

def headline_in_depth(name, depth = 1)
  name = name.downcase.gsub(' ', '_')
  ret = []

  self.headlines.each do |k, v|
    ret << v if k.downcase.start_with?(name)
    next if v.headlines.empty?

    if depth.positive?
      q = v.headline_in_depth(name, (depth - 1))
      ret.concat(q)
    end
  end
  ret
end

#to_hashObject



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

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

#to_pretty_jsonObject



91
92
93
# File 'lib/wiki/api/page_headline.rb', line 91

def to_pretty_json
  JSON.pretty_generate(to_hash)
end

#typeObject



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

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