Module: Latexmath::Unwrap

Extended by:
Parsanol::Atoms::CanFlatten
Defined in:
lib/latexmath/parser/unwrap.rb

Overview

The inverse of the grammar's shape normalization (option F), for the raw-tree boundary and parity replay only.

The generator only ADDS captures, listed in grammars/latex.wrappers.json as => kind. Unwrap replaces each wrapper by the value legacy produced there and re-runs Parslet's own flattening algebra (Parsanol::Atoms::CanFlatten#merge_fold / #flatten_repetition) over the parts, bottom-up:

hash   entries are split, in order, into runs of ordinary keys (one hash
     part each) and wrapper keys (one part each, by kind):
       text       -> the wrapped value (nil / [] -> "", legacy unnamed)
       empty      -> "" (String)
       list       -> the list ([] -> "")
       nil        -> nil
       text_list  -> the text; "" (or []) -> [] (legacy named, empty)
       items      -> the list, unchanged
     then folded with merge_fold, as legacy folded the sequence.
     A hash of nil-kind wrappers only is nil.
array  elements are unwrapped, then flatten_repetition (named).

Constant Summary collapse

PATH =
File.expand_path("../../../grammars/latex.wrappers.json", __dir__)

Class Method Summary collapse

Class Method Details

.call(value, wrappers = self.wrappers) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/latexmath/parser/unwrap.rb', line 45

def call(value, wrappers = self.wrappers)
  case value
  when Hash then unwrap_hash(value, wrappers)
  when Array then Unwrap.send(:flatten_repetition, value.map { |v| call(v, wrappers) }, true)
  else value
  end
end

.load(path) ⇒ Object



41
42
43
# File 'lib/latexmath/parser/unwrap.rb', line 41

def load(path)
  JSON.parse(File.read(path, encoding: "UTF-8")).fetch("wrappers")
end

.wrappers ⇒ Object

The wrapper table of the bundled grammar.



37
38
39
# File 'lib/latexmath/parser/unwrap.rb', line 37

def wrappers
  @lock.synchronize { @wrappers ||= load(PATH).freeze }
end