Class: Coradoc::Element::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/element/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.children_accessorsObject



60
61
62
# File 'lib/coradoc/element/base.rb', line 60

def self.children_accessors
  @children || []
end

.declare_children(*children) ⇒ Object



40
41
42
# File 'lib/coradoc/element/base.rb', line 40

def self.declare_children(*children)
  @children = (@children || []).dup + children
end

.visit(element) {|element, :post| ... } ⇒ Object

Yields:

  • (element, :post)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coradoc/element/base.rb', line 44

def self.visit(element, &block)
  element = yield element, :pre
  element = if element.respond_to? :visit
              element.visit(&block)
            elsif element.is_a? Array
              element.map { |child| visit(child, &block) }.flatten.compact
            elsif element.is_a? Hash
              element.to_h do |k, v|
                [visit(k, &block), visit(v, &block)]
              end
            else
              element
            end
  yield element, :post
end

Instance Method Details

#children_accessorsObject



64
65
66
# File 'lib/coradoc/element/base.rb', line 64

def children_accessors
  self.class.children_accessors
end

#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

#visit(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/coradoc/element/base.rb', line 68

def visit(&block)
  children_accessors.each do |accessor|
    child = public_send(accessor)
    result = self.class.visit(child, &block)
    if result != child
      public_send(:"#{accessor}=", result)
    end
  end
  self
end