Class: Wiki::Api::PageHeadline
- Inherits:
-
Object
- Object
- Wiki::Api::PageHeadline
- 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
-
#block ⇒ Object
Returns the value of attribute block.
-
#headlines ⇒ Object
Returns the value of attribute headlines.
-
#level ⇒ Object
Returns the value of attribute level.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #elements ⇒ Object
-
#has_headline?(name) ⇒ Boolean
headline exists for current headline.
-
#headline(name) ⇒ Object
get headline by name.
-
#initialize(options = {}) ⇒ PageHeadline
constructor
A new instance of PageHeadline.
- #to_hash ⇒ Object
- #to_pretty_json ⇒ Object
- #type ⇒ Object
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 ={} self.name = [:name] if .include? :name self.parent = [:parent] if .include? :parent self.level = [:level] if .include? :level [:headlines] ||= [] self.headlines ||= {} # store elements in a block self.block = PageBlock.new parent: self if [:headlines].include? self.name [:headlines][self.name].each do |element| self.block << element end end # collect nested headlines headlines = [: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
#block ⇒ Object
Returns the value of attribute block.
11 12 13 |
# File 'lib/wiki/api/page_headline.rb', line 11 def block @block end |
#headlines ⇒ Object
Returns the value of attribute headlines.
11 12 13 |
# File 'lib/wiki/api/page_headline.rb', line 11 def headlines @headlines end |
#level ⇒ Object
Returns the value of attribute level.
11 12 13 |
# File 'lib/wiki/api/page_headline.rb', line 11 def level @level end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/wiki/api/page_headline.rb', line 11 def name @name end |
#parent ⇒ Object
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
#elements ⇒ Object
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
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_hash ⇒ Object
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_json ⇒ Object
89 90 91 |
# File 'lib/wiki/api/page_headline.rb', line 89 def to_pretty_json JSON.pretty_generate self.to_hash end |
#type ⇒ Object
45 46 47 |
# File 'lib/wiki/api/page_headline.rb', line 45 def type self.block.elements.first.first.previous.name end |