class IsoDoc::HtmlFunction::MathvariantToPlain

Class for recursively converting mathvariant text into plain text symbols

Constants

MATHML
MATHVARIANT_SPECIAL_CASE_MAPPINGS_1
MATHVARIANT_SPECIAL_CASE_MAPPINGS_2
MATHVARIANT_TO_PLANE_MAPPINGS

Attributes

docxml[R]

Public Class Methods

new(docxml) click to toggle source

@param [Nokogiri::Document] docxml

# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 37
def initialize(docxml)
  @docxml = docxml
end

Public Instance Methods

convert() click to toggle source
# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 41
def convert
  docxml.xpath("//m:math", MATHML).each do |elem|
    next if nothing_to_style(elem)

    mathml1(elem)
  end
  docxml
end

Private Instance Methods

mathml1(base_elem) click to toggle source
# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 57
def mathml1(base_elem)
  MATHVARIANT_SPECIAL_CASE_MAPPINGS_1
    .merge(MATHVARIANT_SPECIAL_CASE_MAPPINGS_2)
    .merge(MATHVARIANT_TO_PLANE_MAPPINGS)
    .each_pair do |mathvariant_list, plain_font|
      base_elem.xpath(mathvariant_xpath(mathvariant_list)).each do |elem|
        to_plane(elem, plain_font)
      end
    end
end
mathvariant_xpath(list) click to toggle source
# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 68
def mathvariant_xpath(list)
  list
    .map { |variant| "//*[@mathvariant = '#{variant}']" }
    .join
end
nothing_to_style(elem) click to toggle source
# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 52
def nothing_to_style(elem)
  !elem.at("./*[@mathvariant][not(@mathvariant = 'normal')]"\
           "[not(@mathvariant = 'italic')]")
end
to_plane(elem, font) click to toggle source
# File lib/isodoc/html_function/mathvariant_to_plain.rb, line 74
def to_plane(elem, font)
  elem.traverse do |n|
    next unless n.text?

    n.replace(Plane1Converter.conv(HTMLEntities.new.decode(n.text), font))
  end
  elem
end