math-to-itex 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -5
- data/lib/math-to-itex.rb +32 -27
- data/lib/math-to-itex/support_parens.rb +15 -0
- data/lib/math-to-itex/version.rb +1 -1
- data/math-to-itex.gemspec +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f05a939cba1dbfa2747fd5c6693f796eb17bec31
|
4
|
+
data.tar.gz: 30d01cfd7e0f33cb1ac0923331b1b5ac44c8e645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6838353e1d7abd41c25a40177e3d13717ec55056cb67a648204f828fa3b52bf427134c60c44dd11a979ffa7f81fc2967dda7648ef965fe900bf726eb06786883
|
7
|
+
data.tar.gz: 63dfc3d77457092d46078c8860dde0a2c6d1d367bfc592b685d6585cb2d92894a9608ef5855f6d647f5d410c9d0b8cbbaba2886a10f437aa17184ce7f2e002cf
|
data/README.md
CHANGED
@@ -20,16 +20,17 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
Due to fancy regexes, this gem only works with Ruby 2.0+.
|
24
|
+
|
25
|
+
To use this gem, pass a string to the `MathToItex` module, and an optional block:
|
24
26
|
|
25
27
|
``` ruby
|
26
28
|
require 'math-to-itex'
|
27
|
-
using MathToItex
|
28
29
|
|
29
|
-
result = '\[a \ne 0\]'.
|
30
|
+
result = MathToItex('\[a \ne 0\]').convert
|
30
31
|
# result is now `$$a \ne 0$$`
|
31
32
|
|
32
|
-
result = '\[a \ne 0\]'.
|
33
|
+
result = MathToItex('\[a \ne 0\]').convert { |string| "<span>#{string}</span>" }
|
33
34
|
# result is now `<span>$$a \ne 0$$</span>`
|
34
35
|
```
|
35
36
|
|
@@ -42,7 +43,7 @@ modified.
|
|
42
43
|
If you want, you can determine the type (inline or display), too:
|
43
44
|
|
44
45
|
``` ruby
|
45
|
-
result = '$0$ is not equal to $$1 = 0$$'.
|
46
|
+
result = MathToItex('$0$ is not equal to $$1 = 0$$').convert do |string, type|
|
46
47
|
%|<span class="#{type}">#{string}</span>|
|
47
48
|
end
|
48
49
|
|
data/lib/math-to-itex.rb
CHANGED
@@ -1,38 +1,43 @@
|
|
1
1
|
require "math-to-itex/parser"
|
2
|
+
require "math-to-itex/support_parens"
|
2
3
|
require "math-to-itex/version"
|
3
4
|
|
4
5
|
module MathToItex
|
5
|
-
refine String do
|
6
|
-
def convert_to_itex(&block)
|
7
|
-
self.gsub(MathToItex::Parser::REGEX) do |maths|
|
8
|
-
if maths =~ /\A\$(?!\$)/
|
9
|
-
just_maths = maths[1..-2]
|
10
|
-
type = :inline
|
11
|
-
elsif maths =~ /^\\\((?!\\\()/
|
12
|
-
just_maths = maths[2..-3]
|
13
|
-
type = :inline
|
14
|
-
elsif maths =~ /\A\$\$/
|
15
|
-
just_maths = maths[2..-3]
|
16
|
-
type = :display
|
17
|
-
elsif maths =~ /\A\\\[(?!\\\[)/
|
18
|
-
just_maths = maths[2..-3]
|
19
|
-
type = :display
|
20
|
-
elsif maths =~ /\A\\begin(?!\\begin)/
|
21
|
-
just_maths = maths[16..-15]
|
22
|
-
type = :display
|
23
|
-
end
|
24
6
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
just_maths = "$$#{just_maths}$$"
|
30
|
-
end
|
7
|
+
def self.parens(string)
|
8
|
+
@string = string
|
9
|
+
self
|
10
|
+
end
|
31
11
|
|
32
|
-
|
12
|
+
def self.convert(&block)
|
13
|
+
@string.gsub(MathToItex::Parser::REGEX) do |maths|
|
14
|
+
if maths =~ /\A\$(?!\$)/
|
15
|
+
just_maths = maths[1..-2]
|
16
|
+
type = :inline
|
17
|
+
elsif maths =~ /^\\\((?!\\\()/
|
18
|
+
just_maths = maths[2..-3]
|
19
|
+
type = :inline
|
20
|
+
elsif maths =~ /\A\$\$/
|
21
|
+
just_maths = maths[2..-3]
|
22
|
+
type = :display
|
23
|
+
elsif maths =~ /\A\\\[(?!\\\[)/
|
24
|
+
just_maths = maths[2..-3]
|
25
|
+
type = :display
|
26
|
+
elsif maths =~ /\A\\begin(?!\\begin)/
|
27
|
+
just_maths = maths[16..-15]
|
28
|
+
type = :display
|
29
|
+
end
|
33
30
|
|
34
|
-
|
31
|
+
# this is the format itex2MML expects
|
32
|
+
if type == :inline
|
33
|
+
just_maths = "$#{just_maths}$"
|
34
|
+
else
|
35
|
+
just_maths = "$$#{just_maths}$$"
|
35
36
|
end
|
37
|
+
|
38
|
+
next(just_maths) if block.nil?
|
39
|
+
|
40
|
+
yield just_maths, type
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# from http://coderrr.wordpress.com/2008/11/30/overloading-the-parenthesis-operator-in-ruby/
|
2
|
+
|
3
|
+
class Object
|
4
|
+
alias_method :orig_method_missing, :method_missing
|
5
|
+
|
6
|
+
def method_missing(m, *a, &b)
|
7
|
+
klass = begin
|
8
|
+
(self.is_a?(Module) ? self : self.class).const_get(m)
|
9
|
+
rescue NameError
|
10
|
+
end
|
11
|
+
|
12
|
+
return klass.send(:parens, *a, &b) if klass.respond_to? :parens
|
13
|
+
orig_method_missing m, *a, &b
|
14
|
+
end
|
15
|
+
end
|
data/lib/math-to-itex/version.rb
CHANGED
data/math-to-itex.gemspec
CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = %q{Pass in a string and turn all math equations into itex equations. Or, pass in a block manipulate multiple matches.}
|
13
13
|
spec.homepage = "https://github.com/gjtorikian/math-to-itex"
|
14
14
|
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = '>= 2.0'
|
15
16
|
|
16
17
|
spec.files = %w(LICENSE.txt README.md Rakefile math-to-itex.gemspec)
|
17
18
|
spec.files += Dir.glob("lib/**/*")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: math-to-itex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Garen Torikian
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- Rakefile
|
38
38
|
- lib/math-to-itex.rb
|
39
39
|
- lib/math-to-itex/parser.rb
|
40
|
+
- lib/math-to-itex/support_parens.rb
|
40
41
|
- lib/math-to-itex/version.rb
|
41
42
|
- math-to-itex.gemspec
|
42
43
|
homepage: https://github.com/gjtorikian/math-to-itex
|
@@ -51,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
52
|
requirements:
|
52
53
|
- - ">="
|
53
54
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
+
version: '2.0'
|
55
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
57
|
requirements:
|
57
58
|
- - ">="
|