/itexToMML

To download this project, use:
bzr branch http://golem.ph.utexas.edu/~distler/code/itexToMML/

« back to all changes in this revision

Viewing changes to itex-src/itextomml.rb

  • Committer: Jacques Distler
  • Date: 2007-01-11 07:17:18 UTC
  • Revision ID: distler@golem.ph.utexas.edu-20070111071718-3azxrr5b29d00h26
Initial commit.

Show diffs side-by-side

added added

removed removed

 
1
#  Ruby module for itex2MML
 
2
#
 
3
#  Installation
 
4
#  ------------
 
5
#    1. You need SWIG (>= 1.3) and GNU Make to install
 
6
#       the Ruby module.
 
7
#    2. Then type:
 
8
#            make ruby
 
9
#             make test_ruby
 
10
#            make install_ruby
 
11
#    3. This module can then be invoked like:
 
12
#
 
13
#            require 'itextomml'
 
14
#            
 
15
#            itex = Itex2MML::Parser.new
 
16
#            itex.html_filter(string)
 
17
#
 
18
#  Usage
 
19
#  -----
 
20
#
 
21
#      There are two public methods
 
22
#
 
23
#       itex.html_filter(a_string)
 
24
#               converts all itex equations in a_string to MathML, passing the
 
25
#               rest of a_string unmodified. Returns the converted string. 
 
26
#
 
27
#       itex.filter(a_string)
 
28
#               converts all itex equations in a_string to MathML. Returns just
 
29
#               the MathML equation(s), as a string.
 
30
#
 
31
#  Author: Justin Bonnar <jbonnar@berkeley.edu>
 
32
#  Placed in the Public Domain
 
33
 
 
34
require 'itex2MML'
 
35
require 'thread'
 
36
 
 
37
module Itex2MML
 
38
  class Error < RuntimeError; end
 
39
 
 
40
  class Parser
 
41
    def self.semaphore
 
42
      @semaphore ||= Mutex.new
 
43
    end
 
44
   
 
45
    def html_filter(string)
 
46
      parse(string, :itex2MML_html_filter)
 
47
    end
 
48
   
 
49
    def filter(string)
 
50
      parse(string, :itex2MML_filter)
 
51
    end
 
52
 
 
53
  private
 
54
 
 
55
    def parse(string, message)
 
56
      string = string.to_str
 
57
      self.class.semaphore.synchronize do
 
58
        raise Itex2MML::Error unless Itex2MML.send(message, string, string.length) == 0
 
59
        Itex2MML.itex2MML_output
 
60
      end
 
61
    end
 
62
  end
 
63
end
 
64
 
 
65
## Unit Tests ##
 
66
if __FILE__ == $0
 
67
  require 'test/unit'
 
68
 
 
69
  class Itex2MMLTest < Test::Unit::TestCase
 
70
 
 
71
    def test_inline_html
 
72
      itex = Itex2MML::Parser.new
 
73
      assert_equal("Inline: <math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><mi>sin</mi><mo stretchy=\"false\">(</mo><mi>x</mi><mo stretchy=\"false\">)</mo></math>", itex.html_filter('Inline: $\sin(x)$'))
 
74
    end
 
75
 
 
76
    def test_inline
 
77
      itex = Itex2MML::Parser.new
 
78
      assert_equal("<math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><mi>sin</mi><mo stretchy=\"false\">(</mo><mi>x</mi><mo stretchy=\"false\">)</mo></math>", itex.filter('Inline: $\sin(x)$'))
 
79
    end
 
80
 
 
81
    def test_block_html
 
82
      itex = Itex2MML::Parser.new
 
83
      assert_equal("Block: <math xmlns='http://www.w3.org/1998/Math/MathML' display='block'><mi>sin</mi><mo stretchy=\"false\">(</mo><mi>x</mi><mo stretchy=\"false\">)</mo></math>", itex.html_filter('Block: $$\sin(x)$$'))
 
84
    end
 
85
 
 
86
    def test_block
 
87
      itex = Itex2MML::Parser.new
 
88
      assert_equal("<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'><mi>sin</mi><mo stretchy=\"false\">(</mo><mi>x</mi><mo stretchy=\"false\">)</mo></math>", itex.filter('Block: $$\sin(x)$$'))
 
89
    end
 
90
 
 
91
  end
 
92
end