Module: Mml::VersionedParser

Included in:
V2, V3, V4
Defined in:
lib/mml/versioned_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(input, namespace_exist: true, context: Mml::UNSPECIFIED_CONTEXT, register: nil) ⇒ Object

Shared parse entrypoint for versioned modules.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mml/versioned_parser.rb', line 6

def parse(input, namespace_exist: true,
          context: Mml::UNSPECIFIED_CONTEXT, register: nil)
  context_id = parse_context_id(context, register)
  root_class = Lutaml::Model::GlobalContext.resolve_type(
    :math,
    context_id,
  )

  root_class.from_xml(
    xml_input(input, namespace_exist),
    register: context_id,
  )
end

#parse_context_id(context, register) ⇒ Object

Version modules keep their own default context id.



21
22
23
24
25
26
27
28
# File 'lib/mml/versioned_parser.rb', line 21

def parse_context_id(context, register)
  Mml::ContextOptions.normalize_context_option(
    context: context,
    register: register,
    default_context: self::Configuration.default_context_id,
    warning_source: "#{name}.parse",
  )
end

#xml_input(input, namespace_exist) ⇒ Object

Inject the MathML namespace into the raw XML string before parsing. This avoids a double parse-serialize cycle (previously the code used Moxml to parse → add namespace → serialize → parse again).

String-level injection is safe for well-formed MathML: the root element is always <math>. For malformed input, the subsequent from_xml parse will raise a proper error.



37
38
39
40
41
# File 'lib/mml/versioned_parser.rb', line 37

def xml_input(input, namespace_exist)
  return input if namespace_exist

  inject_namespace(input, self::Namespace.uri)
end