Class: Plurimath::Math::Formula

Inherits:
Core
  • Object
show all
Includes:
Plurimath::Mathml::Utility
Defined in:
lib/plurimath/math/formula.rb,
lib/plurimath/math/formula/mrow.rb,
lib/plurimath/math/formula/mstyle.rb

Direct Known Subclasses

Mrow, Mstyle

Defined Under Namespace

Classes: Mrow, Mstyle

Constant Summary collapse

MATH_ZONE_TYPES =
%i[
  omml
  latex
  mathml
  asciimath
  unicodemath
].freeze
POWER_BASE_CLASSES =
%w[powerbase power base].freeze
DERIVATIVE_CONSTS =
[
  "𝑑",
  "ⅅ",
  "ⅆ",
  "d",
].freeze

Constants included from Plurimath::Mathml::Utility::FormulaTransformation

Plurimath::Mathml::Utility::FormulaTransformation::CONDITIONAL_COMMON_UNARY_FUNCTIONS, Plurimath::Mathml::Utility::FormulaTransformation::SYMBOL_UPDATABLE_FUNCTIONS

Constants inherited from Core

Core::ALL_PARAMETERS, Core::REPLACABLES

Instance Attribute Summary collapse

Attributes included from Plurimath::Mathml::Utility

#temp_mathml_order

Instance Method Summary collapse

Methods included from Plurimath::Mathml::Utility

#accent=, #accentunder=, #bevelled=, #clear_temp_order, #linebreak=, #linebreakstyle=, #linethickness=, #mlabeledtr_value=, #mscarry_value=, #mtable_value=, #notation=

Methods included from Plurimath::Mathml::Utility::EmptyDefinedMethods

#accent, #accentunder, #align, #align=, #alignmentscope, #alignmentscope=, #annotation, #background, #background=, #bevelled, #charalign, #charalign=, #charspacing, #charspacing=, #close, #close=, #color, #color=, #columnalign, #columnalign=, #columnlines, #columnlines=, #columnspacing, #columnspacing=, #columnspan, #columnspan=, #columnwidth, #columnwidth=, #crossout, #crossout=, #decimalpoint, #decimalpoint=, #denomalign, #denomalign=, #depth, #depth=, #dir, #dir=, #edge, #edge=, #equalcolumns, #equalcolumns=, #equalrows, #equalrows=, #fence, #fence=, #fontfamily, #fontfamily=, #fontsize, #fontsize=, #fontstyle, #fontstyle=, #fontweight, #fontweight=, #form, #form=, #frame, #frame=, #framespacing, #framespacing=, #groupalign, #groupalign=, #height, #height=, #id, #indentalign, #indentalign=, #indentalignfirst, #indentalignfirst=, #indentalignlast, #indentalignlast=, #indentshift, #indentshift=, #indentshiftfirst, #indentshiftfirst=, #indentshiftlast, #indentshiftlast=, #indenttarget, #indenttarget=, #infixlinebreakstyle, #infixlinebreakstyle=, #intent, #largeop, #largeop=, #leftoverhang, #leftoverhang=, #length, #length=, #linebreak, #linebreakmultchar, #linebreakmultchar=, #linebreakstyle, #lineleading, #lineleading=, #linethickness, #location, #location=, #longdivstyle, #longdivstyle=, #lquote, #lquote=, #lspace, #lspace=, #maligngroup_value, #malignmark, #malignmark=, #malignmark_value, #mathbackground, #mathbackground=, #mathbackgroundcolor, #mathbackgroundcolor=, #mathcolor, #mathsize, #mathsize=, #mathvariant, #maxsize, #maxsize=, #mediummathspace, #mediummathspace=, #menclose_value, #merror_value, #mfenced_value, #mfrac_value, #mfraction_value, #mglyph_value, #mi_value, #minlabelspacing, #minlabelspacing=, #minsize, #minsize=, #mlabeledtr_value, #mlongdiv_value, #mmultiscripts_value, #mn_value, #mo_value, #movablelimits, #movablelimits=, #mover_value, #mpadded_value, #mphantom_value, #mprescripts_value, #mprescripts_value=, #mroot_value, #mrow_value, #ms_value, #mscarries_value, #mscarry_value, #msgroup_value, #msline_value, #mslinethickness, #mslinethickness=, #mspace_value, #msqrt_value, #msrow_value, #mstack_value, #mstyle_value, #msub_value, #msubsup_value, #msup_value, #mtable_value, #mtd_value, #mtext_value, #mtr_value, #munder_value, #munderover_value, #none_value, #notation, #numalign, #numalign=, #open, #open=, #position, #position=, #rightoverhang, #rightoverhang=, #rowalign, #rowalign=, #rowlines, #rowlines=, #rowspacing, #rowspacing=, #rowspan, #rowspan=, #rquote, #rquote=, #rspace, #rspace=, #scriptlevel, #scriptlevel=, #scriptminsize, #scriptminsize=, #scriptsizemultiplier, #scriptsizemultiplier=, #selection, #selection=, #semantics_value, #separator, #separator=, #separators, #separators=, #shift, #shift=, #side, #side=, #stackalign, #stackalign=, #stretchy, #stretchy=, #subscriptshift, #subscriptshift=, #superscriptshift, #superscriptshift=, #symmetric, #symmetric=, #thickmathspace, #thickmathspace=, #thinmathspace, #thinmathspace=, #valign, #valign=, #verythickmathspace, #verythickmathspace=, #verythinmathspace, #verythinmathspace=, #veryverythickmathspace, #veryverythickmathspace=, #veryverythinmathspace, #veryverythinmathspace=, #width, #width=

Methods inherited from Core

#ascii_fields_to_print, #class_name, #common_math_zone_conversion, descendants, #dump_mathml, #dump_nodes, #dump_omml, #dump_ox_nodes, #empty_tag, #extractable?, #filtered_values, #font_style_t_tag, #get, #gsub_spacing, inherited, #insert_t_tag, #invert_unicode_symbols, #is_binary_function?, #is_mrow?, #is_mstyle?, #is_nary_function?, #is_nary_symbol?, #is_ternary_function?, #is_unary?, #latex_fields_to_print, #linebreak?, #mathml_fields_to_print, #nary_intent_name, #omml_fields_to_print, #omml_nodes, #omml_parameter, #omml_tag_name, #ox_element, #paren?, #pretty_print_instance_variables, #prime_unicode?, #r_element, #replacable_values, #result, #separate_table, #set, #symbol?, #tag_name, #to_ms_value, #unicodemath_fields_to_print, #unicodemath_parens, #updated_object_values, #validate_mathml_fields, #variable_value, #variables

Constructor Details

#initialize(value = [], left_right_wrapper = true, display_style: true, input_string: nil, unitsml: false) ⇒ Formula

Returns a new instance of Formula.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plurimath/math/formula.rb', line 27

def initialize(
  value = [],
  left_right_wrapper = true,
  display_style: true,
  input_string: nil,
  unitsml: false
)
  @value = value.is_a?(Array) ? value : [value]
  left_right_wrapper = false if @value.first.is_a?(Function::Left)
  @left_right_wrapper = left_right_wrapper
  @displaystyle = boolean_display_style(display_style)
  @unitsml = unitsml if unitsml
end

Instance Attribute Details

#displaystyleObject

Returns the value of attribute displaystyle.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def displaystyle
  @displaystyle
end

#input_stringObject

Returns the value of attribute input_string.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def input_string
  @input_string
end

#left_right_wrapperObject

Returns the value of attribute left_right_wrapper.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def left_right_wrapper
  @left_right_wrapper
end

#unitsmlObject

Returns the value of attribute unitsml.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def unitsml
  @unitsml
end

#unitsml_xmlObject

Returns the value of attribute unitsml_xml.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def unitsml_xml
  @unitsml_xml
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/plurimath/math/formula.rb', line 10

def value
  @value
end

Instance Method Details

#==(object) ⇒ Object



41
42
43
44
45
46
# File 'lib/plurimath/math/formula.rb', line 41

def ==(object)
  object.respond_to?(:value) &&
    object.respond_to?(:left_right_wrapper) &&
    object.value == value &&
    object.left_right_wrapper == left_right_wrapper
end

#cloned_objectsObject



256
257
258
259
260
261
# File 'lib/plurimath/math/formula.rb', line 256

def cloned_objects
  cloned_obj = value.map(&:cloned_objects)
  formula = self.class.new(cloned_obj)
  formula.left_right_wrapper = @left_right_wrapper
  formula
end

#element_order=(value) ⇒ Object



310
311
312
# File 'lib/plurimath/math/formula.rb', line 310

def element_order=(value)
  @value = validated_order(value)
end

#extract_class_name_from_textObject



234
235
236
237
238
# File 'lib/plurimath/math/formula.rb', line 234

def extract_class_name_from_text
  return unless value.length < 2 && value.first.is_a?(Function::Text)

  value.first.parameter_one
end

#insert(values) ⇒ Object



295
296
297
# File 'lib/plurimath/math/formula.rb', line 295

def insert(values)
  update(Array(value) + values)
end

#intent=(value) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/plurimath/math/formula.rb', line 342

def intent=(value)
  return unless value

  self.content = nil
  update(
    [
      Function::Intent.new(
        filter_values(@value, array_to_instance: true),
        Function::Text.new(value),
      )
    ]
  )
end

#intent_namesObject



303
304
305
306
307
308
# File 'lib/plurimath/math/formula.rb', line 303

def intent_names
  {
    partial_derivative: ":partial-derivative",
    derivative: ":derivative",
  }
end

#line_breaked_mathml(display_style, intent, options:) ⇒ Object



80
81
82
83
84
# File 'lib/plurimath/math/formula.rb', line 80

def line_breaked_mathml(display_style, intent, options:)
  new_line_support.map do |formula|
    formula.to_mathml(display_style: display_style, intent: intent, formatter: options[:formatter])
  end.join
end

#line_breaking(obj) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/plurimath/math/formula.rb', line 271

def line_breaking(obj)
  if result.size > 1
    breaked_result = result.first.last.omml_line_break(result)
    update(Array(breaked_result.shift))
    obj.update(breaked_result.flatten)
    reprocess_value(obj)
    return
  end

  value.each.with_index(1) do |object, index|
    object.line_breaking(obj)
    break obj.insert(value.slice!(index..value.size)) if obj.value_exist?
  end
end

#maligngroup_value=(value) ⇒ Object



536
537
538
# File 'lib/plurimath/math/formula.rb', line 536

def maligngroup_value=(value)
  @value&.delete("maligngroup")
end

#malignmark_value=(value) ⇒ Object



564
565
566
# File 'lib/plurimath/math/formula.rb', line 564

def malignmark_value=(value)
  @value&.delete("malignmark")
end

#mathcolor=(value) ⇒ Object

Attributes start



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/plurimath/math/formula.rb', line 315

def mathcolor=(value)
  return if value.nil? || value.empty?

  update(
    [
      Math::Function::Color.new(
        Math::Function::Text.new(value),
        filter_values(@value, array_to_instance: true),
      )
    ]
  )
end

#mathml_content(intent, options:) ⇒ Object



97
98
99
100
101
# File 'lib/plurimath/math/formula.rb', line 97

def mathml_content(intent, options:)
  nodes = value.map { |val| val.to_mathml_without_math_tag(intent, options: options) }
  intent_post_processing(nodes, intent) if intent
  nodes
end

#mathvariant=(value) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/plurimath/math/formula.rb', line 328

def mathvariant=(value)
  return if value.nil? || value.empty?
  return unless Plurimath::Utility::FONT_STYLES.key?(value.to_sym)

  update(
    [
      Plurimath::Utility::FONT_STYLES[value.to_sym].new(
        filter_values(@value, array_to_instance: true),
        value,
      )
    ]
  )
end

#menclose_value=(value) ⇒ Object



540
541
542
# File 'lib/plurimath/math/formula.rb', line 540

def menclose_value=(value)
  update_temp_order(value, "menclose")
end

#merror_value=(value) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
# File 'lib/plurimath/math/formula.rb', line 516

def merror_value=(value)
  return if value.nil? || value.empty?

  update(
    replace_order_with_value(
      @value,
      filter_values(update_temp_mathml_values(value)),
      "merror"
    )
  )
end

#mfenced_value=(value) ⇒ Object



484
485
486
# File 'lib/plurimath/math/formula.rb', line 484

def mfenced_value=(value)
  update_temp_order(value, "mfenced")
end

#mfrac_value=(value) ⇒ Object



476
477
478
# File 'lib/plurimath/math/formula.rb', line 476

def mfrac_value=(value)
  update_temp_order(value, "mfrac")
end

#mfraction_value=(value) ⇒ Object



572
573
574
# File 'lib/plurimath/math/formula.rb', line 572

def mfraction_value=(value)
  update_temp_order(value, "mfraction")
end

#mglyph_value=(value) ⇒ Object



584
585
586
# File 'lib/plurimath/math/formula.rb', line 584

def mglyph_value=(value)
  update_temp_order(value, "mglyph")
end

#mi_value=(value) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/plurimath/math/formula.rb', line 369

def mi_value=(value)
  return if value.nil? || value.empty?

  value = update_temp_mathml_values(value) if value.any? do |val|
    val.is_a?(Math::Core) && val.temp_mathml_order.any?
  end
  update(
    replace_order_with_value(
      @value,
      Array(validate_symbols(value)).flatten,
      "mi"
    )
  )
end

#mini_sized?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/plurimath/math/formula.rb', line 299

def mini_sized?
  true if value&.first&.mini_sized?
end

#mlongdiv_value=(value) ⇒ Object



528
529
530
# File 'lib/plurimath/math/formula.rb', line 528

def mlongdiv_value=(value)
  update_temp_order(value, "mlongdiv")
end

#mmultiscripts_value=(value) ⇒ Object



576
577
578
# File 'lib/plurimath/math/formula.rb', line 576

def mmultiscripts_value=(value)
  update_temp_order(value, "mmultiscripts")
end

#mn_value=(value) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
# File 'lib/plurimath/math/formula.rb', line 384

def mn_value=(value)
  return if value.nil? || value.empty?

  update(
    replace_order_with_value(
      @value,
      Array(validate_symbols(value)),
      "mn"
    )
  )
end

#mo_value=(value) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/plurimath/math/formula.rb', line 408

def mo_value=(value)
  return if value.nil? || value.empty?

  value = update_temp_mathml_values(value)
  update(
    replace_order_with_value(
      @value,
      Array(value),
      "mo"
    )
  )
end

#mover_value=(value) ⇒ Object



464
465
466
# File 'lib/plurimath/math/formula.rb', line 464

def mover_value=(value)
  update_temp_order(value, "mover")
end

#mpadded_value=(value) ⇒ Object



568
569
570
# File 'lib/plurimath/math/formula.rb', line 568

def mpadded_value=(value)
  update_temp_order(value, "mpadded")
end

#mphantom_value=(value) ⇒ Object



580
581
582
# File 'lib/plurimath/math/formula.rb', line 580

def mphantom_value=(value)
  update_temp_order(value, "mphantom")
end

#mroot_value=(value) ⇒ Object



488
489
490
# File 'lib/plurimath/math/formula.rb', line 488

def mroot_value=(value)
  update_temp_order(value, "mroot")
end

#mrow_value=(value) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/plurimath/math/formula.rb', line 435

def mrow_value=(value)
  return if value.nil? || value.empty?

  replacing_order = value.length > 1 && value.any?(String)
  update(
    replace_order_with_value(
      @value,
      filter_values(
        value,
        array_to_instance: true,
        replacing_order: replacing_order
      ),
      "mrow"
    )
  )
end

#ms_value=(value) ⇒ Object

Attributes end



357
358
359
360
361
362
363
364
365
366
367
# File 'lib/plurimath/math/formula.rb', line 357

def ms_value=(value)
  return if value.nil? || value.empty?

  update(
    replace_order_with_value(
      @value,
      Array(validate_symbols(value)),
      "ms"
    )
  )
end

#mscarries_value=(value) ⇒ Object



496
497
498
# File 'lib/plurimath/math/formula.rb', line 496

def mscarries_value=(value)
  update_temp_order(value, "mscarries")
end

#msgroup_value=(value) ⇒ Object



492
493
494
# File 'lib/plurimath/math/formula.rb', line 492

def msgroup_value=(value)
  update_temp_order(value, "msgroup")
end

#msline_value=(value) ⇒ Object



500
501
502
# File 'lib/plurimath/math/formula.rb', line 500

def msline_value=(value)
  update_temp_order(value, "msline")
end

#mspace_value=(value) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/plurimath/math/formula.rb', line 544

def mspace_value=(value)
  return if value.nil? || value.empty?

  if value.first.linebreak
    linebreak = Math::Function::Linebreak.new(
      nil,
      { linebreak: value.first.linebreak }
    )
    update(
      replace_order_with_value(
        @value,
        [linebreak],
        "mspace"
      )
    )
  else
    @value&.delete("mspace")
  end
end

#msqrt_value=(value) ⇒ Object



480
481
482
# File 'lib/plurimath/math/formula.rb', line 480

def msqrt_value=(value)
  update_temp_order(value, "msqrt")
end

#msrow_value=(value) ⇒ Object



504
505
506
# File 'lib/plurimath/math/formula.rb', line 504

def msrow_value=(value)
  update_temp_order(value, "msrow")
end

#mstack_value=(value) ⇒ Object



512
513
514
# File 'lib/plurimath/math/formula.rb', line 512

def mstack_value=(value)
  update_temp_order(value, "mstack")
end

#mstyle_value=(value) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/plurimath/math/formula.rb', line 421

def mstyle_value=(value)
  return if value.empty?

  update(
    filter_values(
      replace_order_with_value(
        @value,
        Array(filter_values(value, array_to_instance: true)),
        "mstyle"
      )
    )
  )
end

#msub_value=(value) ⇒ Object



456
457
458
# File 'lib/plurimath/math/formula.rb', line 456

def msub_value=(value)
  update_temp_order(value, "msub")
end

#msubsup_value=(value) ⇒ Object



472
473
474
# File 'lib/plurimath/math/formula.rb', line 472

def msubsup_value=(value)
  update_temp_order(value, "msubsup")
end

#msup_value=(value) ⇒ Object



460
461
462
# File 'lib/plurimath/math/formula.rb', line 460

def msup_value=(value)
  update_temp_order(value, "msup")
end

#mtext_value=(value) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
# File 'lib/plurimath/math/formula.rb', line 396

def mtext_value=(value)
  return if value.nil? || value.empty?

  update(
    replace_order_with_value(
      @value,
      Array(validate_symbols(value)),
      "mtext"
    )
  )
end

#munder_value=(value) ⇒ Object



468
469
470
# File 'lib/plurimath/math/formula.rb', line 468

def munder_value=(value)
  update_temp_order(value, "munder")
end

#munderover_value=(value) ⇒ Object



452
453
454
# File 'lib/plurimath/math/formula.rb', line 452

def munderover_value=(value)
  update_temp_order(value, "munderover")
end

#nary_attr_value(options:) ⇒ Object



240
241
242
# File 'lib/plurimath/math/formula.rb', line 240

def nary_attr_value(options:)
  value.first.nary_attr_value(options: Hash.new(options))
end

#new_line_support(array = []) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/plurimath/math/formula.rb', line 263

def new_line_support(array = [])
  cloned = cloned_objects
  obj = self.class.new
  cloned.line_breaking(obj)
  array << cloned
  obj.value_exist? ? obj.new_line_support(array) : array
end

#none_value=(_) ⇒ Object



532
533
534
# File 'lib/plurimath/math/formula.rb', line 532

def none_value=(_)
  @value&.delete("none")
end

#omml_attrsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/plurimath/math/formula.rb', line 117

def omml_attrs
  {
    "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
    "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
    "xmlns:mo": "http://schemas.microsoft.com/office/mac/office/2008/main",
    "xmlns:mv": "urn:schemas-microsoft-com:mac:vml",
    "xmlns:o": "urn:schemas-microsoft-com:office:office",
    "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
    "xmlns:v": "urn:schemas-microsoft-com:vml",
    "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
    "xmlns:w10": "urn:schemas-microsoft-com:office:word",
    "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
    "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
    "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
    "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
    "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
    "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
    "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
    "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
    "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
  }
end

#omml_content(display_style, options:) ⇒ Object



158
159
160
# File 'lib/plurimath/math/formula.rb', line 158

def omml_content(display_style, options:)
  value&.map { |val| val.insert_t_tag(display_style, options: options) }
end

#reprocess_value(obj) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/plurimath/math/formula.rb', line 286

def reprocess_value(obj)
  new_obj = self.class.new([])
  self.line_breaking(new_obj)
  if new_obj.value_exist?
    obj.value.insert(0, Function::Linebreak.new)
    obj.value.insert(0, self.class.new(new_obj.value))
  end
end

#semantics_value=(value) ⇒ Object



508
509
510
# File 'lib/plurimath/math/formula.rb', line 508

def semantics_value=(value)
  update_temp_order(value, "semantics")
end

#to_asciimath(formatter: nil, options: nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/plurimath/math/formula.rb', line 48

def to_asciimath(formatter: nil, options: nil)
  options ||= { formatter: formatter }
  value.map { |val| val.to_asciimath(options: options) }.join(" ")
rescue
  parse_error!(:asciimath)
end

#to_asciimath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object



199
200
201
202
203
204
# File 'lib/plurimath/math/formula.rb', line 199

def to_asciimath_math_zone(spacing = "", last = false, indent = true, options:)
  filtered_values(value, lang: :asciimath).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_asciimath_math_zone(new_space(spacing, indent), last, indent, options: options)
  end
end

#to_display(type = nil, formatter: nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/plurimath/math/formula.rb', line 173

def to_display(type = nil, formatter: nil)
  options = { formatter: formatter }
  return type_error!(type) unless MATH_ZONE_TYPES.include?(type.downcase.to_sym)

  math_zone = case type
              when :asciimath
                "  |_ \"#{to_asciimath(options: options)}\"\n#{to_asciimath_math_zone("     ", options: options).join}"
              when :latex
                "  |_ \"#{to_latex(options: options)}\"\n#{to_latex_math_zone("     ", options: options).join}"
              when :mathml
                mathml = to_mathml(formatter: formatter).gsub(/\n\s*/, "")
                math_display = to_mathml_math_zone("     ", options: options).join
                "  |_ \"#{mathml}\"\n#{math_display}"
              when :omml
                omml = to_omml.gsub(/\n\s*/, "")
                omml_display = to_omml_math_zone("     ", display_style: displaystyle, options: options).join
                "  |_ \"#{omml}\"\n#{omml_display}"
              when :unicodemath
                "  |_ \"#{to_unicodemath(options: options)}\"\n#{to_unicodemath_math_zone("     ", options: options).join}"
              end
  <<~MATHZONE.sub(/\n$/, "")
  |_ Math zone
  #{math_zone}
  MATHZONE
end

#to_html(formatter: nil, options: nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/plurimath/math/formula.rb', line 110

def to_html(formatter: nil, options: nil)
  options ||= { formatter: formatter }
  value&.map { |val| val.to_html(options: options) }&.join(" ")
rescue
  parse_error!(:html)
end

#to_latex(formatter: nil, options: nil) ⇒ Object



103
104
105
106
107
108
# File 'lib/plurimath/math/formula.rb', line 103

def to_latex(formatter: nil, options: nil)
  options ||= { formatter: formatter }
  value.map { |val| val.to_latex(options: options) }.join(" ")
rescue
  parse_error!(:latex)
end

#to_latex_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object



206
207
208
209
210
211
# File 'lib/plurimath/math/formula.rb', line 206

def to_latex_math_zone(spacing = "", last = false, indent = true, options:)
  filtered_values(value, lang: :latex).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_latex_math_zone(new_space(spacing, indent), last, indent, options: options)
  end
end

#to_mathml(intent: false, formatter: nil, unitsml_xml: nil, split_on_linebreak: false, display_style: displaystyle) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/plurimath/math/formula.rb', line 55

def to_mathml(
  intent: false,
  formatter: nil,
  unitsml_xml: nil,
  split_on_linebreak: false,
  display_style: displaystyle
)
  options = { formatter: formatter, unitsml_xml: unitsml_xml }
  return line_breaked_mathml(display_style, intent, options: options) if split_on_linebreak

  math_attrs = {
    xmlns: "http://www.w3.org/1998/Math/MathML",
    display: "block",
  }
  style_attrs = { displaystyle: boolean_display_style(display_style) }
  math  = ox_element("math", attributes: math_attrs)
  style = ox_element("mstyle", attributes: style_attrs)
  Utility.update_nodes(style, mathml_content(intent, options: options))
  Utility.update_nodes(math, [style])
  unitsml_post_processing(math.nodes, math)
  dump_nodes(math, indent: 2)
rescue
  parse_error!(:mathml)
end

#to_mathml_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object



213
214
215
216
217
218
# File 'lib/plurimath/math/formula.rb', line 213

def to_mathml_math_zone(spacing = "", last = false, indent = true, options:)
  filtered_values(value, lang: :mathml, options: options).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_mathml_math_zone(new_space(spacing, indent), last, indent, options: options)
  end
end

#to_mathml_without_math_tag(intent, options:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/plurimath/math/formula.rb', line 86

def to_mathml_without_math_tag(intent, options:)
  return mathml_content(intent, options: options) unless left_right_wrapper

  mathml_value = mathml_content(intent, options: options)
  attributes = intent_attribute(mathml_value) if intent
  mrow = ox_element("mrow", attributes: attributes)
  mrow[:unitsml] = true if unitsml
  mathml_value += wrapped_unitsml_xml(mrow) if unitsml_xml && options[:unitsml_xml]
  Utility.update_nodes(mrow, mathml_value)
end

#to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/plurimath/math/formula.rb', line 140

def to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil)
  objects = split_on_linebreak ? new_line_support : [self]
  options = { formatter: formatter }
  para_element = Utility.ox_element("oMathPara", attributes: omml_attrs, namespace: "m")
  objects.each.with_index(1) do |object, index|
    para_element << Utility.update_nodes(
      Utility.ox_element("oMath", namespace: "m"),
      object.omml_content(boolean_display_style(display_style), options: options),
    )
    next if objects.length == index

    para_element << omml_br_tag
  end
  dump_nodes(para_element, indent: 2)
rescue
  parse_error!(:omml)
end

#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:, options:) ⇒ Object



220
221
222
223
224
225
# File 'lib/plurimath/math/formula.rb', line 220

def to_omml_math_zone(spacing = "", last = false, indent = true, display_style:, options:)
  filtered_values(value, lang: :omml).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_omml_math_zone(new_space(spacing, indent), last, indent, display_style: display_style, options: options)
  end
end

#to_omml_without_math_tag(display_style, options:) ⇒ Object



162
163
164
# File 'lib/plurimath/math/formula.rb', line 162

def to_omml_without_math_tag(display_style, options:)
  omml_content(display_style, options: options)
end

#to_unicodemath(formatter: nil, options: nil) ⇒ Object



166
167
168
169
170
171
# File 'lib/plurimath/math/formula.rb', line 166

def to_unicodemath(formatter: nil, options: nil)
  options ||= { formatter: formatter }
  Utility.html_entity_to_unicode(unicodemath_value(options: options)).gsub(/\s\/\s/, "/")
rescue
  parse_error!(:unicodemath)
end

#to_unicodemath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object



227
228
229
230
231
232
# File 'lib/plurimath/math/formula.rb', line 227

def to_unicodemath_math_zone(spacing = "", last = false, indent = true, options:)
  filtered_values(value, lang: :unicodemath).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_unicodemath_math_zone(new_space(spacing, indent), last, indent, options: options)
  end
end

#update(object) ⇒ Object



252
253
254
# File 'lib/plurimath/math/formula.rb', line 252

def update(object)
  @value = Array(object).flatten.compact
end

#validate_function_formulaObject



244
245
246
# File 'lib/plurimath/math/formula.rb', line 244

def validate_function_formula
  (value.none?(Function::Left) || value.none?(Function::Right))
end

#value_exist?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/plurimath/math/formula.rb', line 248

def value_exist?
  value && !value.empty?
end