Class: Coradoc::Input::HTML::Converters::A

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

Instance Method Summary collapse

Methods inherited from Base

#convert, #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

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



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

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