Class: Brief::Document::Section
- Inherits:
-
Object
- Object
- Brief::Document::Section
- Defined in:
- lib/brief/document/section.rb,
lib/brief/document/section/builder.rb,
lib/brief/document/section/mapping.rb
Defined Under Namespace
Constant Summary collapse
- Headings =
%w(h1 h2 h3 h4 h5 h6)
- BuilderError =
Class.new(Exception)
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(title, fragment, config) ⇒ Section
constructor
A new instance of Section.
- #items ⇒ Object
- #text ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(title, fragment, config) ⇒ Section
Returns a new instance of Section.
7 8 9 10 11 |
# File 'lib/brief/document/section.rb', line 7 def initialize(title, fragment, config) @title = title @fragment = fragment @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/brief/document/section.rb', line 3 def config @config end |
#fragment ⇒ Object
Returns the value of attribute fragment.
2 3 4 |
# File 'lib/brief/document/section.rb', line 2 def fragment @fragment end |
#title ⇒ Object
Returns the value of attribute title.
2 3 4 |
# File 'lib/brief/document/section.rb', line 2 def title @title end |
Instance Method Details
#items ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brief/document/section.rb', line 21 def items return @items if @items data = [] config.selectors.each do |selector| settings = config.selector_config[selector] if Headings.include?(selector) headings = fragment.css("article > #{selector}") articles = headings.map do |el| el.parent.set_attribute('data-parent-heading', el.text) el.parent end unless settings.empty? articles.compact.each do |article| data.push(settings.inject({}.to_mash) do |memo, pair| attribute, selector = pair result = article.css(selector) memo[attribute] = result.length > 1 ? result.map(&:text) : result.text memo end) end end end end @items = data end |
#text ⇒ Object
17 18 19 |
# File 'lib/brief/document/section.rb', line 17 def text fragment.text end |
#to_html ⇒ Object
13 14 15 |
# File 'lib/brief/document/section.rb', line 13 def to_html fragment.to_html end |