mathematical 0.0.4 → 0.1.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: 90116f92892598eb7e352b94a5063289b1a4d597
4
- data.tar.gz: e0d87480fadd5ce03a9f6d112d035c686cf9871d
3
+ metadata.gz: 9e4cb929866748f87d1b15561f28e2cb5be55038
4
+ data.tar.gz: 71cfa345fd9663b10209bd536619a69638d2d0ff
5
5
  SHA512:
6
- metadata.gz: 128b9d3bcd5b4633203f0c76628e5190f80219b4f0d4f8f5b59d55a9a3182fd2e271886aa3245683ea0fbfd7770e48498f15e11fffc3abff3e9f1a4ed10a1270
7
- data.tar.gz: ff3df8ae35fa24a359ca304531bec3ce0f610eae97819084fe617e752450396ef923b32baf12a36bde7f39eb2f9abd3ffeb0723e08e9cb7661ee6c5a8afc5f2d
6
+ metadata.gz: 1540affcb37235374e27834f805d90abd09d763815fc21854b8eebc5fdd9ca2f0f11c80428e1e937a3790ba02bae9730901e007fad79de2c9dcc07e65109271d
7
+ data.tar.gz: fe10a215421e6e25e021545bbea5fa17c419c8e63ac5c8962f7721197a393b68d76609e8b6ea2bae0845512bf106db9ec19fd9ecfa77d4e1c001f1d2e810e304
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Mathematical
2
2
 
3
- Convert mathematical equations to base64 encoded images.
3
+ Quickly convert math equations into beautiful SVGs.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/gjtorikian/mathematical.svg?branch=master)](https://travis-ci.org/gjtorikian/mathematical)
6
6
 
@@ -28,45 +28,29 @@ The simplest way to do this is
28
28
  Mathematical::Render.new.render(string_with_math)
29
29
  ```
30
30
 
31
- `string_with_math` should just be a string, containing inline or display style math.
32
- The output will be all the math equations, as SVGs, converted into base64 encoded images.
33
- They look something like this:
34
-
35
- ``` html
36
- <img class="type-inline" data-math-type="type-inline" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0..."/>
37
- ```
38
-
39
- Inline math will have `class="type-inline" data-math-type="type-inline"` set, and display math will have
40
- `class="type-display" data-math-type="type-display"`.
41
-
42
- ### Matched math notations
43
-
44
- Currently, the following formats are supported:
45
-
46
- | inline formulas | displayed equations |
47
- | ------------- |-------------|
48
- | `$...$` | `[...]`
49
- | `(...)` | `\begin{equation}...\end{equation}`
50
-
51
- ### Supported commands and symbols
52
-
53
- Literally everything on the [itex2MML homepage for version 1.5.1](http://golem.ph.utexas.edu/~distler/blog/itex2MMLcommands.html)
54
- is supported, because it acts as the interpretation engine for mathematical.
31
+ `string_with_math` should just be a string of itex inline (`$..$`) or display (`$$..$$`) style math.
32
+ The output will be the math equation, as an SVG blob.
55
33
 
56
34
  ### Options
57
35
 
58
36
  `Mathematical::Render.new` can take a few options:
59
37
 
60
- * `:ppi`, the pixels per inch of the resulting SVG (default: `72.0`)
61
- * `:zoom`, the zoome level of the resulting SVG (default: `1.0`)
38
+ * `:ppi` - A double determining the pixels per inch of the resulting SVG (default: `72.0`).
39
+ * `:zoom` - A double determining the zoom level of the resulting SVG (default: `1.0`).
40
+ * `:base64` - A boolean determining whether Mathematical's output should be a base64-encoded SVG string (default: `false`).
62
41
 
63
- Pass these in as an options hash; these *must* be Float values! For example:
42
+ Pass these in as an options hash:
64
43
 
65
44
  ``` ruby
66
- opts = { :ppi => 200.0, :zoom => 5.0 }
45
+ opts = { :ppi => 200.0, :zoom => 5.0, :base64 => true }
67
46
  renderer = Mathematical::Render.new(opts)
47
+ renderer.render('$a \ne b$')
68
48
  ```
69
49
 
50
+ ### Supported commands and symbols
51
+
52
+ Literally everything on the [itex2MML homepage for version 1.5.1](http://golem.ph.utexas.edu/~distler/blog/itex2MMLcommands.html) is supported, because it acts as the interpretation engine for Mathematical.
53
+
70
54
  ## Dependencies
71
55
 
72
56
  Before using this gem, you must install the following libraries:
@@ -105,6 +89,16 @@ sudo apt-get -qq -y install libxml2-dev libcairo2-dev libpango1.0-dev ttf-lyx
105
89
 
106
90
  On a Windows machine, I have no idea. Pull requests welcome!
107
91
 
92
+ ## Benchmarks
93
+
94
+ ```
95
+ Benchmarking....
96
+ Size: 1175 kilobytes
97
+ Iterations: 10
98
+ user system total real
99
+ Rendering... 9.470000 0.750000 10.220000 ( 13.909420)
100
+ ```
101
+
108
102
  ## Hacking
109
103
 
110
104
  After cloning the repo:
@@ -184,3 +178,19 @@ can convert directly to SVG, and it's fast. The same arbitrary 880 equations wer
184
178
  rendered in less than three seconds.
185
179
 
186
180
  And thus a wrapper was born.
181
+
182
+ ## More math stuff
183
+
184
+ Check out [math-to-itex](https://github.com/gjtorikian/math-to-itex/), which quickly
185
+ parses out itex notation from strings.
186
+
187
+ With it, you could do something fun like:
188
+
189
+ ``` ruby
190
+ MathToItex(string).convert do |eq, type|
191
+ svg_content = Mathematical::Render.new(:base64 => true).render(eq)
192
+
193
+ # create image tags of math with base64-encoded SVGs
194
+ %|<img class="#{type.to_s}-math" data-math-type="#{type.to_s}-math" src="#{svg_content}"/>|
195
+ end
196
+ ```
data/lib/mathematical.rb CHANGED
@@ -1,13 +1,7 @@
1
1
  require "mathematical/mathematical"
2
2
 
3
- require "mathematical/parser"
4
3
  require "mathematical/render"
5
4
  require "mathematical/version"
6
5
 
7
6
  module Mathematical
8
- # Raised when missing binaries for images rendering.
9
- class CommandNotFoundError < Exception; end
10
-
11
- # Raised when the contents could not be parsed
12
- class ParseError < Exception; end
13
7
  end
Binary file
@@ -6,7 +6,8 @@ module Mathematical
6
6
  class Render
7
7
  DEFAULT_OPTS = {
8
8
  :ppi => 72.0,
9
- :zoom => 1.0
9
+ :zoom => 1.0,
10
+ :base64 => false
10
11
  }
11
12
 
12
13
  def initialize(opts = {})
@@ -18,59 +19,33 @@ module Mathematical
18
19
  rescue TypeError => e # some error in the C code
19
20
  raise
20
21
  end
21
-
22
22
  end
23
23
 
24
- def render(text)
25
- raise(TypeError, "text must be a string!") unless text.is_a? String
24
+ def render(maths)
25
+ raise(TypeError, "text must be a string!") unless maths.is_a? String
26
+ raise(ArgumentError, "text must be in itex format (`$...$` or `$$...$$`)!") unless maths =~ /\A\${1,2}/
26
27
 
27
- # TODO: figure out how to write svgs without the tempfile
28
+ # TODO: figure out how to write SVGs without the tempfile
28
29
  tempfile = Tempfile.new('mathematical-temp.svg')
29
- text = text.gsub(Mathematical::Parser::REGEX) do |maths|
30
- if maths =~ /^\$(?!\$)/
31
- just_maths = maths[1..-2]
32
- type = :inline
33
- elsif maths =~ /^\\\((?!\\\[)/
34
- just_maths = maths[2..-4]
35
- type = :inline
36
- elsif maths =~ /^\\\[(?!\\\[)/
37
- just_maths = maths[2..-4]
38
- type = :display
39
- elsif maths =~ /^\\begin(?!\\begin)/
40
- just_maths = maths[16..-15]
41
- type = :display
42
- end
43
-
44
- # this is the format itex2MML expects
45
- if type == :inline
46
- just_maths = "$#{just_maths}$"
47
- else
48
- just_maths = "$$#{just_maths}$$"
49
- end
50
-
51
- begin
52
- status = @processer.process(just_maths, tempfile.path)
53
- raise RuntimeError unless status
54
- svg_content = File.open(tempfile.path, 'r') { |image_file| image_file.read }
55
- svg_content = svg_content.lines.to_a[1..-1].join
56
- rescue RuntimeError => e # an error in the C code, probably a bad TeX parse
57
- $stderr.puts "#{e.message}: #{maths}"
58
- next(maths)
59
- end
60
-
61
- "<img class=\"#{named_type(type)}\" data-math-type=\"#{named_type(type)}\" src=\"data:image/svg+xml;base64,#{svg_to_base64(svg_content)}\"/>"
30
+ begin
31
+ raise RuntimeError unless @processer.process(maths, tempfile.path)
32
+ svg_content = File.open(tempfile.path, 'r') { |image_file| image_file.read }
33
+ svg_content = svg_content[xml_header.length..-1] # remove starting <?xml...> tag
34
+ @config[:base64] ? svg_to_base64(svg_content) : svg_content
35
+ rescue RuntimeError => e # an error in the C code, probably a bad TeX parse
36
+ $stderr.puts "#{e.message}: #{maths}"
37
+ maths
62
38
  end
63
- tempfile.close
64
- tempfile.unlink
65
- text
66
39
  end
67
40
 
68
- def svg_to_base64(contents)
69
- Base64.strict_encode64(contents)
41
+ private
42
+
43
+ def xml_header
44
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
70
45
  end
71
46
 
72
- def named_type(type)
73
- "#{type.to_s}-math"
47
+ def svg_to_base64(contents)
48
+ "data:image/svg+xml;base64,#{Base64.strict_encode64(contents)}"
74
49
  end
75
50
  end
76
51
  end
@@ -1,3 +1,3 @@
1
1
  module Mathematical
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/mathematical.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Mathematical::VERSION
9
9
  spec.authors = ["Garen Torikian"]
10
10
  spec.email = ["gjtorikian@gmail.com"]
11
- spec.summary = %q{Quickly turn itex math equations into base64 SVGs.}
12
- spec.description = %q{A very fast way to turn itex math equations into beautifully rendered base64 SVGs, to embed on the web. This library is a general purpose wrapper to GNOME's Lasem. }
11
+ spec.summary = %q{Quickly convert math equations into beautiful SVGs.}
12
+ spec.description = %q{A very fast way to turn itex math equations into beautifully rendered SVGs, to embed on the web. This library is a general purpose wrapper to GNOME's Lasem. }
13
13
  spec.homepage = "https://github.com/gjtorikian/mathematical"
14
14
  spec.license = "MIT"
15
15
 
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake-compiler"
23
23
  spec.add_development_dependency "bundler", "~> 1.5"
24
24
  spec.add_development_dependency "mocha", "~> 1.0"
25
+ spec.add_development_dependency "math-to-itex"
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mathematical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-05 00:00:00.000000000 Z
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -52,8 +52,22 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: math-to-itex
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: 'A very fast way to turn itex math equations into beautifully rendered
56
- base64 SVGs, to embed on the web. This library is a general purpose wrapper to GNOME''s
70
+ SVGs, to embed on the web. This library is a general purpose wrapper to GNOME''s
57
71
  Lasem. '
58
72
  email:
59
73
  - gjtorikian@gmail.com
@@ -67,7 +81,6 @@ files:
67
81
  - lib/mathematical.bundle
68
82
  - lib/mathematical.rb
69
83
  - lib/mathematical/mathematical.bundle
70
- - lib/mathematical/parser.rb
71
84
  - lib/mathematical/render.rb
72
85
  - lib/mathematical/version.rb
73
86
  - mathematical.gemspec
@@ -94,5 +107,5 @@ rubyforge_project:
94
107
  rubygems_version: 2.2.2
95
108
  signing_key:
96
109
  specification_version: 4
97
- summary: Quickly turn itex math equations into base64 SVGs.
110
+ summary: Quickly convert math equations into beautiful SVGs.
98
111
  test_files: []
@@ -1,30 +0,0 @@
1
- module Mathematical
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