Class: Plurimath::Utility
- Inherits:
-
Object
- Object
- Plurimath::Utility
- Defined in:
- lib/plurimath/utility.rb,
lib/plurimath/utility/intent_encoding.rb
Direct Known Subclasses
Defined Under Namespace
Classes: IntentEncoding
Constant Summary collapse
- UNICODE_REGEX =
%r{&#x[a-zA-Z0-9]+;}
- FONT_STYLES =
{ "double-struck": Math::Function::FontStyle::DoubleStruck, "sans-serif-bold-italic": Math::Function::FontStyle::SansSerifBoldItalic, "sans-serif-italic": Math::Function::FontStyle::SansSerifItalic, "bold-sans-serif": Math::Function::FontStyle::BoldSansSerif, "sans-serif": Math::Function::FontStyle::SansSerif, "bold-fraktur": Math::Function::FontStyle::BoldFraktur, "bold-italic": Math::Function::FontStyle::BoldItalic, "bold-script": Math::Function::FontStyle::BoldScript, mbfitsans: Math::Function::FontStyle::SansSerifBoldItalic, monospace: Math::Function::FontStyle::Monospace, mathfrak: Math::Function::FontStyle::Fraktur, mitsans: Math::Function::FontStyle::SansSerifItalic, mbfsans: Math::Function::FontStyle::BoldSansSerif, mbffrak: Math::Function::FontStyle::BoldFraktur, mathcal: Math::Function::FontStyle::Script, fraktur: Math::Function::FontStyle::Fraktur, mbfscr: Math::Function::FontStyle::BoldScript, mathbb: Math::Function::FontStyle::DoubleStruck, double: Math::Function::FontStyle::DoubleStruck, mathtt: Math::Function::FontStyle::Monospace, mathsf: Math::Function::FontStyle::SansSerif, mathrm: Math::Function::FontStyle::Normal, textrm: Math::Function::FontStyle::Normal, italic: Math::Function::FontStyle::Italic, mathit: Math::Function::FontStyle::Italic, textit: Math::Function::FontStyle::Italic, mathbf: Math::Function::FontStyle::Bold, textbf: Math::Function::FontStyle::Bold, script: Math::Function::FontStyle::Script, normal: Math::Function::FontStyle::Normal, mbfit: Math::Function::FontStyle::BoldItalic, msans: Math::Function::FontStyle::SansSerif, mfrak: Math::Function::FontStyle::Fraktur, mscr: Math::Function::FontStyle::Script, bold: Math::Function::FontStyle::Bold, bbb: Math::Function::FontStyle::DoubleStruck, Bbb: Math::Function::FontStyle::DoubleStruck, mtt: Math::Function::FontStyle::Monospace, cal: Math::Function::FontStyle::Script, mit: Math::Function::FontStyle::Italic, mup: Math::Function::FontStyle::Normal, mbf: Math::Function::FontStyle::Bold, sf: Math::Function::FontStyle::SansSerif, tt: Math::Function::FontStyle::Monospace, fr: Math::Function::FontStyle::Fraktur, rm: Math::Function::FontStyle::Normal, cc: Math::Function::FontStyle::Script, ii: Math::Function::FontStyle::Italic, bb: Math::Function::FontStyle::Bold, bf: Math::Function::FontStyle::Bold, }.freeze
- ALIGNMENT_LETTERS =
{ c: "center", r: "right", l: "left", }.freeze
- UNARY_CLASSES =
%w[ arccos arcsin arctan liminf limsup right sech sinh tanh cosh coth csch left max min sec sin sup deg det dim exp gcd glb lub lcm ker tan cos cot csc ln lg ].freeze
- MUNDER_CLASSES =
%w[ ubrace obrace right max min ].freeze
- PARENTHESIS =
{ "〈": "〉", "⌊": "⌋", "⌈": "⌉", "‖": "‖", "{": "}", "[": "]", "|": "|", "(": ")", "{": "}", "[": "]", }.freeze
- TEXT_CLASSES =
%w[ symbol number text ].freeze
- TABLE_SUPPORTED_ATTRS =
%i[ columnlines rowlines frame ].freeze
- MPADDED_ATTRS =
%i[ height depth width ].freeze
- MGLYPH_ATTRS =
%i[ height width index alt src ].freeze
- UNICODEMATH_MENCLOSE_FUNCTIONS =
{ underline: "bottom", underbar: "bottom", longdiv: "longdiv", xcancel: "updiagonalstrike downdiagonalstrike", bcancel: "updiagonalstrike", ellipse: "circle", circle: "circle", cancel: "downdiagonalstrike", rrect: "roundedbox", rect: "box", }.freeze
- MASK_CLASSES =
{ 1 => 'top', 2 => 'bottom', 4 => 'left', 8 => 'right', 16 => 'horizontalstrike', 32 => 'verticalstrike', 64 => 'downdiagonalstrike', 128 => 'updiagonalstrike' }.freeze
Class Method Summary collapse
- .accent_value(accent, function: false, lang:) ⇒ Object
- .all_symbols_classes(lang) ⇒ Object
- .asciimath_symbol_object(value, lang: :asciimath) ⇒ Object
- .attr_is_accent(attrs, value) ⇒ Object
- .attr_is_function(attrs, value) ⇒ Object
- .base_is_prime?(base) ⇒ Boolean
- .base_is_sub_or_sup?(base) ⇒ Boolean
- .base_recursion(sub_script, sub_recursion) ⇒ Object
- .binary_function_classes(mrow, under: false, lang:) ⇒ Object
- .capitalize(text) ⇒ Object
- .enclosure_attrs(mask) ⇒ Object
- .fenceable_classes(mrow = []) ⇒ Object
- .filter_math_zone_values(value, lang:, intent: false, options: nil) ⇒ Object
- .filter_table_data(table_data) ⇒ Object
- .filter_values(array) ⇒ Object
- .find_pos_chr(fonts_array, key) ⇒ Object
- .frac_values(object) ⇒ Object
- .fractions(numerator, denominator, options = nil) ⇒ Object
- .get_class(text) ⇒ Object
- .get_paren_class(text) ⇒ Object
- .get_symbol_class(text) ⇒ Object
- .get_table_class(text) ⇒ Object
- .hexcode_in_input(field) ⇒ Object
- .html_entity_to_unicode(string) ⇒ Object
- .identity_matrix(size) ⇒ Object
- .join_attr_value(attrs, value, unicode_only: false, lang: :mathml) ⇒ Object
- .latex_table_curly_paren(string) ⇒ Object
- .left_right_objects(paren, function) ⇒ Object
- .math_display_text_objects(object) ⇒ Object
- .mathml_unary_classes(text_array, omml: false, unicode_only: false, lang: nil) ⇒ Object
- .mrow_left_right(mrow = []) ⇒ Object
- .multiscript(values) ⇒ Object
- .nary_fonts(nary, lang:) ⇒ Object
- .nil_to_none_object(value) ⇒ Object
- .notations_to_mask(notations) ⇒ Object
- .organize_options(table_data, column_align) ⇒ Object
- .organize_table(array, column_align: nil, options: nil) ⇒ Object
- .organize_tds(tr_array, column_align, options) ⇒ Object
- .ox_element(node, attributes: [], namespace: "") ⇒ Object
- .paren_able?(arr = [], mrow = []) ⇒ Boolean
- .paren_files ⇒ Object
- .parens_hash(lang, skipables: nil) ⇒ Object
- .populate_function_classes(mrow = [], lang:) ⇒ Object
- .pr_element(main_tag, wi_tag = false, namespace: "") ⇒ Object
- .primes_constants ⇒ Object
- .recursion_fraction(frac, numerator, options) ⇒ Object
- .recursive_sub(sub_script, sub_recursion) ⇒ Object
- .recursive_sup(sup_script, sup_recursion) ⇒ Object
- .rpr_element(wi_tag: false) ⇒ Object
- .sequence_slashed_values(values, lang:) ⇒ Object
- .slashed_values(value) ⇒ Object
- .string_to_html_entity(string) ⇒ Object
- .sub_sup_method?(sub_sup) ⇒ Boolean
- .sup_recursion(sup_script, sup_recursion) ⇒ Object
- .symbol_object(value, lang: nil) ⇒ Object
- .symbol_prime?(obj) ⇒ Boolean
- .symbol_to_text(symbol, lang:, intent: false, options:) ⇒ Object
- .symbol_value(object, value) ⇒ Object
- .symbols_class(string, lang:, table: false) ⇒ Object
- .symbols_files ⇒ Object
- .symbols_hash(lang) ⇒ Object
- .table_options(table_data) ⇒ Object
- .table_separator(separator, value, symbol: "solid") ⇒ Object
- .table_td(object) ⇒ Object
- .td_value(td_object) ⇒ Object
- .td_values(objects, slicer) ⇒ Object
- .ternary_function_classes(mrow) ⇒ Object
- .text_classes(text, lang:) ⇒ Object
- .transform_accents(accents, lang:) ⇒ Object
- .unary_function_classes(mrow, lang:) ⇒ Object
- .unfenced_value(object, paren_specific: false) ⇒ Object
- .unicode_accents(accents, lang: :unicodemath) ⇒ Object
- .unicode_fractions(fractions) ⇒ Object
- .update_nodes(element, nodes) ⇒ Object
- .updated_primes(prime) ⇒ Object
- .valid_class(object) ⇒ Object
- .valid_paren?(object) ⇒ Boolean
- .validate_left_right(fields = []) ⇒ Object
- .validate_math_zone(object, lang:, intent: false, options: nil) ⇒ Object
Class Method Details
.accent_value(accent, function: false, lang:) ⇒ Object
860 861 862 863 864 865 866 |
# File 'lib/plurimath/utility.rb', line 860 def accent_value(accent, function: false, lang:) if accent[:accent_symbols] symbols_class(UnicodeMath::Constants::ACCENT_SYMBOLS[accent[:accent_symbols].to_sym] || accent[:accent_symbols], lang: lang) else accent[:first_value] || filter_values(accent[:prime_accent_symbols]) end end |
.all_symbols_classes(lang) ⇒ Object
304 305 306 |
# File 'lib/plurimath/utility.rb', line 304 def all_symbols_classes(lang) symbols_hash(lang).merge(parens_hash(lang)) end |
.asciimath_symbol_object(value, lang: :asciimath) ⇒ Object
638 639 640 641 642 643 644 645 |
# File 'lib/plurimath/utility.rb', line 638 def asciimath_symbol_object(value, lang: :asciimath) return if value.nil? symbols = symbols_hash(lang) return symbols[value&.to_s].new if symbols.key?(value&.to_s) symbol_object(value&.to_s, lang: lang) end |
.attr_is_accent(attrs, value) ⇒ Object
528 529 530 531 532 533 534 |
# File 'lib/plurimath/utility.rb', line 528 def attr_is_accent(attrs, value) if value.last.is_a?(Math::Function::UnaryFunction) value.last.parameter_one = value.shift if value.length > 1 value.last.attributes = attrs.transform_values { |v| YAML.safe_load(v) } end value end |
.attr_is_function(attrs, value) ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/plurimath/utility.rb', line 536 def attr_is_function(attrs, value) case attrs when Math::Function::Fenced attrs.parameter_two = value.compact attrs when Math::Function::FontStyle attrs.parameter_one = filter_values(value) attrs when Math::Function::Color color_value = filter_values(value) if attrs.parameter_two attrs.parameter_two.parameter_one = color_value else attrs.parameter_two = color_value end attrs end end |
.base_is_prime?(base) ⇒ Boolean
980 981 982 983 |
# File 'lib/plurimath/utility.rb', line 980 def base_is_prime?(base) symbol_prime?(base.parameter_two) || primes_constants.key(base.parameter_two.value) end |
.base_is_sub_or_sup?(base) ⇒ Boolean
1001 1002 1003 1004 1005 1006 1007 1008 1009 |
# File 'lib/plurimath/utility.rb', line 1001 def base_is_sub_or_sup?(base) if base.is_a?(Math::Formula) base_is_sub_or_sup?(base.value.first) elsif base.is_a?(Math::Function::Fenced) base_is_sub_or_sup?(base.parameter_two.first) elsif base.is_a?(Math::Symbols::Symbol) || base.is_a?(Math::Number) base.mini_sub_sized || base_mini_sup_sized end end |
.base_recursion(sub_script, sub_recursion) ⇒ Object
939 940 941 942 943 944 945 946 947 948 949 |
# File 'lib/plurimath/utility.rb', line 939 def base_recursion(sub_script, sub_recursion) base_class = Math::Function::Base new_sub = sub_recursion.parameter_one if new_sub.is_a?(base_class) base_recursion(sub_script, new_sub) sub_recursion else sub_recursion.parameter_one = base_class.new(sub_script, new_sub) sub_recursion end end |
.binary_function_classes(mrow, under: false, lang:) ⇒ Object
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/plurimath/utility.rb', line 686 def binary_function_classes(mrow, under: false, lang:) binary_class = Math::Function::BinaryFunction mrow.each_with_index do |object, ind| mrow[ind] = mathml_unary_classes([object], lang: lang) if object.is_a?(String) object = mrow[ind] next unless object.is_a?(binary_class) if object.is_a?(Math::Function::Mod) next unless mrow.length >= 1 object.parameter_one = mrow.delete_at(ind - 1) unless ind.zero? object.parameter_two = mrow.delete_at(ind) elsif Mathml::Constants::UNICODE_SYMBOLS.invert[object.class_name] && mrow.length > 1 next if object.parameter_one || mrow.length > 2 next object.parameter_one = mrow.delete_at(ind - 1) if under && ind <= 1 object.parameter_one = mrow.delete_at(ind + 1) end end end |
.capitalize(text) ⇒ Object
259 260 261 |
# File 'lib/plurimath/utility.rb', line 259 def capitalize(text) text.to_s.split("_").map(&:capitalize).join end |
.enclosure_attrs(mask) ⇒ Object
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 |
# File 'lib/plurimath/utility.rb', line 1022 def enclosure_attrs(mask) raise "enclosure mask is not between 0 and 255" if (mask.nil? || mask < 0 || mask > 255) ret = "" unless mask.nil? mask ^= 15 bin_mask = mask.to_s(2).reverse classes = bin_mask.chars.each_with_index.map { |bit, i| MASK_CLASSES[2**i] if bit == '1' }.compact ret = classes.join(' ') end ret end |
.fenceable_classes(mrow = []) ⇒ Object
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/plurimath/utility.rb', line 744 def fenceable_classes(mrow = []) return false unless mrow.length > 1 return unless paren_able?(PARENTHESIS, mrow) || (mrow.first.is_a?(Math::Symbols::Paren) && mrow.last.is_a?(Math::Symbols::Paren)) open_paren = mrow.shift close_paren = mrow.pop if mrow.length == 1 && mrow.first.is_a?(Math::Function::Table) table = mrow.first table.open_paren = open_paren table.close_paren = close_paren else mrow.replace( [ Math::Function::Fenced.new(open_paren, mrow.dup, close_paren), ], ) end end |
.filter_math_zone_values(value, lang:, intent: false, options: nil) ⇒ Object
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
# File 'lib/plurimath/utility.rb', line 775 def filter_math_zone_values(value, lang:, intent: false, options: nil) return [] if value&.empty? new_arr = [] temp_array = [] skip_index = nil value.each_with_index do |obj, index| object = obj.dup next if index == skip_index if TEXT_CLASSES.include?(object.class_name) || math_display_text_objects(object) next temp_array << (object.is_a?(Math::Symbols::Symbol) ? symbol_to_text(object, lang: lang, intent: intent, options: ) : object.value) end new_arr << Math::Function::Text.new(temp_array.join(" ")) if temp_array.any? temp_array = [] new_arr << object end new_arr << Math::Function::Text.new(temp_array.join(" ")) if temp_array.any? new_arr end |
.filter_table_data(table_data) ⇒ Object
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/plurimath/utility.rb', line 224 def filter_table_data(table_data) table_data.each_with_index do |object, ind| if object.is_a?(Math::Symbols::Minus) table_data[ind] = Math::Formula.new( [object, table_data.delete_at(ind.next)], ) end end table_data end |
.filter_values(array) ⇒ Object
345 346 347 348 349 350 351 352 |
# File 'lib/plurimath/utility.rb', line 345 def filter_values(array) return array unless array.is_a?(Array) || array.is_a?(Math::Formula) array = array.is_a?(Math::Formula) ? array.value : array.flatten.compact return Math::Formula.new(array) if array.length > 1 array.first end |
.find_pos_chr(fonts_array, key) ⇒ Object
389 390 391 |
# File 'lib/plurimath/utility.rb', line 389 def find_pos_chr(fonts_array, key) fonts_array.find { |d| d.is_a?(Hash) && d[key] } end |
.frac_values(object) ⇒ Object
608 609 610 611 612 613 614 615 |
# File 'lib/plurimath/utility.rb', line 608 def frac_values(object) case object when Math::Formula object.value.any? { |d| symbol_value(d, ",") } when Array object.any? { |d| symbol_value(d, ",") } end end |
.fractions(numerator, denominator, options = nil) ⇒ Object
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
# File 'lib/plurimath/utility.rb', line 885 def fractions(numerator, denominator, = nil) frac_class = Math::Function::Frac if denominator.is_a?(frac_class) if denominator.parameter_one.is_a?(frac_class) recursion_fraction(denominator, numerator, ) else denominator.parameter_one = frac_class.new( unfenced_value(numerator, paren_specific: true), unfenced_value(denominator.parameter_one, paren_specific: true), ) end denominator else frac_class.new( unfenced_value(numerator, paren_specific: true), unfenced_value(denominator, paren_specific: true), ) end end |
.get_class(text) ⇒ Object
245 246 247 |
# File 'lib/plurimath/utility.rb', line 245 def get_class(text) Object.const_get("Plurimath::Math::Function::#{capitalize(text)}") end |
.get_paren_class(text) ⇒ Object
255 256 257 |
# File 'lib/plurimath/utility.rb', line 255 def get_paren_class(text) Object.const_get("Plurimath::Math::Symbols::Paren::#{capitalize(text)}") end |
.get_symbol_class(text) ⇒ Object
249 250 251 252 253 |
# File 'lib/plurimath/utility.rb', line 249 def get_symbol_class(text) Object.const_get("Plurimath::Math::Symbols::#{capitalize(text)}") rescue => e get_paren_class(text) end |
.get_table_class(text) ⇒ Object
235 236 237 |
# File 'lib/plurimath/utility.rb', line 235 def get_table_class(text) Object.const_get("Plurimath::Math::Function::Table::#{capitalize(text)}") end |
.hexcode_in_input(field) ⇒ Object
997 998 999 |
# File 'lib/plurimath/utility.rb', line 997 def hexcode_in_input(field) field.input(:unicodemath)&.flatten&.find { |input| input.match?(/&#x.+;/) } end |
.html_entity_to_unicode(string) ⇒ Object
458 459 460 461 |
# File 'lib/plurimath/utility.rb', line 458 def html_entity_to_unicode(string) entities = HTMLEntities.new entities.decode(string) end |
.identity_matrix(size) ⇒ Object
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'lib/plurimath/utility.rb', line 1011 def identity_matrix(size) matrix = Array.new(size) { Array.new(size, 0) } size.times { |i| matrix[i][i] = 1 } matrix.map do |tr| tr.map.with_index do |td, i| tr[i] = Math::Function::Td.new([Math::Number.new(td.to_s)]) end Math::Function::Tr.new(tr) end end |
.join_attr_value(attrs, value, unicode_only: false, lang: :mathml) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/plurimath/utility.rb', line 478 def join_attr_value(attrs, value, unicode_only: false, lang: :mathml) if attrs&.is_a?(Hash) && attrs.key?(:intent) [ Math::Function::Intent.new( filter_values(join_attr_value(nil, value, unicode_only: unicode_only, lang: lang)), Math::Function::Text.new(attrs[:intent]), ) ] elsif value.any?(String) new_value = mathml_unary_classes(value, unicode_only: unicode_only, lang: lang) array_value = Array(new_value) attrs.nil? ? array_value : join_attr_value(attrs, array_value, unicode_only: unicode_only) elsif attrs.nil? value elsif attrs.is_a?(String) && ["solid", "none"].include?(attrs.split.first.downcase) table_separator(attrs.split, value) elsif attrs.is_a?(Hash) if (attrs.key?(:accent) || attrs.key?(:accentunder)) attr_is_accent(attrs, value) elsif attrs.key?(:linebreak) Math::Function::Linebreak.new(value.first, attrs) elsif attrs.key?(:bevelled) || attrs.key?(:linethickness) Math::Function::Frac.new(value[0], value[1], attrs) elsif attrs.key?(:notation) value << attrs elsif attrs.key?(:separators) fenced = Math::Function::Fenced.new( symbol_object(attrs[:open] || "(", lang: lang), value, symbol_object(attrs[:close] || ")", lang: lang), ) fenced. = { separators: attrs[:separators] } fenced elsif TABLE_SUPPORTED_ATTRS.any? { |atr| attrs.key?(atr) } Math::Function::Table.new(value, nil, nil, attrs) elsif MPADDED_ATTRS.any? { |atr| attrs.key?(atr) } Math::Function::Mpadded.new( filter_values(value), attrs, ) elsif MGLYPH_ATTRS.any? { |atr| attrs.key?(atr) } Math::Function::Mglyph.new( attrs, ) end elsif attrs.is_a?(Math::Core) attr_is_function(attrs, value) end end |
.latex_table_curly_paren(string) ⇒ Object
1068 1069 1070 1071 1072 1073 1074 |
# File 'lib/plurimath/utility.rb', line 1068 def latex_table_curly_paren(string) case string when "{" then Math::Symbols::Paren::Lcurly.new when "}" then Math::Symbols::Paren::Rcurly.new else symbols_class(string, lang: :latex) end end |
.left_right_objects(paren, function) ⇒ Object
655 656 657 658 659 660 661 662 |
# File 'lib/plurimath/utility.rb', line 655 def left_right_objects(paren, function) paren = if paren.to_s.match?(/\\\{|\\\}/) paren.to_s.gsub(/\\/, "") else Latex::Constants::LEFT_RIGHT_PARENTHESIS[paren.to_sym] end get_class(function).new(paren) end |
.math_display_text_objects(object) ⇒ Object
1063 1064 1065 1066 |
# File 'lib/plurimath/utility.rb', line 1063 def math_display_text_objects(object) class_names = ["plus", "minus", "circ", "equal", "symbol"].freeze class_names.include?(object.class_name) end |
.mathml_unary_classes(text_array, omml: false, unicode_only: false, lang: nil) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/plurimath/utility.rb', line 421 def mathml_unary_classes(text_array, omml: false, unicode_only: false, lang: nil) return [] if text_array.empty? compacted = text_array.compact return filter_values(compacted) unless compacted.any?(String) string = compacted.join classes = Mathml::Constants::CLASSES unicode = string_to_html_entity(string) return symbols_class(unicode, lang: lang) if unicode_only && unicode symbol = Mathml::Constants::UNICODE_SYMBOLS[unicode.strip.to_sym] if classes.include?(symbol&.strip) get_class(symbol.strip).new elsif classes.any?(string&.strip) get_class(string.strip).new else omml ? text_classes(string, lang: lang) : symbols_class(unicode, lang: lang) end end |
.mrow_left_right(mrow = []) ⇒ Object
670 671 672 673 674 675 676 |
# File 'lib/plurimath/utility.rb', line 670 def mrow_left_right(mrow = []) object = mrow.first !( ((object.is_a?(Math::Function::TernaryFunction) && object.any_value_exist?) && (mrow.length <= 2)) || (object.is_a?(Math::Function::UnaryFunction) && mrow.length == 1) ) end |
.multiscript(values) ⇒ Object
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/plurimath/utility.rb', line 555 def multiscript(values) values.slice_before("mprescripts").map do |value| base_value = value.shift value = nil_to_none_object(value) part_val = value.partition.with_index { |_, i| i.even? } first_value = part_val[0].empty? ? nil : part_val[0] second_value = part_val[1].empty? ? nil : part_val[1] if base_value.to_s.include?("mprescripts") [first_value, second_value] else Math::Function::PowerBase.new( filter_values(base_value), filter_values(first_value), filter_values(second_value), ) end end end |
.nary_fonts(nary, lang:) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/plurimath/utility.rb', line 376 def nary_fonts(nary, lang:) narypr = nary.first.flatten.compact subsup = narypr.any?("undOvr") ? "undOvr" : "subSup" unicode = narypr.any?(Hash) ? narypr.first[:chr] : "∫" Math::Function::Nary.new( symbols_class(string_to_html_entity(unicode), lang: lang), filter_values(nary[1]), filter_values(nary[2]), filter_values(nary[3]), { type: subsup } ) end |
.nil_to_none_object(value) ⇒ Object
574 575 576 577 578 579 580 581 582 |
# File 'lib/plurimath/utility.rb', line 574 def nil_to_none_object(value) return value unless value.any?(NilClass) || value.any? { |val| val.is_a?(Array) && val.empty? } value.each.with_index do |val, index| next unless val.nil? || val.is_a?(Array) value[index] = Math::Function::None.new end end |
.notations_to_mask(notations) ⇒ Object
1035 1036 1037 1038 1039 |
# File 'lib/plurimath/utility.rb', line 1035 def notations_to_mask(notations) mask = [] notations.split(" ").each { |notation| mask << MASK_CLASSES.key(notation) } mask.inject(*:+)^15 end |
.organize_options(table_data, column_align) ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/plurimath/utility.rb', line 192 def (table_data, column_align) return column_align if column_align.length <= 1 align = [column_align&.shift] table_data.insert(0, *column_align) align end |
.organize_table(array, column_align: nil, options: nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/plurimath/utility.rb', line 164 def organize_table(array, column_align: nil, options: nil) table = [] table_data = [] table_row = [] (array, column_align) if string_columns = column_align&.map { |column| column.is_a?(Math::Symbols::Paren) ? "|" : column.value } array.each do |data| if data&.separate_table table_row << Math::Function::Td.new(filter_table_data(table_data).compact) table_data = [] if data.linebreak organize_tds(table_row.flatten, string_columns.dup, ) table << Math::Function::Tr.new(table_row) table_row = [] end next end table_data << data end table_row << Math::Function::Td.new(table_data.compact) if table_data unless table_row.nil? || table_row.empty? organize_tds(table_row.flatten, string_columns.dup, ) table << Math::Function::Tr.new(table_row) end table_separator(string_columns, table, symbol: "|") unless column_align.nil? || column_align.empty? table end |
.organize_tds(tr_array, column_align, options) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/plurimath/utility.rb', line 213 def organize_tds(tr_array, column_align, ) return tr_array if column_align.nil? || column_align.empty? column_align.reject! { |string| string == "|" } column_align = column_align * tr_array.length if tr_array.map.with_index do |td, ind| columnalign = ALIGNMENT_LETTERS[column_align[ind]&.to_sym] td.parameter_two = { columnalign: columnalign } if columnalign end end |
.ox_element(node, attributes: [], namespace: "") ⇒ Object
308 309 310 311 312 313 314 |
# File 'lib/plurimath/utility.rb', line 308 def ox_element(node, attributes: [], namespace: "") namespace = "#{namespace}:" unless namespace.empty? element = Plurimath.xml_engine.new_element("#{namespace}#{node}") element.set_attr(attributes) element end |
.paren_able?(arr = [], mrow = []) ⇒ Boolean
738 739 740 741 742 |
# File 'lib/plurimath/utility.rb', line 738 def paren_able?(arr = [], mrow = []) arr.any? do |opening, closing| symbol_value(mrow.first, opening.to_s) && symbol_value(mrow.last, closing.to_s) end end |
.paren_files ⇒ Object
263 264 265 |
# File 'lib/plurimath/utility.rb', line 263 def paren_files Math::Symbols::Paren.descendants end |
.parens_hash(lang, skipables: nil) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/plurimath/utility.rb', line 287 def parens_hash(lang, skipables: nil) @@parens ||= {} lang_parens = @@parens[lang] return lang_parens if lang_parens && !lang_parens.empty? lang_parens = {} paren_files.map do |class_object| class_object::INPUT[lang]&.flatten&.each do |symbol| next if skipables && skipables.include?(class_object.new.class_name) next if lang_parens.key?(symbol) lang_parens[symbol] = class_object end end @@parens[lang] = lang_parens.sort_by { |v, _| -v.length }.to_h end |
.populate_function_classes(mrow = [], lang:) ⇒ Object
678 679 680 681 682 683 684 |
# File 'lib/plurimath/utility.rb', line 678 def populate_function_classes(mrow = [], lang:) flatten_mrow = mrow.flatten.compact unary_function_classes(flatten_mrow, lang: lang) binary_function_classes(flatten_mrow, lang: lang) ternary_function_classes(flatten_mrow) flatten_mrow end |
.pr_element(main_tag, wi_tag = false, namespace: "") ⇒ Object
337 338 339 340 341 342 343 |
# File 'lib/plurimath/utility.rb', line 337 def pr_element(main_tag, wi_tag = false, namespace: "") tag_name = "#{main_tag}Pr" ox_element( tag_name, namespace: namespace, ) << rpr_element(wi_tag: wi_tag) end |
.primes_constants ⇒ Object
990 991 992 993 994 995 |
# File 'lib/plurimath/utility.rb', line 990 def primes_constants primes = {} primes .merge!(UnicodeMath::Constants::PREFIXED_PRIMES) .merge({ sprime: "'" }) end |
.recursion_fraction(frac, numerator, options) ⇒ Object
907 908 909 910 911 912 913 914 915 916 917 918 919 920 |
# File 'lib/plurimath/utility.rb', line 907 def recursion_fraction(frac, numerator, ) frac_class = Math::Function::Frac new_numerator = frac.parameter_one if new_numerator.is_a?(frac_class) recursion_fraction(new_numerator, numerator, ) else frac.parameter_one = frac_class.new( unfenced_value(numerator, paren_specific: true), unfenced_value(frac.parameter_one, paren_specific: true), ) frac end end |
.recursive_sub(sub_script, sub_recursion) ⇒ Object
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
# File 'lib/plurimath/utility.rb', line 922 def recursive_sub(sub_script, sub_recursion) base_class = Math::Function::Base if sub_recursion.is_a?(base_class) if sub_recursion.parameter_one.is_a?(base_class) base_recursion(sub_script, sub_recursion) else sub_recursion.parameter_one = base_class.new(sub_script, sub_recursion.parameter_one) end sub_recursion else base_class.new( sub_script, sub_recursion, ) end end |
.recursive_sup(sup_script, sup_recursion) ⇒ Object
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'lib/plurimath/utility.rb', line 951 def recursive_sup(sup_script, sup_recursion) power_class = Math::Function::Power if sup_recursion.is_a?(power_class) if sup_recursion.parameter_one.is_a?(power_class) sup_recursion(sup_script, sup_recursion) else sup_recursion.parameter_one = power_class.new(sup_script, sup_recursion.parameter_one) end sup_recursion else power_class.new( sup_script, sup_recursion, ) end end |
.rpr_element(wi_tag: false) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/plurimath/utility.rb', line 316 def rpr_element(wi_tag: false) rpr_element = ox_element("rPr", namespace: "w") attributes = { "w:ascii": "Cambria Math", "w:hAnsi": "Cambria Math" } rpr_element << ox_element( "rFonts", namespace: "w", attributes: attributes, ) rpr_element << ox_element("i", namespace: "w") if wi_tag rpr_element end |
.sequence_slashed_values(values, lang:) ⇒ Object
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 |
# File 'lib/plurimath/utility.rb', line 1050 def sequence_slashed_values(values, lang:) values.each.with_index do |value, index| decoded = HTMLEntities.new.decode(value.value) slashed = if index == 0 slashed_values(value.value) else decoded.match?(/[0-9]/) ? Math::Number.new(decoded) : symbols_class(decoded, lang: lang) end values[index] = slashed end values end |
.slashed_values(value) ⇒ Object
1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'lib/plurimath/utility.rb', line 1041 def slashed_values(value) decoded = HTMLEntities.new.decode(value) if decoded.to_s.match?(/^\w+/) Math::Function::Text.new("\\#{decoded}") else Math::Symbols::Symbol.new(decoded, true) end end |
.string_to_html_entity(string) ⇒ Object
450 451 452 453 454 455 456 |
# File 'lib/plurimath/utility.rb', line 450 def string_to_html_entity(string) entities = HTMLEntities.new entities.encode( string.frozen? ? string : string.force_encoding('UTF-8'), :hexadecimal, ) end |
.sub_sup_method?(sub_sup) ⇒ Boolean
239 240 241 242 243 |
# File 'lib/plurimath/utility.rb', line 239 def sub_sup_method?(sub_sup) if sub_sup.methods.include?(:class_name) Html::Constants::SUB_SUP_CLASSES.value?(sub_sup.class_name.to_sym) end end |
.sup_recursion(sup_script, sup_recursion) ⇒ Object
968 969 970 971 972 973 974 975 976 977 978 |
# File 'lib/plurimath/utility.rb', line 968 def sup_recursion(sup_script, sup_recursion) power_class = Math::Function::Power new_sup = sup_recursion.parameter_one if new_sup.is_a?(power_class) sup_recursion(sup_script, new_sup) sup_recursion else sup_recursion.parameter_one = power_class.new(sup_script, new_sup) sup_recursion end end |
.symbol_object(value, lang: nil) ⇒ Object
627 628 629 630 631 632 633 634 635 636 |
# File 'lib/plurimath/utility.rb', line 627 def symbol_object(value, lang: nil) value = case value when "ℒ" then "{:" when "ℛ" then ":}" when "ᑕ" then "〈" when "ᑐ" then "〉" else value end symbols_class(value, lang: lang) end |
.symbol_prime?(obj) ⇒ Boolean
985 986 987 988 |
# File 'lib/plurimath/utility.rb', line 985 def symbol_prime?(obj) obj&.class&.const_defined?(:INPUT) && primes_constants&.key(hexcode_in_input(obj)) end |
.symbol_to_text(symbol, lang:, intent: false, options:) ⇒ Object
796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/plurimath/utility.rb', line 796 def symbol_to_text(symbol, lang:, intent: false, options:) case lang when :asciimath symbol.to_asciimath(options: ) when :latex symbol.to_latex(options: ) when :mathml symbol.to_mathml_without_math_tag(intent, options: ).nodes.first when :omml symbol.to_omml_without_math_tag(true, options: ) when :unicodemath symbol.to_unicodemath(options: ) end end |
.symbol_value(object, value) ⇒ Object
404 405 406 407 408 409 410 |
# File 'lib/plurimath/utility.rb', line 404 def symbol_value(object, value) (object.is_a?(Math::Symbols::Comma) if value&.include?(",")) || (object.is_a?(Math::Symbols::Minus) if value&.include?("-")) || (object.is_a?(Math::Symbols::Paren::Vert) if value&.include?("|")) || (object.is_a?(Math::Symbols::Symbol) && object&.value&.include?(value)) || (value == "\\\\" && object.is_a?(Math::Function::Linebreak)) end |
.symbols_class(string, lang:, table: false) ⇒ Object
442 443 444 445 446 447 448 |
# File 'lib/plurimath/utility.rb', line 442 def symbols_class(string, lang:, table: false) return string unless string.is_a?(String) || string.is_a?(Parslet::Slice) return latex_table_curly_paren(string) if table && lang == :latex all_symbols_classes(lang)[string.to_s.strip]&.new || Math::Symbols::Symbol.new(string) end |
.symbols_files ⇒ Object
267 268 269 |
# File 'lib/plurimath/utility.rb', line 267 def symbols_files Math::Symbols::Symbol.descendants end |
.symbols_hash(lang) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/plurimath/utility.rb', line 271 def symbols_hash(lang) @@symbols ||= {} lang_symbols = @@symbols[lang] return lang_symbols if lang_symbols && !lang_symbols.empty? lang_symbols = {} symbols_files.map do |class_object| class_object::INPUT[lang]&.flatten&.each do |symbol| next if lang_symbols.key?(symbol) lang_symbols[symbol] = class_object end end @@symbols[lang] = lang_symbols.sort_by { |v, _| -v.length }.to_h end |
.table_options(table_data) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/plurimath/utility.rb', line 200 def (table_data) rowline = "" table_data.map do |tr| if tr&.parameter_one&.first&.parameter_one&.first.is_a?(Math::Symbols::Hline) rowline += "solid " else rowline += "none " end end = { rowline: rowline.strip } if rowline.include?("solid") || {} end |
.table_separator(separator, value, symbol: "solid") ⇒ Object
463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/plurimath/utility.rb', line 463 def table_separator(separator, value, symbol: "solid") sep_symbol = Math::Function::Td.new([Math::Symbols::Paren::Vert.new]) separator&.each_with_index do |sep, ind| next unless sep == symbol value.map do |val| val.parameter_one.insert((ind + 1), sep_symbol) if symbol == "solid" val.parameter_one.insert(ind, sep_symbol) if symbol == "|" (val.parameter_one[val.parameter_one.index(nil)] = Math::Function::Td.new([])) rescue nil val end end value end |
.table_td(object) ⇒ Object
617 618 619 620 621 622 623 624 625 |
# File 'lib/plurimath/utility.rb', line 617 def table_td(object) new_object = case object when Math::Function::Td object else Math::Function::Td.new([object]) end Array(new_object) end |
.td_value(td_object) ⇒ Object
412 413 414 415 416 417 418 419 |
# File 'lib/plurimath/utility.rb', line 412 def td_value(td_object) str_classes = [String, Parslet::Slice] if str_classes.include?(td_object.class) && td_object.to_s.empty? return Math::Function::Text.new(nil) end td_object end |
.td_values(objects, slicer) ⇒ Object
393 394 395 396 397 398 399 400 401 402 |
# File 'lib/plurimath/utility.rb', line 393 def td_values(objects, slicer) sliced = objects.slice_when { |object, _| symbol_value(object, slicer) } tds = sliced.map do |slice| Math::Function::Td.new( slice.delete_if { |d| symbol_value(d, slicer) }.compact, ) end tds << Math::Function::Td.new([]) if symbol_value(objects.last, slicer) tds end |
.ternary_function_classes(mrow) ⇒ Object
723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/plurimath/utility.rb', line 723 def ternary_function_classes(mrow) ternary_class = Math::Function::TernaryFunction if mrow.any?(ternary_class) && mrow.length > 1 mrow.each_with_index do |object, ind| if object.is_a?(ternary_class) next if [Math::Function::Fenced, Math::Function::Multiscript].include?(object.class) next unless object.parameter_one || object.parameter_two next if object.parameter_three object.parameter_three = filter_values(mrow.delete_at(ind + 1)) end end end end |
.text_classes(text, lang:) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/plurimath/utility.rb', line 354 def text_classes(text, lang:) return nil unless text text = filter_values(text) unless text.is_a?(String) return text if text.is_a?(Math::Core) if text&.scan(/[[:digit:]]/)&.length == text&.length Math::Number.new(text) elsif text&.match?(/[a-zA-Z]/) Math::Function::Text.new(text) else text = string_to_html_entity(text) .gsub("&", "&") .gsub("<", "<") .gsub("'", "'") .gsub(" ", " ") .gsub(">", ">") .gsub("
", "\n") symbols_class(text, lang: lang) end end |
.transform_accents(accents, lang:) ⇒ Object
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/plurimath/utility.rb', line 828 def transform_accents(accents, lang:) accents.reduce do |function, accent| if function.is_a?(Hash) if function[:prime_accent_symbols] Math::Function::Power.new( unfenced_value(accent_value(function, function: true, lang: lang), paren_specific: true), accent_value(accent, lang: lang), ) else Math::Function::Overset.new( accent_value(accent, lang: lang), unfenced_value(accent_value(function, function: true, lang: lang), paren_specific: true), { accent: true } ) end else if accent[:prime_accent_symbols] Math::Function::Power.new( unfenced_value(function, paren_specific: true), accent_value(accent, lang: lang), ) else Math::Function::Overset.new( accent_value(accent, lang: lang), unfenced_value(function, paren_specific: true), { accent: true } ) end end end end |
.unary_function_classes(mrow, lang:) ⇒ Object
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/plurimath/utility.rb', line 707 def unary_function_classes(mrow, lang:) unary_class = Math::Function::UnaryFunction if mrow.any?(String) || mrow.any?(unary_class) mrow.each_with_index do |object, ind| mrow[ind] = mathml_unary_classes([object], lang: lang) if object.is_a?(String) object = mrow[ind] if object.is_a?(String) next unless object.is_a?(unary_class) next if object.is_a?(Math::Function::Text) next if object.parameter_one || mrow[ind + 1].nil? next unless ind.zero? object.parameter_one = mrow.delete_at(ind + 1) end end end |
.unfenced_value(object, paren_specific: false) ⇒ Object
584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/plurimath/utility.rb', line 584 def unfenced_value(object, paren_specific: false) case object when Math::Function::Fenced if !paren_specific || (paren_specific && valid_paren?(object)) filter_values(object.parameter_two) else object end when Array filter_values(object) else object end end |
.unicode_accents(accents, lang: :unicodemath) ⇒ Object
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/plurimath/utility.rb', line 811 def unicode_accents(accents, lang: :unicodemath) if accents.is_a?(Math::Function::BinaryFunction) accents else if accents.any? { |acc| acc&.dig(:first_value)&.is_a?(Array) } accent_value = accents.first[:first_value].pop first_value = accents.first[:first_value] accents.first[:first_value] = accent_value Math::Formula.new( first_value + [transform_accents(accents, lang: lang)] ) else transform_accents(accents, lang: lang) end end end |
.unicode_fractions(fractions) ⇒ Object
868 869 870 871 872 873 874 875 |
# File 'lib/plurimath/utility.rb', line 868 def unicode_fractions(fractions) frac_arr = UnicodeMath::Constants::UNICODE_FRACTIONS[fractions.to_sym] Math::Function::Frac.new( Math::Number.new(frac_arr.first.to_s), Math::Number.new(frac_arr.last.to_s), { displaystyle: false, unicodemath_fraction: true } ) end |
.update_nodes(element, nodes) ⇒ Object
328 329 330 331 332 333 334 335 |
# File 'lib/plurimath/utility.rb', line 328 def update_nodes(element, nodes) nodes&.each do |node| next update_nodes(element, node) if node.is_a?(Array) element << node unless node.nil? end element end |
.updated_primes(prime) ⇒ Object
877 878 879 880 881 882 883 |
# File 'lib/plurimath/utility.rb', line 877 def updated_primes(prime) filter_values( prime.to_s.scan(UNICODE_REGEX).map do |str| symbols_class(str, lang: :unicodemath) end ) end |
.valid_class(object) ⇒ Object
664 665 666 667 668 |
# File 'lib/plurimath/utility.rb', line 664 def valid_class(object) text = object.extract_class_name_from_text (object.extractable? && Asciimath::Constants::SUB_SUP_CLASSES.include?(text)) || Latex::Constants::SYMBOLS[text.to_sym] == :power_base end |
.valid_paren?(object) ⇒ Boolean
599 600 601 602 603 604 605 606 |
# File 'lib/plurimath/utility.rb', line 599 def valid_paren?(object) object.parameter_one.is_a?(Math::Symbols::Paren::Lround) && object.parameter_three.is_a?(Math::Symbols::Paren::Rround) && !object..keys.any? { |k| [:open_paren, :close_paren].any?(k.to_sym) } && !object.parameter_one.mini_sup_sized && !object.parameter_three.mini_sub_sized && object..empty? end |
.validate_left_right(fields = []) ⇒ Object
647 648 649 650 651 652 653 |
# File 'lib/plurimath/utility.rb', line 647 def validate_left_right(fields = []) fields.each do |field| if field.is_a?(Math::Formula) && field.value.first.is_a?(Math::Function::Left) field.left_right_wrapper = true end end end |
.validate_math_zone(object, lang:, intent: false, options: nil) ⇒ Object
763 764 765 766 767 768 769 770 771 772 773 |
# File 'lib/plurimath/utility.rb', line 763 def validate_math_zone(object, lang:, intent: false, options: nil) return false unless object if object.is_a?(Math::Formula) filter_math_zone_values(object.value, lang: lang, intent: intent, options: ).find do |value| !(value.is_a?(Math::Function::Text) || value.is_a?(Math::Symbols::Symbol)) end else !(TEXT_CLASSES.include?(object.class_name) || object.is_a?(Math::Symbols::Symbol)) end end |