Module: Metanorma::Core::Isodoc

Defined in:
lib/metanorma/core/isodoc.rb

Overview

Construction and initialisation of an isodoc converter instance for a given Metanorma flavor. Two entry points:

  • Isodoc.resolve_converter — pick the right converter class for the flavor + output stage (presentation XML or HTML), instantiate it, and return a fresh, uninitialised converter.

  • Isodoc.init — wire i18n, metadata, and xref state onto an existing converter, returning it ready for use.

Callers usually need both: init(resolve_converter(flavor), …). The collection layer’s Util::isodoc_create is the canonical wrapper around that pair.

Defined Under Namespace

Classes: EmptyNode

Class Method Summary collapse

Class Method Details

.create(flavor, lang:, script:, locale: nil, i18nyaml: nil, xml: nil, localdir: nil, presxml: true) ⇒ Object

Convenience wrapper combining resolve_converter and init: resolve the converter for flavor and immediately initialise it with the supplied i18n / metadata kwargs. Most callers want this one-stop helper rather than the two-step form.

Parameters:

  • flavor (Symbol, String)

    flavor or taste name; resolved via FlavorLoader.load_flavor.

  • lang (String)

    BCP-47 language tag (e.g. “en”).

  • script (String)

    ISO-15924 script tag (e.g. “Latn”).

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

    optional BCP-47 locale tag.

  • i18nyaml (Hash, String, nil) (defaults to: nil)

    either a parsed i18n hash or a path to a YAML file. Forwarded to init_i18n.

  • xml (Nokogiri::XML::Node, nil) (defaults to: nil)

    if supplied, converter.info is invoked against it for metadata priming.

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

    wired into converter.meta and converter.xrefs.klass for relative file-lookup resolution.

  • presxml (Boolean) (defaults to: true)

    if true (default), return the flavor’s presentation-XML converter; otherwise its HTML converter.

Returns:

  • (Object)

    a fully initialised IsoDoc-style converter.

See Also:



123
124
125
126
127
128
# File 'lib/metanorma/core/isodoc.rb', line 123

def self.create(flavor, lang:, script:, locale: nil, i18nyaml: nil,
                xml: nil, localdir: nil, presxml: true)
  conv = resolve_converter(flavor, presxml: presxml)
  init(conv, lang: lang, script: script, locale: locale,
             i18nyaml: i18nyaml, xml: xml, localdir: localdir)
end

.init(converter, lang:, script:, locale: nil, i18nyaml: nil, xml: nil, localdir: nil) ⇒ Object

Initialise the i18n / metadata / xref state on an existing converter. Mutates the converter in place and returns it.

The converter must already implement the standard Metanorma converter contract: #init_i18n, #i18n_init, #metadata_init, #meta, #xref_init, #xrefs, and (optionally) #info.

Parameters:

  • converter (Object)

    An IsoDoc-style converter instance, typically returned by resolve_converter.

  • lang (String)

    BCP-47 language tag (e.g. “en”).

  • script (String)

    ISO-15924 script tag (e.g. “Latn”).

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

    Optional BCP-47 locale tag for region-specific overrides.

  • i18nyaml (Hash, String, nil) (defaults to: nil)

    Either a parsed i18n hash or a path to a YAML file. Forwarded to init_i18n.

  • xml (Nokogiri::XML::Node, nil) (defaults to: nil)

    If supplied, converter.info is invoked against it so document-level metadata can be primed.

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

    If supplied, wired into both converter.meta and converter.xrefs.klass so file lookups resolve relative to the right directory.

Returns:

  • (Object)

    the same converter, fully initialised.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/metanorma/core/isodoc.rb', line 60

def self.init(converter, lang:, script:, locale: nil,
              i18nyaml: nil, xml: nil, localdir: nil)
  converter.init_i18n(i18nyaml: i18nyaml, language: lang,
                      script: script, locale: locale)
  i18n = converter.i18n_init(lang, script, locale, i18nyaml)
  converter.(lang, script, locale, i18n)
  converter.meta.localdir = localdir if localdir
  converter.xref_init(lang, script, nil, i18n, {})
  converter.xrefs.klass.meta = converter.meta
  converter.xrefs.klass.localdir = localdir if localdir
  converter.info(xml, nil) if xml
  converter
end

.resolve_converter(flavor, presxml: true) ⇒ Object

Resolve the right IsoDoc converter for a given Metanorma flavor and output stage, and return a fresh, uninitialised instance of it. The flavor’s gem (e.g. metanorma-iso) is autoloaded via FlavorLoader.load_flavor if it is not already registered.

Parameters:

  • flavor (Symbol, String)

    Metanorma flavor (e.g. :iso, :standoc) or a taste name resolvable to one.

  • presxml (Boolean) (defaults to: true)

    If true (default), return the flavor’s presentation-XML converter; otherwise its HTML converter.

Returns:

  • (Object)

    An IsoDoc-style converter instance, ready to pass to init.

Raises:

  • (RuntimeError)

    If no Asciidoctor converter is registered for the resolved flavor, or if the converter does not support the requested output stage.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metanorma/core/isodoc.rb', line 89

def self.resolve_converter(flavor, presxml: true)
  resolved = Metanorma::Core::FlavorLoader.load_flavor(flavor)
  conv_class = ::Asciidoctor::Converter.for(resolved.to_s) ||
    ::Asciidoctor::Converter.for(resolved)
  conv_class or
    raise "No Asciidoctor converter registered for #{resolved}"
  conv_instance = conv_class.new(resolved.to_s, {})
  method_name = presxml ? :presentation_xml_converter : :html_converter
  conv_instance.respond_to?(method_name) or
    raise "Flavor #{resolved} does not support " \
          "#{presxml ? 'presentation XML' : 'HTML'} conversion"
  conv_instance.send(method_name, EmptyNode.new)
end