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

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

Instance Method Summary collapse

Methods inherited from Base

#convert, #escape_text, #extract_leading_trailing_whitespace, #extract_title, #node_has_ancestor?, #textnode_after_start_with?, #textnode_before_end_with?, #treat, #treat_children, #treat_children_coradoc, #treat_coradoc, #unconstrained_after?, #unconstrained_before?

Instance Method Details

#extract_text_from_content(content) ⇒ Object

Extract text from content array



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/coradoc/html/input/converters/dl.rb', line 79

def extract_text_from_content(content)
  return content if content.is_a?(String)
  return '' if content.nil?

  content.map do |item|
    case item
    when String
      item
    when Coradoc::CoreModel::InlineElement
      item.content.to_s
    when Coradoc::CoreModel::Base
      if item.content
        item.content.to_s
      else
        ''
      end
    else
      item.to_s
    end
  end.join
end

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



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

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



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

def process_dt_or_dd(groups, current, seen_dd, subnode, state = {})
  if subnode.name == 'dt'
    if seen_dd
      # groups << current
      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



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

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