Class: Spirit::Render::Processors::MathProcessor

Inherits:
Base
  • Object
show all
Defined in:
lib/spirit/render/processors/math_processor.rb

Overview

Pre-processes math markup in latex. Adapted from www.math.union.edu/~dpvc/transfer/mathjax/mathjax-editing.js

Constant Summary collapse

SPLIT =

Pattern for delimiters and special symbols; used to search for math in the document.

/(\$\$?|                        (?# 1 or 2 dollars)
   \\(?:begin|end)\{[a-z]*\*?\}| (?# latex envs)
   \\[\\{}$]|                    (?# \\ \{ \})
   [{}]|                         (?# braces )
   (?:\n\s*)+|                   (?# newlines w. optional spaces)
   @@\d+@@)                      (?# @@digits@@)
/ix

Instance Method Summary collapse

Methods inherited from Base

events, inherited, #invoke_callbacks_for, process

Constructor Details

#initialize(renderer, document) ⇒ MathProcessor

Returns a new instance of MathProcessor.



23
24
25
26
27
# File 'lib/spirit/render/processors/math_processor.rb', line 23

def initialize(renderer, document)
  reset_counters
  @math   = []
  @blocks = document.gsub(/\r\n?/, "\n").split SPLIT
end

Instance Method Details

#filter(document) ⇒ String

Replace math in document with @@index@@.

Returns:

  • (String)

    document



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spirit/render/processors/math_processor.rb', line 31

def filter(document)
  blocks.each_with_index do |block, i|
    case
    when '@' == block[0]
      process_pseudo_marker block, i
    when @start
      process_potential_close block, i
    else
      process_potential_start block, i
    end
  end
  process_math if @last
  blocks.join
end

#replace(document) ⇒ Object



46
47
48
# File 'lib/spirit/render/processors/math_processor.rb', line 46

def replace(document)
  document.gsub(/@@(\d+)@@/) { math[$1.to_i] }
end