Sha256: fdfa6729ba157ebaf2d8e6f6942348cb89ba92c3ccb0465489c622c764fdf4fe

Contents?: true

Size: 1.93 KB

Versions: 9

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Plurimath
  module Math
    class Symbol < Base
      attr_accessor :value

      def initialize(sym)
        @value = super
      end

      def ==(object)
        object.value == value
      end

      def to_asciimath
        return "" if value.nil?

        symbol = Asciimath::Constants::SYMBOLS.invert[value.strip.to_sym]
        unicodes = Latex::Constants::UNICODE_SYMBOLS.invert
        if value.match?(/&#x[0-9\w]+;/) && symbol.nil? && unicodes[value]
          return unicodes[value].to_s
        end

        symbol ? symbol.to_s : value
      end

      def to_mathml_without_math_tag
        mi_tag = Utility.ox_element("mi")
        return mi_tag if ["{:", ":}"].include?(value)

        unicodes = Mathml::Constants::UNICODE_SYMBOLS
        unicode = unicodes.invert[value]
        if operator?(unicode) || unicode
          mo_value = (unicodes[value] || unicode || value).to_s
          return Utility.ox_element("mo") << mo_value
        end

        mi_tag << value
      end

      def to_latex
        returned = specific_values
        return returned if returned

        special_char = %w[&#x26; &#x23;]
        symbols = Latex::Constants::UNICODE_SYMBOLS.invert

        symbol = symbols[value]
        if Latex::Constants::SYMBOLS[symbol] == :operant
          special_char.include?(value) ? "\\#{symbol}" : symbol
        else
          symbols.key?(value) ? "\\#{symbol}" : value
        end
      end

      def to_html
        value
      end

      def to_omml_without_math_tag
        value
      end

      private

      def operator?(unicode)
        Mathml::Constants::OPERATORS.any? do |d|
          [unicode.to_s, value.strip].include?(d)
        end
      end

      def specific_values
        return "" if ["{:", ":}"].include?(value)

        return "\\#{value}" if ["{", "}"].include?(value) || value == "_"

        return "\\operatorname{if}" if value == "if"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
plurimath-0.3.0 lib/plurimath/math/symbol.rb
plurimath-0.2.9 lib/plurimath/math/symbol.rb
plurimath-0.2.8 lib/plurimath/math/symbol.rb
plurimath-0.2.7 lib/plurimath/math/symbol.rb
plurimath-0.2.6 lib/plurimath/math/symbol.rb
plurimath-0.2.5 lib/plurimath/math/symbol.rb
plurimath-0.2.4 lib/plurimath/math/symbol.rb
plurimath-0.2.3 lib/plurimath/math/symbol.rb
plurimath-0.2.2 lib/plurimath/math/symbol.rb