Class: Docbook::Services::ImageResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook/services/image_resolver.rb

Overview

Resolves relative image paths to absolute URLs.

Walks a DocbookMirror JSON structure and converts image src values from relative paths to absolute URLs using the configured strategy.

Usage: resolver = ImageResolver.new( search_dirs: ["/path/to/xml", "/path/to/resources"], strategy: :file_url # or :data_url, :relative ) resolver.resolve(data)

Constant Summary collapse

STRATEGIES =
%i[file_url data_url relative].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_dirs:, strategy: :file_url, base_url: nil) ⇒ ImageResolver

Returns a new instance of ImageResolver.

Parameters:

  • search_dirs (Array<String>)

    directories to search for images

  • strategy (Symbol) (defaults to: :file_url)

    resolution strategy:

    • :file_url — convert to file:// URLs (for local file:// viewing)
    • :data_url — embed as data: URLs (self-contained HTML)
    • :relative — keep as relative paths (for HTTP serving)
  • base_url (String) (defaults to: nil)

    base URL for :relative strategy



28
29
30
31
32
# File 'lib/docbook/services/image_resolver.rb', line 28

def initialize(search_dirs:, strategy: :file_url, base_url: nil)
  @search_dirs = Array(search_dirs)
  @strategy = strategy
  @base_url = base_url
end

Instance Attribute Details

#search_dirsObject (readonly)

Returns the value of attribute search_dirs.



20
21
22
# File 'lib/docbook/services/image_resolver.rb', line 20

def search_dirs
  @search_dirs
end

#strategyObject (readonly)

Returns the value of attribute strategy.



20
21
22
# File 'lib/docbook/services/image_resolver.rb', line 20

def strategy
  @strategy
end

Instance Method Details

#resolve(data) ⇒ Object

Resolve image paths in a DocbookMirror JSON structure (mutates in place).

Parameters:

  • data (Hash)

    the JSON data structure



36
37
38
# File 'lib/docbook/services/image_resolver.rb', line 36

def resolve(data)
  walk(data)
end