Class: Uniword::Quality::LinkValidationRule

Inherits:
QualityRule
  • Object
show all
Defined in:
lib/uniword/quality/rules/link_validation_rule.rb

Overview

Checks for broken or invalid links.

Responsibility: Validate hyperlinks in document. Single Responsibility - only checks link validity.

Validates:

  • Internal links point to valid bookmarks

  • External links have proper format

  • Links are not broken (optional network check)

Examples:

Configuration

link_validation:
  enabled: true
  check_internal: true
  check_external: false

Instance Attribute Summary

Attributes inherited from QualityRule

#config, #enabled

Instance Method Summary collapse

Methods inherited from QualityRule

#enabled?, #name

Constructor Details

#initialize(config = {}) ⇒ LinkValidationRule

Returns a new instance of LinkValidationRule.



21
22
23
24
25
# File 'lib/uniword/quality/rules/link_validation_rule.rb', line 21

def initialize(config = {})
  super
  @check_internal = @config.fetch(:check_internal, true)
  @check_external = @config.fetch(:check_external, false)
end

Instance Method Details

#check(document) ⇒ Array<QualityViolation>

Check document for link validation violations

Parameters:

  • document (Document)

    The document to check

Returns:



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
62
63
64
65
# File 'lib/uniword/quality/rules/link_validation_rule.rb', line 31

def check(document)
  violations = []

  link_count = 0
  document.paragraphs.each_with_index do |para, para_index|
    para.hyperlinks.each do |link|
      link_count += 1

      # Check internal links (anchors/bookmarks)
      if @check_internal && internal_link?(link) && !valid_internal_link?(
        link, document
      )
        violations << create_violation(
          severity: :error,
          message: "Internal link #{link_count} references non-existent bookmark '#{link.anchor}'",
          location: "Paragraph #{para_index + 1}, Link #{link_count}",
          element: link,
        )
      end

      # Check external links
      url = link_url(link)
      next unless @check_external && external_link?(link) && !valid_url?(url)

      violations << create_violation(
        severity: :warning,
        message: "External link #{link_count} has invalid URL format: '#{url}'",
        location: "Paragraph #{para_index + 1}, Link #{link_count}",
        element: link,
      )
    end
  end

  violations
end