Class: Metanorma::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/metanorma/registry/registry.rb

Overview

Central registry for managing Metanorma processors, flavors, and their aliases

This singleton class provides a centralized registry for:

  • Metanorma processors (document format processors)

  • Flavor aliases (mapping legacy/alternative names to canonical flavors)

  • Taste configurations (via TasteRegister integration)

The registry maintains backward compatibility with legacy flavor names while integrating with the modern TasteRegister system for dynamic taste management.

Examples:

Basic usage

registry = Metanorma::Registry.instance

# Register a processor
registry.register(MyProcessor)

# Look up flavor aliases
registry.alias(:csd)  # => :cc

# Register custom aliases
registry.register_alias(:my_flavor, :iso)

Finding processors

processor = registry.find_processor(:iso)
formats = registry.output_formats
backends = registry.supported_backends

Constant Summary collapse

DEFAULT_ALIASES =

Default legacy aliases for backward compatibility Maps old flavor names to their canonical equivalents

{ csd: :cc, m3d: :m3aawg, mpfd: :mpfa, csand: :csa }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Initialize the registry with processors, tastes, and aliases

Sets up the registry by:

  1. Initializing empty processors hash

  2. Connecting to the TasteRegister instance

  3. Initializing custom aliases with defaults



51
52
53
54
55
# File 'lib/metanorma/registry/registry.rb', line 51

def initialize
  @processors = {}
  @tastes = Metanorma::TasteRegister.instance
  @custom_aliases = DEFAULT_ALIASES.dup
end

Instance Attribute Details

#processorsObject (readonly)

Returns the value of attribute processors.



39
40
41
# File 'lib/metanorma/registry/registry.rb', line 39

def processors
  @processors
end

#tastesObject (readonly)

Returns the value of attribute tastes.



39
40
41
# File 'lib/metanorma/registry/registry.rb', line 39

def tastes
  @tastes
end

Instance Method Details

#alias(flavour) ⇒ Symbol?

Look up the canonical flavor name for a given alias

Checks aliases in priority order:

  1. Custom registered aliases (highest priority)

  2. Taste-based aliases from TasteRegister

  3. Returns nil if no alias found

Examples:

registry.alias(:csd)  # => :cc (from DEFAULT_ALIASES)
registry.alias(:icc)  # => :iso (from taste configuration)
registry.alias(:unknown)  # => nil
registry.alias(nil)  # => nil

Parameters:

  • flavour (Symbol, String, nil)

    The flavor alias to look up

Returns:

  • (Symbol, nil)

    The canonical flavor name, or nil if no alias exists



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/metanorma/registry/registry.rb', line 72

def alias(flavour)
  return nil if flavour.nil?

  flavour_sym = flavour.to_sym

  # Check custom aliases first (includes defaults)
  return @custom_aliases[flavour_sym] if @custom_aliases.key?(flavour_sym)

  # Then check taste aliases
  taste_aliases = @tastes.aliases
  taste_aliases[flavour_sym]
end

#find_processor(short) ⇒ Metanorma::Processor?

Find a registered processor by its short name

Examples:

processor = registry.find_processor(:iso)
processor = registry.find_processor("iso")

Parameters:

  • short (Symbol, String)

    The short name of the processor to find

Returns:



143
144
145
# File 'lib/metanorma/registry/registry.rb', line 143

def find_processor(short)
  @processors[short.to_sym]
end

#output_formatsHash<Symbol, Hash>

Get output formats supported by each registered processor

Examples:

registry.output_formats
# => { iso: { html: "html", pdf: "pdf", ... }, iec: { ... }, ... }

Returns:

  • (Hash<Symbol, Hash>)

    Hash mapping processor names to their output formats



164
165
166
167
168
169
# File 'lib/metanorma/registry/registry.rb', line 164

def output_formats
  @processors.inject({}) do |acc, (k, v)|
    acc[k] = v.output_formats
    acc
  end
end

#register(processor) ⇒ Array<Symbol>

Register a Metanorma processor

Registers a processor class and automatically creates aliases for all its short names. The last short name is considered the canonical name.

Examples:

registry.register(Metanorma::ISO::Processor)
# Registers processor and creates aliases for all its short names

Parameters:

  • processor (Class)

    A processor class that inherits from Metanorma::Processor

Returns:

  • (Array<Symbol>)

    Array of short names for the processor

Raises:

  • (Error)

    If the processor doesn’t inherit from Metanorma::Processor



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/metanorma/registry/registry.rb', line 115

def register(processor)
  unless processor < ::Metanorma::Processor
    raise Error, "Processor must inherit from Metanorma::Processor"
  end

  # The last short name is the canonical name
  processor_instance = processor.new
  short_names = Array(processor_instance.short)
  canonical_name = short_names.last

  # Register processor with canonical name
  @processors[canonical_name] = processor_instance

  # Create aliases for all short names pointing to canonical name
  short_names.each { |name| @custom_aliases[name] = canonical_name }

  Util.log("[metanorma] processor \"#{short_names.first}\" registered", :info)
  short_names
end

#register_alias(alias_name, target_flavor) ⇒ Object

Register a custom alias mapping

Allows runtime registration of flavor aliases. Custom aliases take precedence over taste-based aliases, allowing overrides of taste configurations.

Examples:

registry.register_alias(:my_custom, :iso)
registry.alias(:my_custom)  # => :iso

# Override a taste alias
registry.register_alias(:icc, :custom_iso)

Parameters:

  • alias_name (Symbol, String)

    The alias name to register

  • target_flavor (Symbol, String)

    The canonical flavor it should map to



99
100
101
# File 'lib/metanorma/registry/registry.rb', line 99

def register_alias(alias_name, target_flavor)
  @custom_aliases[alias_name.to_sym] = target_flavor.to_sym
end

#root_tagsHash<Symbol, String>

Get XML root tags for processors with Asciidoctor backends

Examples:

registry.root_tags
# => { iso: "iso-standard", iec: "iec-standard", ... }

Returns:

  • (Hash<Symbol, String>)

    Hash mapping processor names to their XML root tags



178
179
180
181
182
183
184
185
186
# File 'lib/metanorma/registry/registry.rb', line 178

def root_tags
  @processors.inject({}) do |acc, (k, v)|
    if v.asciidoctor_backend
      x = Asciidoctor.load nil, { backend: v.asciidoctor_backend }
      acc[k] = x.converter.xml_root_tag
    end
    acc
  end
end

#supported_backendsArray<Symbol>

Get list of all supported backend names

Examples:

registry.supported_backends  # => [:iso, :iec, :itu, ...]

Returns:

  • (Array<Symbol>)

    Array of registered processor backend names



153
154
155
# File 'lib/metanorma/registry/registry.rb', line 153

def supported_backends
  @processors.keys
end