math-to-itex 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +11 -0
- data/lib/math-to-itex.rb +38 -0
- data/lib/math-to-itex/parser.rb +30 -0
- data/lib/math-to-itex/version.rb +3 -0
- data/math-to-itex.gemspec +23 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 40247677aebef7cb0db52225dfcea7fa71dba988
|
4
|
+
data.tar.gz: b967b3faa38349251329182b80ad17c53f52c7ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45e8d6ec89382cda0175d380dbf2b2fd0438c308a1192cf26a9f57e80ea7d5322db5b7356dafa52a2cbef748b926291476da9b3476642197c73c27253c5b5666
|
7
|
+
data.tar.gz: 0d704f0cf2319393ad728cb89c8385eb762c5688b818dff9e08f4d399d1f9f08aed2fc320980058b073d5612a293d4f8eb2ab2cc81ae7c8533f3a470cbaa605c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Garen Torikian
|
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,65 @@
|
|
1
|
+
# Math to Itex
|
2
|
+
|
3
|
+
Convert strings into a proper [itex](http://golem.ph.utexas.edu/~distler/blog/itex2MML.html) format.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/gjtorikian/math-to-itex.svg?branch=master)](https://travis-ci.org/gjtorikian/math-to-itex)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'math-to-itex'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install math-to-itex
|
20
|
+
|
21
|
+
## Usage
|
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:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
require 'math-to-itex'
|
27
|
+
using MathToItex
|
28
|
+
|
29
|
+
result = '\[a \ne 0\]'.convert_to_itex
|
30
|
+
# result is now `$$a \ne 0$$`
|
31
|
+
|
32
|
+
result = '\[a \ne 0\]'.convert_to_itex { |string| "<span>#{string}</span>" }
|
33
|
+
# result is now `<span>$$a \ne 0$$</span>`
|
34
|
+
```
|
35
|
+
|
36
|
+
Basically, by not passing a block, you do a simple convert. By passing a block,
|
37
|
+
you can modify the resulting math equation.
|
38
|
+
|
39
|
+
Behind the scenes, `MathToItex` uses `gsub`, so *all* equations in a string are
|
40
|
+
modified.
|
41
|
+
|
42
|
+
If you want, you can determine the type (inline or display), too:
|
43
|
+
|
44
|
+
``` ruby
|
45
|
+
result = '$0$ is not equal to $$1 = 0$$'.convert_to_itex do |string, type|
|
46
|
+
%|<span class="#{type}">#{string}</span>|
|
47
|
+
end
|
48
|
+
|
49
|
+
# result is `<span class="inline">$0$</span> is not equal to <span class="display">$$1 = 0$$</span>`
|
50
|
+
```
|
51
|
+
|
52
|
+
## Matched math notations
|
53
|
+
|
54
|
+
Currently, the following formats are supported:
|
55
|
+
|
56
|
+
| inline formulas | display formulas |
|
57
|
+
| ------------- |-------------|
|
58
|
+
| `$...$` | `$$...$$`
|
59
|
+
| `(...)` | `[...]`
|
60
|
+
| | `\begin{equation}...\end{equation}`
|
61
|
+
|
62
|
+
## More math stuff
|
63
|
+
|
64
|
+
Check out [Mathematical](https://github.com/gjtorikian/mathematical), which quickly
|
65
|
+
converts itex notation equations into beautiful SVGs.
|
data/Rakefile
ADDED
data/lib/math-to-itex.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "math-to-itex/parser"
|
2
|
+
require "math-to-itex/version"
|
3
|
+
|
4
|
+
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
|
+
|
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
|
31
|
+
|
32
|
+
next(just_maths) if block.nil?
|
33
|
+
|
34
|
+
yield just_maths, type
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MathToItex
|
2
|
+
class Parser
|
3
|
+
# https://stackoverflow.com/questions/14182879/regex-to-match-latex-equations
|
4
|
+
REGEX = /
|
5
|
+
(?<!\\) # negative look-behind to make sure start is not escaped
|
6
|
+
(?: # start non-capture group for all possible match starts
|
7
|
+
# group 1, match dollar signs only
|
8
|
+
# single or double dollar sign enforced by look-arounds
|
9
|
+
((?<!\$)\${1,2}(?!\$))|
|
10
|
+
# group 2, match escaped parenthesis
|
11
|
+
(\\\()|
|
12
|
+
# group 3, match escaped bracket
|
13
|
+
(\\\[)|
|
14
|
+
# group 4, match begin equation
|
15
|
+
(\\begin\{equation\})
|
16
|
+
)
|
17
|
+
(.*?(\g<1>)?.*?) # match everything in between including nested LaTeX equations
|
18
|
+
(?<!\\) # negative look-behind to make sure end is not escaped
|
19
|
+
# if group 1 was start, match \1
|
20
|
+
(?(1)(?<!\$)\1(?!\$)|
|
21
|
+
# if group 2 was start, escaped parenthesis is end
|
22
|
+
(?(2)\\\)|
|
23
|
+
# if group 3 was start, escaped bracket is end
|
24
|
+
(?(3)\\\]|
|
25
|
+
# otherwise group 4 was start, match end equation
|
26
|
+
\\end\{equation\}
|
27
|
+
)))
|
28
|
+
/x
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'math-to-itex/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "math-to-itex"
|
8
|
+
spec.version = MathToItex::VERSION
|
9
|
+
spec.authors = ["Garen Torikian"]
|
10
|
+
spec.email = ["gjtorikian@gmail.com"]
|
11
|
+
spec.summary = %q{Turn math syntaxes into itex equations.}
|
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
|
+
spec.homepage = "https://github.com/gjtorikian/math-to-itex"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = %w(LICENSE.txt README.md Rakefile math-to-itex.gemspec)
|
17
|
+
spec.files += Dir.glob("lib/**/*")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: math-to-itex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Garen Torikian
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Pass in a string and turn all math equations into itex equations. Or,
|
28
|
+
pass in a block manipulate multiple matches.
|
29
|
+
email:
|
30
|
+
- gjtorikian@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/math-to-itex.rb
|
39
|
+
- lib/math-to-itex/parser.rb
|
40
|
+
- lib/math-to-itex/version.rb
|
41
|
+
- math-to-itex.gemspec
|
42
|
+
homepage: https://github.com/gjtorikian/math-to-itex
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.2.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Turn math syntaxes into itex equations.
|
66
|
+
test_files: []
|