module Asciimath2UnitsML class Conv def multiplier(x) case x when :space { html: " ", mathml: "" } when :nospace { html: "", mathml: "" } else { html: HTMLEntities.new.encode(x), mathml: "#{HTMLEntities.new.encode(x)}" } end end def render(unit, style) @symbols[unit][style] || unit end def htmlsymbol(units, normalise) units.map do |u| if u[:multiplier] then u[:multiplier] == "*" ? @multiplier[:html] : u[:multiplier] else u[:display_exponent] and exp = "#{u[:display_exponent].sub(/-/, "−")}" base = render(normalise ? @units[u[:unit]].symbolid : u[:unit], :html) "#{u[:prefix]}#{base}#{exp}" end end.join("") end def mathmlsymbol(units, normalise) exp = units.map do |u| if u[:multiplier] then u[:multiplier] == "*" ? @multiplier[:mathml] : "#{u[:multiplier]}" else base = render(normalise ? @units[u[:unit]].symbolid : u[:unit], :mathml) if u[:prefix] base = base.match(//) ? base.sub(//, "#{u[:prefix]}") : "#{u[:prefix]}#{base}" end if u[:display_exponent] exp = "#{u[:display_exponent]}".sub(/-/, "") base = "#{base}#{exp}" end base end end.join("") end def mathmlsymbolwrap(units, normalise) <<~END #{mathmlsymbol(units, normalise)} END end end end