Class: Uniword::Batch::ValidateLinksStage

Inherits:
ProcessingStage show all
Defined in:
lib/uniword/batch/stages/validate_links_stage.rb

Overview

Processing stage that validates document links.

Responsibility: Check and report broken internal and external links. Single Responsibility - only handles link validation.

Examples:

Use in pipeline

stage = ValidateLinksStage.new(
  check_internal: true,
  check_external: false,
  report_broken: true
)
document = stage.process(document, context)

Instance Attribute Summary

Attributes inherited from ProcessingStage

#enabled, #options

Instance Method Summary collapse

Methods inherited from ProcessingStage

#enabled?, #name

Constructor Details

#initialize(options = {}) ⇒ ValidateLinksStage

Initialize validate links stage

Parameters:

  • options (Hash) (defaults to: {})

    Stage options

Options Hash (options):

  • :check_internal (Boolean)

    Check internal links

  • :check_external (Boolean)

    Check external links

  • :check_bookmarks (Boolean)

    Check bookmark references

  • :report_broken (Boolean)

    Report broken links

  • :broken_link_action (String)

    Action for broken links (‘report’, ‘remove’, ‘disable’)



26
27
28
29
30
31
32
33
# File 'lib/uniword/batch/stages/validate_links_stage.rb', line 26

def initialize(options = {})
  super
  @check_internal = options.fetch(:check_internal, true)
  @check_external = options.fetch(:check_external, false)
  @check_bookmarks = options.fetch(:check_bookmarks, true)
  @report_broken = options.fetch(:report_broken, true)
  @broken_link_action = options.fetch(:broken_link_action, "report")
end

Instance Method Details

#descriptionString

Get stage description

Returns:

  • (String)

    Description



74
75
76
# File 'lib/uniword/batch/stages/validate_links_stage.rb', line 74

def description
  "Validate document links"
end

#process(document, context = {}) ⇒ Document

Process document to validate links

Parameters:

  • document (Document)

    Document to process

  • context (Hash) (defaults to: {})

    Processing context

Returns:

  • (Document)

    Processed document



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
66
67
68
69
# File 'lib/uniword/batch/stages/validate_links_stage.rb', line 40

def process(document, context = {})
  log "Validating links in #{context[:filename]}"

  broken_links = []

  # Collect all hyperlinks
  hyperlinks = collect_hyperlinks(document)

  # Validate each hyperlink
  hyperlinks.each do |link|
    next unless should_validate_link?(link)

    if link_is_broken?(link, document)
      broken_links << link
      handle_broken_link(link, document)
    end
  end

  # Report results
  if @report_broken && broken_links.any?
    log "Found #{broken_links.size} broken link(s)", level: :warn
    broken_links.each do |link|
      log "  - #{link[:type]}: #{link[:target]}", level: :warn
    end
  else
    log "All links valid"
  end

  document
end