Class: Uniword::Hyperlink

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/hyperlink.rb

Overview

Hyperlink convenience wrapper for external and internal links.

Converts to Wordprocessingml::Hyperlink model objects. For external links, properly assigns an rId that references a relationship in document.xml.rels — not the raw URL string.

Examples:

External hyperlink

link = Hyperlink.new(url: 'https://example.com', text: 'Click here')
link.external?  # => true

Internal hyperlink (bookmark)

link = Hyperlink.new(anchor: 'section1', text: 'Go to section')
link.internal?  # => true

Constant Summary collapse

REL_TYPE =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, anchor: nil, text: nil, tooltip: nil) ⇒ Hyperlink

Returns a new instance of Hyperlink.



22
23
24
25
26
27
# File 'lib/uniword/hyperlink.rb', line 22

def initialize(url: nil, anchor: nil, text: nil, tooltip: nil)
  @url = url
  @anchor = anchor
  @text = text
  @tooltip = tooltip
end

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



20
21
22
# File 'lib/uniword/hyperlink.rb', line 20

def anchor
  @anchor
end

#textObject (readonly)

Returns the value of attribute text.



20
21
22
# File 'lib/uniword/hyperlink.rb', line 20

def text
  @text
end

#tooltipObject (readonly)

Returns the value of attribute tooltip.



20
21
22
# File 'lib/uniword/hyperlink.rb', line 20

def tooltip
  @tooltip
end

#urlObject (readonly)

Returns the value of attribute url.



20
21
22
# File 'lib/uniword/hyperlink.rb', line 20

def url
  @url
end

Instance Method Details

#external?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/uniword/hyperlink.rb', line 29

def external?
  !url.nil?
end

#internal?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/uniword/hyperlink.rb', line 33

def internal?
  !anchor.nil?
end

#to_hObject



73
74
75
# File 'lib/uniword/hyperlink.rb', line 73

def to_h
  { url: url, anchor: anchor, text: text, tooltip: tooltip }
end

#to_model(allocator: nil) ⇒ Wordprocessingml::Hyperlink

Convert to the underlying Wordprocessingml::Hyperlink model.

When an allocator is provided, external links get a proper rId registered as a hyperlink relationship. Without an allocator, falls back to setting id to the raw URL (legacy behavior).

Parameters:

  • allocator (Docx::IdAllocator, nil) (defaults to: nil)

    ID allocator for rId assignment

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/uniword/hyperlink.rb', line 45

def to_model(allocator: nil)
  model = Wordprocessingml::Hyperlink.new

  if url
    if allocator
      r_id = allocator.alloc_rid(
        target: url,
        type: REL_TYPE,
        target_mode: "External",
      )
      model.id = r_id
    else
      model.id = url
    end
  end

  model.anchor = anchor if anchor
  model.tooltip = tooltip if tooltip

  if text
    run = Wordprocessingml::Run.new
    run.text = Wordprocessingml::Text.cast(text)
    model.runs << run
  end

  model
end