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 =
%w[text h1 h2 h3 h4 h5 h6].freeze
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.
- #headline_in_depth(name, depth = 1) ⇒ Object
-
#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 40 |
# 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?(name) [:headlines][name].each do |element| block << element end end # collect nested headlines headlines = [: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
#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
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_hash ⇒ Object
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_json ⇒ Object
91 92 93 |
# File 'lib/wiki/api/page_headline.rb', line 91 def to_pretty_json JSON.pretty_generate(to_hash) end |
#type ⇒ Object
46 47 48 |
# File 'lib/wiki/api/page_headline.rb', line 46 def type block.elements.first.first.previous.name end |