Class: Brief::Document::Structure
- Inherits:
-
Object
- Object
- Brief::Document::Structure
- Defined in:
- lib/brief/document/structure.rb
Defined Under Namespace
Classes: Util
Instance Attribute Summary collapse
-
#content_lines ⇒ Object
Returns the value of attribute content_lines.
-
#fragment ⇒ Object
Returns the value of attribute fragment.
Instance Method Summary collapse
- #create_wrappers ⇒ Object
- #find_heading_by(level, heading) ⇒ Object
- #heading_elements ⇒ Object
- #heading_with_text(text) ⇒ Object
- #headings_at_level(level, options = {}) ⇒ Object
- #headings_with_text(text) ⇒ Object
- #highest_level ⇒ Object
-
#initialize(fragment, content_lines = []) ⇒ Structure
constructor
A new instance of Structure.
- #levels ⇒ Object
- #lowest_level ⇒ Object
- #prescan ⇒ Object
Constructor Details
#initialize(fragment, content_lines = []) ⇒ Structure
Returns a new instance of Structure.
5 6 7 8 |
# File 'lib/brief/document/structure.rb', line 5 def initialize(fragment, content_lines = []) @fragment = fragment @content_lines = content_lines end |
Instance Attribute Details
#content_lines ⇒ Object
Returns the value of attribute content_lines.
3 4 5 |
# File 'lib/brief/document/structure.rb', line 3 def content_lines @content_lines end |
#fragment ⇒ Object
Returns the value of attribute fragment.
3 4 5 |
# File 'lib/brief/document/structure.rb', line 3 def fragment @fragment end |
Instance Method Details
#create_wrappers ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/brief/document/structure.rb', line 29 def create_wrappers return if @elements_have_been_wrapped elements = fragment.children mapping = [] bucket = [] current_level = Util.level(elements.first) elements.each_cons(2) do |element, next_element| bucket << element if Util.is_header?(next_element) && Util.level(next_element) >= current_level mapping.push([current_level, bucket]) bucket = [] end if Util.is_header?(element) current_level = Util.level(element) end end mapping.push([current_level, bucket]) unless mapping.include?(bucket) base_fragment = Nokogiri::HTML.fragment("<div class='brief top level' />") mapping.map! do |item| level, group = item group.reject! { |i| i.text == "\n" } if level == 0 base_fragment = fragment = Nokogiri::HTML.fragment("<div class='brief top level'>#{ group.map(&:to_html).join('') }</div>") elsif level <= lowest_level fragment = Nokogiri::HTML.fragment("<section>#{ group.map(&:to_html).join('') }</section>") elsif level > lowest_level # should be able to look at the document section mappings and # apply custom css classes to these based on the name of the section fragment = Nokogiri::HTML.fragment("<article>#{ group.map(&:to_html).join('') }</article>") end [level, [fragment]] end self.fragment = Brief::Document::Section::Builder.run(mapping, low: lowest_level, high: highest_level) end |
#find_heading_by(level, heading) ⇒ Object
115 116 117 118 119 |
# File 'lib/brief/document/structure.rb', line 115 def find_heading_by(level, heading) heading_elements.find do |el| el.level.to_s == level.to_s && heading.to_s.strip == el.heading.to_s.strip end end |
#heading_elements ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/brief/document/structure.rb', line 121 def heading_elements @heading_elements ||= fragment.css('h1,h2,h3,h4,h5,h6').map do |el| if el.attr('data-level').to_i > 0 { level: el.attr('data-level'), heading: el.attr('data-heading'), element: el }.to_mash end end.compact end |
#heading_with_text(text) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/brief/document/structure.rb', line 102 def heading_with_text(text) headings_with_text(text).tap do |results| fail 'no section found with content: ' + text if results.length == 0 fail 'more than one section found with content: ' + text if results.length >= 2 end.first end |
#headings_at_level(level, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/brief/document/structure.rb', line 92 def headings_at_level(level, = {}) matches = heading_elements.select { |el| el.level.to_i == level.to_i } if [:text] matches.map(&:text) else matches end end |
#headings_with_text(text) ⇒ Object
109 110 111 112 113 |
# File 'lib/brief/document/structure.rb', line 109 def headings_with_text(text) heading_elements.select do |el| el.heading.to_s.strip == text.to_s.strip end end |
#highest_level ⇒ Object
84 85 86 |
# File 'lib/brief/document/structure.rb', line 84 def highest_level levels.max end |
#levels ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/brief/document/structure.rb', line 76 def levels l = fragment.css('[data-level]').map { |el| el.attr('data-level').to_i } l.reject!(&:nil?) l.reject! { |v| v.to_i == 0 } l.uniq! l end |
#lowest_level ⇒ Object
88 89 90 |
# File 'lib/brief/document/structure.rb', line 88 def lowest_level levels.min end |
#prescan ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/brief/document/structure.rb', line 10 def prescan content_lines.each_with_index do |line, index| if line.match(/^#/) line = line.strip level = line.count('#') text = line.gsub('#', '').strip if level > 0 && text.length > 0 line_number = index + 1 heading = find_heading_by(level, text) if heading heading.element.set_attribute('data-line-number', line_number) end end end end end |