Module: Metanorma::Html::Renderers::ElementOrderTraversal

Defined in:
lib/metanorma/html/renderers/element_order_traversal.rb

Overview

Builds the reverse lookup { xml_element_name => attribute_name } from a node's xml mapping. Shared between walkers that iterate element_order: InlineRenderer#walk_ordered (renders in document order) and BaseRenderer#extract_plain_text (extracts text).

Both walkers previously reimplemented this hash construction inline; the construction is the genuinely-shared traversal concern. Per-element handling (recursion, span emission, tab/br spacing) differs and stays in each walker.

Class Method Summary collapse

Class Method Details

.build_map(xml_mapping) ⇒ Object

Returns { xml_element_name => attr_name } including both symbol and string keys for each mapped element, so callers can look up by either el.name form.



35
36
37
38
39
40
41
42
43
44
# File 'lib/metanorma/html/renderers/element_order_traversal.rb', line 35

def build_map(xml_mapping)
  {}.tap do |map|
    xml_mapping.mapping_elements_hash.each_value do |rule_or_array|
      Array(rule_or_array).each do |rule|
        map[rule.name] = rule.to
        map[rule.name.to_s] = rule.to if rule.name.is_a?(Symbol)
      end
    end
  end
end

.element_to_attr_map(xml_mapping) ⇒ Object



23
24
25
# File 'lib/metanorma/html/renderers/element_order_traversal.rb', line 23

def element_to_attr_map(xml_mapping)
  @cache[xml_mapping] ||= build_map(xml_mapping)
end

.reset_cache!Object

Test/benchmark hook: drop all memoized maps.



28
29
30
# File 'lib/metanorma/html/renderers/element_order_traversal.rb', line 28

def reset_cache!
  @cache.clear
end