Class: Vectory::SvgMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/svg_mapping.rb

Overview

Processes SVG mapping in XML documents.

SvgMapping integrates SVG content into XML documents by:

  • Extracting SVG from image tags or inline <svg> elements

  • Processing <target> elements to build link mappings

  • Applying ID suffixes for uniqueness in multi-document scenarios

  • Simplifying svgmap structure for final output

ID Suffixing

SVG IDs are suffixed in two stages to ensure uniqueness:

  1. **ID suffix** (optional) - Derived from document/container identity Provides cross-document uniqueness (e.g., <id>_ISO_17301-1_2016)

  2. **Index suffix** - Derived from svgmap position in document Provides multi-svgmap uniqueness (e.g., <id>_000000000)

Final ID: <original_id><id_suffix><index_suffix> Example: fig1_ISO_17301-1_2016_000000000

Examples:

Basic usage

xml_string = Vectory::SvgMapping.from_path("doc.xml").to_xml

With ID suffix for multi-document uniqueness

mapping = Vectory::SvgMapping.new(doc, "", id_suffix: "_ISO_17301-1_2016")
xml_string = mapping.call.to_xml

Defined Under Namespace

Classes: Namespace

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, local_directory = "", id_suffix: nil) ⇒ SvgMapping

Initializes a new SvgMapping processor.

Parameters:

  • doc (Nokogiri::XML::Document)

    The document containing svgmap elements

  • local_directory (String) (defaults to: "")

    Directory for resolving relative file paths

  • id_suffix (String, nil) (defaults to: nil)

    Optional suffix derived from document/container identity. Applied before the index suffix to provide cross-document uniqueness. Example: “_ISO_17301-1_2016”



82
83
84
85
86
# File 'lib/vectory/svg_mapping.rb', line 82

def initialize(doc, local_directory = "", id_suffix: nil)
  @doc = doc
  @local_directory = local_directory
  @id_suffix = id_suffix
end

Class Method Details

.from_path(path) ⇒ Vectory::SvgMapping

Creates an SvgMapping from an XML file path.

Parameters:

  • path (String)

    Path to the XML file

Returns:



63
64
65
# File 'lib/vectory/svg_mapping.rb', line 63

def self.from_path(path)
  new(Nokogiri::XML(File.read(path)))
end

.from_xml(xml) ⇒ Vectory::SvgMapping

Creates an SvgMapping from an XML string.

Parameters:

  • xml (String)

    XML content

Returns:



71
72
73
# File 'lib/vectory/svg_mapping.rb', line 71

def self.from_xml(xml)
  new(Nokogiri::XML(xml))
end

Instance Method Details

#callNokogiri::XML::Document

Processes all svgmap elements in the document.

Returns:

  • (Nokogiri::XML::Document)

    The processed document



91
92
93
94
95
96
97
98
99
# File 'lib/vectory/svg_mapping.rb', line 91

def call
  @namespace = Namespace.new(@doc)

  @doc.xpath(@namespace.ns("//svgmap")).each_with_index do |svgmap, index|
    process_svgmap(svgmap, index)
  end

  @doc
end

#to_xmlString

Processes and returns XML string.

Returns:

  • (String)

    XML representation of processed document



104
105
106
# File 'lib/vectory/svg_mapping.rb', line 104

def to_xml
  call.to_xml
end