Class: Uniword::Transformation::HyperlinkTransformationRule

Inherits:
TransformationRule show all
Defined in:
lib/uniword/transformation/hyperlink_transformation_rule.rb

Overview

Transformation rule for Hyperlink elements.

Responsibility: Transform Hyperlink objects between DOCX and MHTML formats. Single Responsibility - handles only Hyperlink transformations.

Transforms hyperlinks including URL, anchor, tooltip, and contained runs.

Examples:

Transform DOCX hyperlink to MHTML

rule = HyperlinkTransformationRule.new(source_format: :docx, target_format: :mhtml)
mhtml_link = rule.transform(docx_link)

Instance Attribute Summary

Attributes inherited from TransformationRule

#source_format, #target_format

Instance Method Summary collapse

Methods inherited from TransformationRule

#initialize

Constructor Details

This class inherits a constructor from Uniword::Transformation::TransformationRule

Instance Method Details

#matches?(element_type:, source_format:, target_format:) ⇒ Boolean

Check if this rule matches the transformation request

Parameters:

  • element_type (Class)

    The element class

  • source_format (Symbol)

    Source format

  • target_format (Symbol)

    Target format

Returns:

  • (Boolean)

    true if matches



22
23
24
25
26
# File 'lib/uniword/transformation/hyperlink_transformation_rule.rb', line 22

def matches?(element_type:, source_format:, target_format:)
  element_type == Hyperlink &&
    source_format == @source_format &&
    target_format == @target_format
end

#transform(source_link) ⇒ Hyperlink

Transform a hyperlink from source format to target format

Creates new Hyperlink with same URL/anchor and transformed runs.

Parameters:

  • source_link (Hyperlink)

    Source hyperlink

Returns:

Raises:

  • (ArgumentError)

    if source_link is not a Hyperlink



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/uniword/transformation/hyperlink_transformation_rule.rb', line 35

def transform(source_link)
  validate_element_type(source_link, Hyperlink)

  # Create new hyperlink with core properties
  target_link = Hyperlink.new(
    url: source_link.url,
    anchor: source_link.anchor,
    text: source_link.text,
    tooltip: source_link.tooltip,
    relationship_id: source_link.relationship_id,
  )

  # Transform runs within hyperlink
  transform_runs(source_link, target_link)

  target_link
end