Method: Coradoc::Input::Html::Converters::A#to_coradoc

Defined in:
lib/coradoc/input/html/converters/a.rb

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



8
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
39
40
41
42
43
44
45
# File 'lib/coradoc/input/html/converters/a.rb', line 8

def to_coradoc(node, state = {})
  name  = treat_children(node, state)

  href  = node["href"]
  title = extract_title(node)
  id = node["id"] || node["name"]

  id = id&.gsub(/\s/, "")&.gsub(/__+/, "_")
  id = nil if id&.empty?

  return "" if /^_Toc\d+$|^_GoBack$/.match?(id)

  return Coradoc::Element::Inline::Anchor.new(id) if id

  if href.to_s.start_with?("#")
    href = href.sub(/^#/, "").gsub(/\s/, "").gsub(/__+/, "_")
    return Coradoc::Element::Inline::CrossReference.new(href, name)
  end

  return name if href.to_s.empty?

  ambigous_characters = /[\w.?&#=%;\[\u{ff}-\u{10ffff}]/
  if name&.strip == href
    name = ""
    right_constrain = textnode_after_start_with?(node,
                                                 ambigous_characters)
  end

  out = []
  out << " " if textnode_before_end_with?(node, ambigous_characters)
  out << Coradoc::Element::Inline::Link.new(
    path: href,
    name: name.strip,
    title: title.strip,
    right_constrain: right_constrain,
  )
  out
end