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
Markdown rendered HTML comes in the forms of a bunch of siblings, and no parents.
- #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
Markdown rendered HTML comes in the forms of a bunch of siblings, and no parents. We need to introduce the concept of ownership of sections of the document, by using the heading level (h1 - h6) as a form of rank.
All h1 elements will ‘own’ the h2,h3,h4,h5,h6 elements underneath them.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/brief/document/structure.rb', line 36 def create_wrappers return if @elements_have_been_wrapped elements = fragment.children # The different groups of elements mapping = [] # The current bucket of elements that is being # collected, will get reset whenever it runs into # an element that is a greater heading rank bucket = [] current_level = Util.level(elements.first) elements.each_cons(2) do |element, next_element| bucket << element # We will have run into a greater header, so close up the bucket # and put it into the mapping 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 # we never ended up reaching a header, so close up and move on if !mapping.include?(bucket) mapping.push([current_level, bucket]) end 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
132 133 134 135 136 |
# File 'lib/brief/document/structure.rb', line 132 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
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/brief/document/structure.rb', line 138 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
119 120 121 122 123 124 |
# File 'lib/brief/document/structure.rb', line 119 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
109 110 111 112 113 114 115 116 117 |
# File 'lib/brief/document/structure.rb', line 109 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
126 127 128 129 130 |
# File 'lib/brief/document/structure.rb', line 126 def headings_with_text(text) heading_elements.select do |el| el.heading.to_s.strip == text.to_s.strip end end |
#highest_level ⇒ Object
101 102 103 |
# File 'lib/brief/document/structure.rb', line 101 def highest_level levels.max end |
#levels ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/brief/document/structure.rb', line 93 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
105 106 107 |
# File 'lib/brief/document/structure.rb', line 105 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 |