Class: Plurimath::Utility
- Inherits:
-
Object
- Object
- Plurimath::Utility
- Defined in:
- lib/plurimath/utility.rb,
lib/plurimath/utility/intent_encoding.rb
Direct Known Subclasses
Asciimath::Utility, Html::Utility, Latex::Utility, Mathml::Utility, Omml::Utility, Plurimath::UnicodeMath::Utility
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
- PARENTHESIS =
{ "〈": "〉", "⌊": "⌋", "⌈": "⌉", "‖": "‖", "{": "}", "[": "]", "|": "|", "(": ")", "{": "}", "[": "]", }.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
- HTML_ENTITIES =
HTMLEntities.new
Class Method Summary collapse
- .all_symbols_classes(lang) ⇒ Object
- .capitalize(text) ⇒ Object
- .filter_values(array, new_formula: true) ⇒ Object
- .get_class(text) ⇒ Object
- .get_table_class(text) ⇒ Object
- .hexcode_in_input(field) ⇒ Object
- .html_entity_to_unicode(string) ⇒ Object
- .latex_table_curly_paren(string) ⇒ Object
- .paren_files ⇒ Object
- .parens_hash(lang, skipables: nil) ⇒ Object
- .primes_constants ⇒ Object
- .string_to_html_entity(string) ⇒ Object
- .symbol_object(value, lang: nil) ⇒ Object
- .symbol_value(object, value) ⇒ Object
- .symbols_class(string, lang:, table: false) ⇒ Object
- .symbols_files ⇒ Object
- .symbols_hash(lang) ⇒ Object
- .table_separator(separator, value, symbol: "solid") ⇒ Object
- .unfenced_value(object, paren_specific: false) ⇒ Object
- .valid_paren?(object) ⇒ Boolean
Class Method Details
.all_symbols_classes(lang) ⇒ Object
188 189 190 |
# File 'lib/plurimath/utility.rb', line 188 def all_symbols_classes(lang) symbols_hash(lang).merge(parens_hash(lang)) end |
.capitalize(text) ⇒ Object
143 144 145 |
# File 'lib/plurimath/utility.rb', line 143 def capitalize(text) text.to_s.split("_").map(&:capitalize).join end |
.filter_values(array, new_formula: true) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/plurimath/utility.rb', line 192 def filter_values(array, new_formula: true) 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 && new_formula return array if array.length > 1 && !new_formula array.first end |
.get_class(text) ⇒ Object
139 140 141 |
# File 'lib/plurimath/utility.rb', line 139 def get_class(text) Object.const_get("Plurimath::Math::Function::#{capitalize(text)}") end |
.get_table_class(text) ⇒ Object
135 136 137 |
# File 'lib/plurimath/utility.rb', line 135 def get_table_class(text) Object.const_get("Plurimath::Math::Function::Table::#{capitalize(text)}") end |
.hexcode_in_input(field) ⇒ Object
299 300 301 302 303 |
# File 'lib/plurimath/utility.rb', line 299 def hexcode_in_input(field) field.input(:unicodemath)&.flatten&.find do |input| input.match?(/&#x.+;/) end end |
.html_entity_to_unicode(string) ⇒ Object
229 230 231 232 233 |
# File 'lib/plurimath/utility.rb', line 229 def html_entity_to_unicode(string) return string unless string&.include?("&") HTML_ENTITIES.decode(string) end |
.latex_table_curly_paren(string) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/plurimath/utility.rb', line 305 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 |
.paren_files ⇒ Object
147 148 149 |
# File 'lib/plurimath/utility.rb', line 147 def paren_files Math::Symbols::Paren.descendants end |
.parens_hash(lang, skipables: nil) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/plurimath/utility.rb', line 171 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&.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 |
.primes_constants ⇒ Object
292 293 294 295 296 297 |
# File 'lib/plurimath/utility.rb', line 292 def primes_constants primes = {} primes .merge!(UnicodeMath::Constants::PREFIXED_PRIMES) .merge({ sprime: "'" }) end |
.string_to_html_entity(string) ⇒ Object
220 221 222 223 224 225 |
# File 'lib/plurimath/utility.rb', line 220 def string_to_html_entity(string) HTML_ENTITIES.encode( string.frozen? ? string : string.force_encoding("UTF-8"), :hexadecimal, ) end |
.symbol_object(value, lang: nil) ⇒ Object
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/plurimath/utility.rb', line 281 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_value(object, value) ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/plurimath/utility.rb', line 202 def symbol_value(object, value) return false if value.nil? (object.is_a?(Math::Symbols::Comma) if value == ",") || (object.is_a?(Math::Symbols::Minus) if value == "-") || (object.is_a?(Math::Symbols::Paren::Vert) if value == "|") || (object.is_a?(Math::Symbols::Symbol) && object.value == value) || (value == "\\\\" && object.is_a?(Math::Function::Linebreak)) end |
.symbols_class(string, lang:, table: false) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/plurimath/utility.rb', line 212 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
151 152 153 |
# File 'lib/plurimath/utility.rb', line 151 def symbols_files Math::Symbols::Symbol.descendants end |
.symbols_hash(lang) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/plurimath/utility.rb', line 155 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_separator(separator, value, symbol: "solid") ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/plurimath/utility.rb', line 235 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 == "|" begin (val.parameter_one[val.parameter_one.index(nil)] = Math::Function::Td.new([])) rescue StandardError nil end val end end value end |
.unfenced_value(object, paren_specific: false) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/plurimath/utility.rb', line 255 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 |
.valid_paren?(object) ⇒ Boolean
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/plurimath/utility.rb', line 270 def valid_paren?(object) object.parameter_one.is_a?(Math::Symbols::Paren::Lround) && object.parameter_three.is_a?(Math::Symbols::Paren::Rround) && object..keys.none? do |k| %i[open_paren close_paren].any?(k.to_sym) end && !object.parameter_one.mini_sup_sized && !object.parameter_three.mini_sub_sized && object..empty? end |