texmath-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58d0d834d41e8dedcde7b731f4aaadb325c2c3b5
4
+ data.tar.gz: 4f56df014bc172c78ed1c2ce8de6e400f67903fa
5
+ SHA512:
6
+ metadata.gz: 68bc2a4a95ced0348f1ddf8be62ec1caaac43a0de3cebf2e59e573e490592c32f898abb9c137395149b9fbb407273026c829f9ae24c38d0e1335fa857e490669
7
+ data.tar.gz: 1b79da5a96331ead2c38f5738ddfd74880443b8cadb1ddc510491d4b4824f49d0d8a4eb34511e4f9af81a4e1b655b43cab1fdcafa991f7ab6747c62228f27ef7
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.2
5
+ - ruby-head
6
+ - jruby-20mode # JRuby in 1.9 mode
7
+ - rbx
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+ cache: bundler
12
+ before_install:
13
+ - sudo apt-get install haskell-platform
14
+ - cabal update
15
+ - cabal install texmath
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ end
8
+
9
+ group :development, :test do
10
+ gem 'bundler', '~> 1.6'
11
+ gem 'minitest', '~> 5.5'
12
+ gem 'rake'
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Casper Hollingberry
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # texmath-ruby
2
+
3
+ A wrapper for [TeXMath](https://github.com/jgm/texmath), a Haskell library for
4
+ converting between formats used to represent mathematics.
5
+
6
+ TeXMath can convert between LaTeX, presentation MathML, and OMML (Office Math
7
+ Markup Language). This wrapper uses the command line program for
8
+ texmath for all operations (rather than interface directly with the
9
+ Haskell library) and is heavily based on
10
+ [William Melody's wrapper](https://github.com/alphabetum/pandoc-ruby)
11
+ for [Pandoc](https://github.com/jgm/pandoc).
12
+
13
+ ## Installation
14
+
15
+ First, make sure [texmath](https://github.com/jgm/texmath) is installed. If you
16
+ already have [The Haskell Platform](https://www.haskell.org/platform/), you can
17
+ install texmath with Cabal:
18
+
19
+ ```shell
20
+ $ cabal install texmath
21
+ ```
22
+
23
+ If you use [Bundler](http://bundler.io), then add this line to your
24
+ application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'texmath-ruby', require: 'texmath'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ ```shell
33
+ $ bundle
34
+ ```
35
+
36
+ Or install it yourself as:
37
+
38
+ ```shell
39
+ $ gem install texmath-ruby
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ Once everything is installed correctly, using TeXMath is incredibly easy.
45
+
46
+ For example, converting from LaTeX to MathML looks like this:
47
+
48
+ ```ruby
49
+ TeXMath.convert('\sqrt{x^3}', from: :tex, to: :mathml)
50
+ # => '<math display="block" xmlns=...'
51
+ ```
52
+
53
+ TeXMath can convert between LaTeX, MathML, and OMML, in any
54
+ direction. By default (if either the source or destination format
55
+ isn't explicitly specified), TeXMath will try to convert from LaTeX to
56
+ MathML. So to convert from LaTeX (the default source format) to OMML:
57
+
58
+ ```ruby
59
+ TeXMath.convert('\sqrt{x^3}', to: :omml)
60
+ # => '<m:oMathPara>...'
61
+ ```
62
+
63
+ Or from OMML to MathML (the default destination format):
64
+
65
+ ```ruby
66
+ TeXMath.convert('<m:oMathPara>...', from: :omml)
67
+ # => '<math display="block" xmlns=...'
68
+ ```
69
+
70
+ TeXMath also comes with writers for XHTML and Pandoc's native format.
71
+
72
+ ## Supported formats
73
+
74
+ | Format | Symbol | Reader | Writer |
75
+ |-------------------------------|--------------|----------|----------|
76
+ | LaTeX | `:tex` | &#10004; | &#10004; |
77
+ | MathML | `:mathml` | &#10004; | &#10004; |
78
+ | Office Math Markup Language | `:omml` | &#10004; | &#10004; |
79
+ | TeXMath native | `:native` | &#10004; | &#10004; |
80
+ | XHTML | `:xhtml` | | &#10004; |
81
+ | Pandoc native | `:pandoc` | | &#10004; |
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/hollingberry/texmath-ruby/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default => :test
4
+
5
+ desc 'Run the tests'
6
+ task :test do
7
+ $LOAD_PATH.unshift 'lib', 'test'
8
+ Dir['test/**/*_test.rb'].each do |file|
9
+ require File.expand_path(file)
10
+ end
11
+ end
data/lib/texmath.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'texmath/converter'
2
+ require 'texmath/errors'
3
+ require 'texmath/version'
4
+
5
+ module TeXMath
6
+ def self.convert(data, options = {})
7
+ Converter.new(options).convert(data)
8
+ end
9
+ end
@@ -0,0 +1,78 @@
1
+ require 'texmath/errors'
2
+ require 'open3'
3
+
4
+ module TeXMath
5
+ ##
6
+ # A `Converter` represents a single `texmath` command to which data
7
+ # can be piped. For example, a `Converter` that converts LaTeX to
8
+ # MathML can be reused for different input expressions, although all
9
+ # would have to be valid LaTeX.
10
+ class Converter
11
+ ##
12
+ # The available readers and their corresponding names.
13
+ READERS = {
14
+ 'tex' => 'LaTeX',
15
+ 'mathml' => 'MathML',
16
+ 'omml' => 'Office Math Markup Language',
17
+ 'native' => 'texmath native'
18
+ }
19
+
20
+ ##
21
+ # The available writers and their corresponding names.
22
+ WRITERS = {
23
+ 'tex' => 'LaTeX',
24
+ 'mathml' => 'MathML',
25
+ 'omml' => 'Office Math Markup Language',
26
+ 'xhtml' => 'XHTML',
27
+ 'pandoc' => 'pandoc native',
28
+ 'native' => 'texmath native'
29
+ }
30
+
31
+ ##
32
+ # Create a new Converter.
33
+ # @param executable [String] the executable path
34
+ # @param from [Symbol] the source format
35
+ # @param to [Symbol] the destination format
36
+ def initialize(executable = 'texmath', from: :tex, to: :mathml)
37
+ @executable = executable
38
+ self.reader = from
39
+ self.writer = to
40
+ end
41
+
42
+ attr_reader :executable
43
+
44
+ ##
45
+ # Convert `data` between formats.
46
+ # @return [String] the converted data
47
+ def convert(data)
48
+ Open3.popen3(command) do |stdin, stdout, stderr|
49
+ stdin.puts(data)
50
+ stdin.close
51
+ output = stdout.read
52
+ error = stderr.read
53
+ raise ConversionError.new(error) unless error.empty?
54
+ return output.strip
55
+ end
56
+ rescue Errno::ENOENT
57
+ raise NoExecutableError.new("Can't find the '#{executable}' executable.")
58
+ end
59
+
60
+ attr_reader :reader, :writer
61
+
62
+ def reader=(format)
63
+ return @reader = format if READERS.has_key?(format.to_s)
64
+ raise InvalidReaderError.new("Can't find '#{format}' reader.")
65
+ end
66
+
67
+ def writer=(format)
68
+ return @writer = format if WRITERS.has_key?(format.to_s)
69
+ raise InvalidWriterError.new("Can't find '#{format}' writer.")
70
+ end
71
+
72
+ private
73
+
74
+ def command
75
+ "#{executable} -f #{reader} -t #{writer}"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ module TeXMath
2
+ ##
3
+ # Error raised when the underlying Haskell library writes to
4
+ # /dev/stderr. This usually occurs when the input was malformed.
5
+ class ConversionError < StandardError; end
6
+
7
+ ##
8
+ # Error raised when the `texmath` executable can't be found.
9
+ class NoExecutableError < StandardError; end
10
+
11
+ ##
12
+ # Error raised when an invalid reader or writer format is specified.
13
+ class InvalidFormatError < StandardError; end
14
+
15
+ ##
16
+ # Error raised when an invalid reader format is specified.
17
+ class InvalidReaderError < InvalidFormatError; end
18
+
19
+ ##
20
+ # Error raised when an invalid writer format is specified.
21
+ class InvalidWriterError < InvalidFormatError; end
22
+ end
@@ -0,0 +1,3 @@
1
+ module TeXMath
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,84 @@
1
+ module Formats
2
+ def tex
3
+ '\sqrt{x^{3}}'
4
+ end
5
+
6
+ def mathml
7
+ <<-XML.gsub(/^ /, '').strip
8
+ <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
9
+ <msqrt>
10
+ <msup>
11
+ <mi>x</mi>
12
+ <mn>3</mn>
13
+ </msup>
14
+ </msqrt>
15
+ </math>
16
+ XML
17
+ end
18
+
19
+ def omml
20
+ <<-XML.gsub(/^ /, '').strip
21
+ <m:oMathPara>
22
+ <m:oMathParaPr>
23
+ <m:jc m:val="center" />
24
+ </m:oMathParaPr>
25
+ <m:oMath>
26
+ <m:rad>
27
+ <m:radPr>
28
+ <m:degHide m:val="on" />
29
+ </m:radPr>
30
+ <m:deg />
31
+ <m:e>
32
+ <m:sSup>
33
+ <m:e>
34
+ <m:r>
35
+ <m:rPr>
36
+ <m:sty m:val="p" />
37
+ </m:rPr>
38
+ <m:t>x</m:t>
39
+ </m:r>
40
+ </m:e>
41
+ <m:sup>
42
+ <m:r>
43
+ <m:rPr>
44
+ <m:sty m:val="p" />
45
+ </m:rPr>
46
+ <m:t>3</m:t>
47
+ </m:r>
48
+ </m:sup>
49
+ </m:sSup>
50
+ </m:e>
51
+ </m:rad>
52
+ </m:oMath>
53
+ </m:oMathPara>
54
+ XML
55
+ end
56
+
57
+ def xhtml
58
+ <<-XML.gsub(/^ /, '').strip
59
+ <html xmlns="http://www.w3.org/1999/xhtml">
60
+ <head>
61
+ <meta content="application/xhtml+xml; charset=UTF-8" http-equiv="Content-Type" />
62
+ </head>
63
+ <body>
64
+ <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
65
+ <msqrt>
66
+ <msup>
67
+ <mi>x</mi>
68
+ <mn>3</mn>
69
+ </msup>
70
+ </msqrt>
71
+ </math>
72
+ </body>
73
+ </html>
74
+ XML
75
+ end
76
+
77
+ def pandoc
78
+ '[Math DisplayMath "\\\\sqrt{x^{3}}"]'
79
+ end
80
+
81
+ def native
82
+ '[ESqrt (ESuper (EIdentifier "x") (ENumber "3"))]'
83
+ end
84
+ end
@@ -0,0 +1,11 @@
1
+ # Configure $LOAD_PATH
2
+ require 'bundler/setup'
3
+
4
+ # Load texmath
5
+ require 'texmath'
6
+
7
+ # Load testing libraries
8
+ require 'minitest/autorun'
9
+
10
+ # Load debugging libraries
11
+ require 'pp'
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+ require 'support/formats'
3
+
4
+ class ConverterTest < Minitest::Test
5
+ include Formats
6
+ include TeXMath
7
+
8
+ Converter::READERS.each_key do |reader|
9
+ Converter::WRITERS.each_key do |writer|
10
+ define_method "test_#{reader}_to_#{writer}" do
11
+ assert_converts reader, writer
12
+ end
13
+ end
14
+ end
15
+
16
+ def test_symbol_reader_and_writer
17
+ assert_converts :tex, :mathml
18
+ end
19
+
20
+ def test_string_reader_and_writer
21
+ assert_converts 'tex', 'mathml'
22
+ end
23
+
24
+ def test_default_reader
25
+ assert_equal :tex, Converter.new(to: :mathml).reader
26
+ end
27
+
28
+ def test_default_writer
29
+ assert_equal :mathml, Converter.new(from: :tex).writer
30
+ end
31
+
32
+ def test_invalid_input
33
+ assert_raises ConversionError do
34
+ Converter.new.convert('\sqrt{3')
35
+ end
36
+ end
37
+
38
+ def test_executable_not_found
39
+ assert_raises NoExecutableError, "Can't find the 'ogremath' executable." do
40
+ Converter.new('ogremath').convert('\sqrt{3}')
41
+ end
42
+ end
43
+
44
+ def test_string_reader_not_found
45
+ assert_raises InvalidReaderError, "Can't find 'pears' reader." do
46
+ Converter.new(from: 'pears', to: :mathml)
47
+ end
48
+ end
49
+
50
+ def test_string_writer_not_found
51
+ assert_raises InvalidWriterError, "Can't find 'pears' writer." do
52
+ Converter.new(from: :tex, to: 'pears')
53
+ end
54
+ end
55
+
56
+ def test_symbol_reader_not_found
57
+ assert_raises InvalidReaderError, "Can't find 'pears' reader." do
58
+ Converter.new(from: :pears, to: :mathml)
59
+ end
60
+ end
61
+
62
+ def test_symbol_writer_not_found
63
+ assert_raises InvalidWriterError, "Can't find 'pears' writer." do
64
+ Converter.new(from: :tex, to: :pears)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def assert_converts(from, to)
71
+ input, expected_output = send(from), send(to)
72
+ actual_output = TeXMath.convert(input, from: from, to: to)
73
+ assert_equal expected_output, actual_output
74
+ end
75
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+
3
+ require 'texmath/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'texmath-ruby'
7
+ spec.version = TeXMath::VERSION
8
+ spec.authors = ['Casper Hollingberry']
9
+ spec.email = ['hollingberry7@gmail.com']
10
+ spec.summary = 'Convert math markup between LaTeX, MathML, and OMML.'
11
+ spec.description = 'Convert math markup between LaTeX, MathML, and OMML using Ruby. A wrapper for TeXMath, a Haskell library written by the creator of Pandoc.'
12
+ spec.homepage = 'http://github.com/hollingberry/texmath-ruby'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(/^test/)
17
+ spec.require_paths = ['lib']
18
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: texmath-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Casper Hollingberry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Convert math markup between LaTeX, MathML, and OMML using Ruby. A wrapper
14
+ for TeXMath, a Haskell library written by the creator of Pandoc.
15
+ email:
16
+ - hollingberry7@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/texmath.rb
28
+ - lib/texmath/converter.rb
29
+ - lib/texmath/errors.rb
30
+ - lib/texmath/version.rb
31
+ - test/support/formats.rb
32
+ - test/test_helper.rb
33
+ - test/texmath/converter_test.rb
34
+ - texmath-ruby.gemspec
35
+ homepage: http://github.com/hollingberry/texmath-ruby
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.2.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Convert math markup between LaTeX, MathML, and OMML.
59
+ test_files:
60
+ - test/support/formats.rb
61
+ - test/test_helper.rb
62
+ - test/texmath/converter_test.rb