class AsciiMath::MathMLBuilder

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method AsciiMath::MarkupBuilder::new
# File lib/asciimath/mathml.rb, line 7
def initialize(opts = {})
  super(opts[:symbol_table] || ::AsciiMath::MarkupBuilder.default_display_symbol_table(fix_phi: opts.fetch(:fix_phi, true)))
  @prefix = opts[:prefix] || ''
  @mathml = ''
  if opts[:msword]
    @row_mode = :force
    @fence_mode = :fenced
  else
    @row_mode = :avoid
    @fence_mode = :row
  end
  @escape_non_ascii = opts.fetch(:escape_non_ascii, true)
end

Public Instance Methods

append_expression(expression, attrs = {}) click to toggle source
# File lib/asciimath/mathml.rb, line 25
def append_expression(expression, attrs = {})
  math('', attrs) do
    append(expression, :row => :omit)
  end
end
to_s() click to toggle source
# File lib/asciimath/mathml.rb, line 21
def to_s
  @mathml
end

Private Instance Methods

append_cancel(expression) click to toggle source
# File lib/asciimath/mathml.rb, line 61
def append_cancel(expression)
  tag("menclose", :notation => "updiagonalstrike") do
    append(expression, :row => :omit)
  end
end
append_color(color, e) click to toggle source
# File lib/asciimath/mathml.rb, line 88
def append_color(color, e)
  tag("mstyle", :mathcolor => color) do
    append(e)
  end
end
append_escaped(text) click to toggle source
# File lib/asciimath/mathml.rb, line 235
def append_escaped(text)
  text.each_codepoint do |cp|
    if cp == 38
      @mathml << "&amp;"
    elsif cp == 60
      @mathml << "&lt;"
    elsif cp == 62
      @mathml << "&gt;"
    elsif cp > 127 && @escape_non_ascii
      @mathml << "&#x#{cp.to_s(16).upcase};"
    else
      @mathml << cp
    end
  end
end
append_font(style, e) click to toggle source
# File lib/asciimath/mathml.rb, line 82
def append_font(style, e)
  tag("mstyle", :mathvariant => style.to_s.gsub('_', '-')) do
    append(e)
  end
end
append_fraction(numerator, denominator) click to toggle source
# File lib/asciimath/mathml.rb, line 74
def append_fraction(numerator, denominator)
  tag("m#{"frac"}") do
    append(numerator, :row => @row_mode)
    append(denominator, :row => @row_mode)
  end
end
append_identifier(identifier) click to toggle source
# File lib/asciimath/mathml.rb, line 43
def append_identifier(identifier)
  mi(identifier)
end
append_identifier_unary(identifier, expression) click to toggle source
# File lib/asciimath/mathml.rb, line 117
def append_identifier_unary(identifier, expression)
  mrow do
    mi(identifier)
    append(expression, :row => @row_mode)
  end
end
append_matrix(lparen, rows, rparen) click to toggle source
# File lib/asciimath/mathml.rb, line 94
def append_matrix(lparen, rows, rparen)
  fenced(lparen, rparen) do
    mtable do
      rows.each do |row|
        mtr do
          row.each do |col|
            mtd do
              append(col)
            end
          end
        end
      end
    end
  end
end
append_number(number) click to toggle source
# File lib/asciimath/mathml.rb, line 51
def append_number(number)
  mn(number)
end
append_operator(operator) click to toggle source
# File lib/asciimath/mathml.rb, line 39
def append_operator(operator)
  mo(operator)
end
append_operator_unary(operator, expression) click to toggle source
# File lib/asciimath/mathml.rb, line 110
def append_operator_unary(operator, expression)
  mrow do
    mo(operator)
    append(expression, :row => @row_mode)
  end
end
append_paren(lparen, e, rparen, opts = {}) click to toggle source
# File lib/asciimath/mathml.rb, line 124
def append_paren(lparen, e, rparen, opts = {})
  fenced(lparen, rparen) do
    append(e, :row => @row_mode)
  end
end
append_root(base, index) click to toggle source
# File lib/asciimath/mathml.rb, line 67
def append_root(base, index)
  tag("m#{"root"}") do
    append(base, :row => @row_mode)
    append(index, :row => @row_mode)
  end
end
append_row(expressions) click to toggle source
# File lib/asciimath/mathml.rb, line 33
def append_row(expressions)
  mrow do
    expressions.each { |e| append(e) }
  end
end
append_sqrt(expression) click to toggle source
# File lib/asciimath/mathml.rb, line 55
def append_sqrt(expression)
  tag("m#{"sqrt"}") do
    append(expression, :row => @row_mode)
  end
end
append_subsup(base, sub, sup) click to toggle source
# File lib/asciimath/mathml.rb, line 130
def append_subsup(base, sub, sup)
  if sub && sup
    msubsup do
      append(base, :row => @row_mode)
      append(sub, :row => @row_mode)
      append(sup, :row => @row_mode)
    end
  elsif sub
    msub do
      append(base, :row => @row_mode)
      append(sub, :row => @row_mode)
    end
  elsif sup
    msup do
      append(base, :row => @row_mode)
      append(sup, :row => @row_mode)
    end
  else
    append(base)
  end
end
append_text(text) click to toggle source
# File lib/asciimath/mathml.rb, line 47
def append_text(text)
  mtext(text)
end
append_underover(base, sub, sup) click to toggle source
# File lib/asciimath/mathml.rb, line 152
def append_underover(base, sub, sup)
  attrs = {}

  sub_row_mode = @row_mode
  if is_accent(sub)
    attrs[:accentunder] = true
    sub_row_mode = :avoid
  end

  sup_row_mode = @row_mode
  if is_accent(sup)
    attrs[:accent] = true
    sup_row_mode = :avoid
  end



  if sub && sup
    munderover(attrs) do
      append(base, :row => @row_mode)
      append(sub, :row => sub_row_mode)
      append(sup, :row => sup_row_mode)
    end
  elsif sub
    munder(attrs) do
      append(base, :row => @row_mode)
      append(sub, :row => sub_row_mode)
    end
  elsif sup
    mover(attrs) do
      append(base, :row => @row_mode)
      append(sup, :row => sup_row_mode)
    end
  else
    append(base)
  end
end
fenced(lparen, rparen) { |self| ... } click to toggle source
# File lib/asciimath/mathml.rb, line 194
def fenced(lparen, rparen)
  if lparen || rparen
    if @fence_mode == :fenced
      mfenced(:open => lparen || '', :close => rparen || '') do
        yield self
      end
    else
      mrow do
        mo(lparen) if lparen
        yield self
        mo(rparen) if rparen
      end
    end
  else
    yield self
  end
end
method_missing(meth, *args, &block) click to toggle source
# File lib/asciimath/mathml.rb, line 190
def method_missing(meth, *args, &block)
  tag(meth, *args, &block)
end
tag(tag, *args) { |self| ... } click to toggle source
# File lib/asciimath/mathml.rb, line 212
def tag(tag, *args)
  attrs = args.last.is_a?(Hash) ? args.pop : {}
  text = args.last.is_a?(String) || args.last.is_a?(Symbol) ? args.pop.to_s : ''

  @mathml << '<' << @prefix << tag.to_s

  attrs.each_pair do |key, value|
    @mathml << ' ' << key.to_s << '="'
    append_escaped(value.to_s)
    @mathml << '"'
  end


  if block_given? || text
    @mathml << '>'
    append_escaped(text)
    yield self if block_given?
    @mathml << '</' << @prefix << tag.to_s << '>'
  else
    @mathml << '/>'
  end
end