Permalink
Browse files

add rudimentary maths mode support

  • Loading branch information...
inukshuk committed Apr 7, 2013
1 parent 8fb98b0 commit e33a6d2ff9383be23d8ce6f9de33066370f56e0b
Showing with 79 additions and 20 deletions.
  1. +2 −1 Gemfile
  2. +7 −0 features/maths.feature
  3. +12 −9 lib/latex/decode.rb
  4. +0 −1 lib/latex/decode/base.rb
  5. +37 −9 lib/latex/decode/compatibility.rb
  6. +21 −0 lib/latex/decode/maths.rb
View
@@ -1,7 +1,6 @@
source 'https://rubygems.org'
gemspec
-
group :test do
gem 'rake'
gem 'rspec', '~> 2.13'
@@ -14,3 +13,5 @@ group :debug do
end
gem 'unicode', '~> 0.4', :platforms => [:mri, :rbx, :mswin, :mingw]
+
+gem 'ritex', '~> 1.0.1'
View
@@ -0,0 +1,7 @@
+Feature: Convert LaTeX maths to MathML
+ As a maths-inclined hacker who works with LaTeX
+ I want to convert LaTeX maths to MathML
+
+ Scenario: Inline maths environment
+ When I decode the string '$I_{S}A$'
+ Then the result should be '<msub><mi>I</mi><mrow><mi>S</mi></mrow></msub><mi>A</mi>'
View
@@ -1,18 +1,18 @@
#--
# LaTeX::Decode
-# Copyright (C) 2011 Sylvester Keil <sylvester.keil.or.at>
+# Copyright (C) 2011-2013 Sylvester Keil <sylvester.keil.or.at>
# Copyright (C) 2010 François Charette
-#
+#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
-#
+#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-#
+#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
@@ -23,26 +23,29 @@
require 'latex/decode/accents'
require 'latex/decode/diacritics'
+require 'latex/decode/maths'
require 'latex/decode/punctuation'
require 'latex/decode/symbols'
module LaTeX
-
+
class << self
def decode (string)
return string unless string.respond_to?(:to_s)
string = string.is_a?(String) ? string.dup : string.to_s
-
+
Decode::Base.normalize(string)
-
+
+ Decode::Maths.decode!(string)
+
Decode::Accents.decode!(string)
Decode::Diacritics.decode!(string)
Decode::Punctuation.decode!(string)
Decode::Symbols.decode!(string)
-
+
Decode::Base.strip_braces(string)
-
+
LaTeX.normalize_C(string)
end
end
View
@@ -20,7 +20,6 @@ def decode (string)
end
def decode! (string)
- puts name unless patterns
patterns.each do |pattern|
string.gsub!(pattern) { |m| [$2,map[$1],$3].compact.join }
end
@@ -14,8 +14,8 @@ def ruby_19; false; end
module LaTeX
def self.to_unicode(string)
- string
- end
+ string
+ end
end
def ruby_18; false; end
@@ -33,17 +33,16 @@ def self.normalize_C(string)
end
else
-
- begin
- require 'unicode'
+ begin
+ require 'unicode'
# Use the Unicode gem
module LaTeX
def self.normalize_C(string)
Unicode::normalize_C(string)
end
end
- rescue LoadError
+ rescue LoadError
begin
require 'active_support/multibyte/chars'
@@ -54,8 +53,37 @@ def self.normalize_C(string)
end
end
rescue LoadError
- fail "Failed to load unicode normalizer: please gem install unicode (or active_support)"
+ fail "Failed to load unicode normalizer: please gem install unicode (or active_support)"
+ end
+ end
+end
+
+module LaTeX
+ begin
+ require 'ritex'
+
+ def self.ritex
+ Ritex::Parser.new(:mathml)
end
- end
-end
+ def self.to_math_ml(string)
+ ritex.parse string, :nowrap => true, :display => false
+ end
+
+ rescue LoadError
+ begin
+ require 'math_ml'
+
+ def self.to_math_ml(string)
+ MathML::String.mathml_latex_parser.parse(string, false)
+ end
+
+ rescue LoadError
+ # No MathML conversion
+
+ def self.to_math_ml(string)
+ string
+ end
+ end
+ end
+end
View
@@ -0,0 +1,21 @@
+module LaTeX
+ module Decode
+
+ class Maths < Decoder
+ @patterns = [
+ /\$([^\$]+)\$/
+ ].freeze
+
+ def self.decode! (string)
+ patterns.each do |pattern|
+ string.gsub!(pattern) do
+ LaTeX.to_math_ml($1)
+ end
+ end
+ string
+ end
+
+ end
+
+ end
+end

0 comments on commit e33a6d2

Please sign in to comment.