math-to-itex 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40247677aebef7cb0db52225dfcea7fa71dba988
4
- data.tar.gz: b967b3faa38349251329182b80ad17c53f52c7ed
3
+ metadata.gz: f05a939cba1dbfa2747fd5c6693f796eb17bec31
4
+ data.tar.gz: 30d01cfd7e0f33cb1ac0923331b1b5ac44c8e645
5
5
  SHA512:
6
- metadata.gz: 45e8d6ec89382cda0175d380dbf2b2fd0438c308a1192cf26a9f57e80ea7d5322db5b7356dafa52a2cbef748b926291476da9b3476642197c73c27253c5b5666
7
- data.tar.gz: 0d704f0cf2319393ad728cb89c8385eb762c5688b818dff9e08f4d399d1f9f08aed2fc320980058b073d5612a293d4f8eb2ab2cc81ae7c8533f3a470cbaa605c
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
- This gem depends on [refinements](http://blog.headius.com/2012/11/refining-ruby.html), which is a Ruby 2.1+ feature. To use it:
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\]'.convert_to_itex
30
+ result = MathToItex('\[a \ne 0\]').convert
30
31
  # result is now `$$a \ne 0$$`
31
32
 
32
- result = '\[a \ne 0\]'.convert_to_itex { |string| "<span>#{string}</span>" }
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$$'.convert_to_itex do |string, type|
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
 
@@ -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
- # this is the format itex2MML expects
26
- if type == :inline
27
- just_maths = "$#{just_maths}$"
28
- else
29
- just_maths = "$$#{just_maths}$$"
30
- end
7
+ def self.parens(string)
8
+ @string = string
9
+ self
10
+ end
31
11
 
32
- next(just_maths) if block.nil?
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
- yield just_maths, type
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
@@ -1,3 +1,3 @@
1
1
  module MathToItex
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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.1.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
  - - ">="