Method: Coradoc::Element::Base#simplify_block_content

Defined in:
lib/coradoc/element/base.rb

#simplify_block_content(content) ⇒ Object

The idea here, is that HTML content generators may often introduce a lot of unnecessary markup, that only makes sense in the HTML+CSS context. The idea is that certain cases can be simplified, making it so that the result is equivalent, but much simpler, allowing us to generate a nicer AsciiDoc syntax for those cases.



9
10
11
12
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
# File 'lib/coradoc/element/base.rb', line 9

def simplify_block_content(content)
  content = Array(content)
  collected_content = []
  content.each do |i|
    case i
    when Coradoc::Element::Section
      return content unless i.safe_to_collapse?

      collected_content << i.anchor if i.anchor

      simplified = simplify_block_content(i.contents)

      if simplified && !simplified.empty?
        collected_content << simplified
      end
    else
      collected_content << i
    end
  end

  collected_content = collected_content.compact

  # We can safely do this optimization only if there's just one other
  # element inside this structure.
  if collected_content.length <= 1
    collected_content
  else
    content
  end
end