Class: Coradoc::Input::Html::Converters::Dl

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/html/input/converters/dl.rb

Constant Summary collapse

INSTANCE =
new

Instance Method Summary collapse

Methods inherited from Base

#extract_leading_trailing_whitespace, #extract_text_from_content, #extract_title, #node_has_ancestor?, #textnode_after_start_with?, #textnode_before_end_with?, #treat_children_coradoc, #treat_coradoc, #unconstrained_after?, #unconstrained_before?

Instance Method Details

#process_dl(node, state = {}) ⇒ Object



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
# File 'lib/coradoc/html/input/converters/dl.rb', line 30

def process_dl(node, state = {})
  groups = []
  current = { name: [], value: [] }

  seen_dd = false
  child = node.at_xpath('*[1]')
  grandchild = nil
  until child.nil?
    if child.name == 'div'
      grandchild = child.at_xpath('*[1]')
      until grandchild.nil?
        groups, current, seen_dd = process_dt_or_dd(
          groups,
          current,
          seen_dd,
          grandchild,
          state
        )
        grandchild = grandchild.at_xpath('following-sibling::*[1]')
      end
    elsif %w[dt dd].include?(child.name)
      groups, current, seen_dd = process_dt_or_dd(
        groups,
        current,
        seen_dd,
        child,
        state
      )
    end
    child = child.at_xpath('following-sibling::*[1]')
    groups << current if current[:name].any? && current[:value].any?
  end
  groups
end

#process_dt_or_dd(groups, current, seen_dd, subnode, state = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coradoc/html/input/converters/dl.rb', line 65

def process_dt_or_dd(groups, current, seen_dd, subnode, state = {})
  if subnode.name == 'dt'
    if seen_dd
      current = { name: [], value: [] }
      seen_dd = false
    end
    current[:name] += treat_children_coradoc(subnode, state)
  elsif subnode.name == 'dd'
    current[:value] += treat_children_coradoc(subnode, state)
    seen_dd = true
  end
  [groups, current, seen_dd]
end

#to_coradoc(node, state = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/coradoc/html/input/converters/dl.rb', line 10

def to_coradoc(node, state = {})
  items = process_dl(node, state)

  # Convert items to CoreModel::ListItem objects
  # For definition lists, term goes in content, definition goes in children
  list_items = items.map do |item|
    term_text = extract_text_from_content(item[:name])
    Coradoc::CoreModel::ListItem.new(
      content: term_text,
      children: item[:value]
    )
  end

  # Use CoreModel::ListBlock with marker_type "definition"
  Coradoc::CoreModel::ListBlock.new(
    marker_type: 'definition',
    items: list_items
  )
end