Module: Unitsml::Xml::Formatter
- Defined in:
- lib/unitsml/xml/formatter.rb
Overview
Owns the canonical post-processing applied to XML emitted by
Unitsml::Model::* before it is returned to callers. Previously
inlined as repeated .gsub chains at six call sites in
Unitsml::Utility — this module is the single source of truth.
The chain has two logical phases:
1. Decode XML entities that lutaml-model emitted but UnitsML
consumers expect as raw characters (`<` → `<`, etc.).
2. Re-encode Unicode mathematical symbols (minus, dot operator)
as numeric character references for downstream XML consumers
that aren't guaranteed UTF-8 clean transport.
Constant Summary collapse
- ENTITY_DECODINGS =
{ "<" => "<", ">" => ">", "&" => "&", }.freeze
- UNIT_SYMBOL_ENCODINGS =
{ "−" => "−", "⋅" => "⋅", }.freeze
Class Method Summary collapse
- .decode_entities(xml) ⇒ Object
- .encode_unit_symbols(xml) ⇒ Object
-
.format_prefix(xml) ⇒ Object
Minimal post-processing for
XML: only the ampersand decode is needed (UnitsML prefix symbols never contain the other reserved characters or Unicode math symbols). -
.format_unit(xml) ⇒ Object
Full post-processing for UnitsML
XML: entity decode followed by Unicode → numeric-reference encoding.
Class Method Details
.decode_entities(xml) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/unitsml/xml/formatter.rb', line 44 def decode_entities(xml) buffer = unfrozen(xml).force_encoding("UTF-8") ENTITY_DECODINGS.reduce(buffer) do |acc, (from, to)| acc.gsub(from, to) end end |
.encode_unit_symbols(xml) ⇒ Object
51 52 53 54 55 |
# File 'lib/unitsml/xml/formatter.rb', line 51 def encode_unit_symbols(xml) UNIT_SYMBOL_ENCODINGS.reduce(xml) do |buffer, (from, to)| buffer.gsub(from, to) end end |
.format_prefix(xml) ⇒ Object
Minimal post-processing for
40 41 42 |
# File 'lib/unitsml/xml/formatter.rb', line 40 def format_prefix(xml) unfrozen(xml).force_encoding("UTF-8").gsub("&", "&") end |
.format_unit(xml) ⇒ Object
Full post-processing for UnitsML
32 33 34 35 |
# File 'lib/unitsml/xml/formatter.rb', line 32 def format_unit(xml) decoded = decode_entities(xml) encode_unit_symbols(decoded) end |