mathematical 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/ext/mathematical/mathematical.c +13 -5
- data/lib/mathematical.rb +7 -13
- data/lib/mathematical/version.rb +1 -1
- data/test/mathematical/maliciousness_test.rb +15 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c820da257090af3286f7e4756f79ce1aece737d
|
4
|
+
data.tar.gz: d2f998fbdd90b00510d0a5ac678af9397fa00dd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 643d66def8e1670019d4a9bdad47d3914fb0e09136790e6349e5088a2dcda5db606e81da6480f92c23dc0887b3b10fa2596c28eb31b53420d38300fec0b729be
|
7
|
+
data.tar.gz: fedb115fd8b874671d9dfaa43004e89d06640ea3f0288ac3c939d8bff4418c0b58c0e7dc07e89c04058f8367a6c2a807e2ed20a32b3225827ba7493201e57b23
|
data/README.md
CHANGED
@@ -48,7 +48,7 @@ The output will be a hash, with keys that depend on the format you want:
|
|
48
48
|
* `:data`: the original invalid LaTeX
|
49
49
|
* `:error`: the error class (with message)
|
50
50
|
|
51
|
-
**Note**: If you pass in invalid LaTeX, an error is not raised, but a message *is* printed to STDERR
|
51
|
+
**Note**: If you pass in invalid LaTeX, an error is not raised, but a message *is* printed to STDERR. It is the caller's responsibility to check for `:error` and act on it.
|
52
52
|
|
53
53
|
### Array of equations
|
54
54
|
|
@@ -75,7 +75,7 @@ Mathematical.new.render(array)
|
|
75
75
|
[ {:data => "...", :width => ... }, { :data => '$not__thisisnotreal$', :error => "...", {:data => "...", :width => ... }]
|
76
76
|
```
|
77
77
|
|
78
|
-
That is, while the first and last elements are valid LaTeX math, the middle one is not, so the same string is returned. As with single strings,
|
78
|
+
That is, while the first and last elements are valid LaTeX math, the middle one is not, so the same string is returned. As with single strings, the error message is printed to STDERR, but not raised.
|
79
79
|
|
80
80
|
### Options
|
81
81
|
|
@@ -192,6 +192,9 @@ VALUE process(VALUE self, unsigned long maxsize, const char *latex_code, unsigne
|
|
192
192
|
return result_hash;
|
193
193
|
}
|
194
194
|
|
195
|
+
// `process` can potentially raise a bunch of exceptions, so we need to wrap
|
196
|
+
// the call in a rescue. And `rb_rescue` only takes one argument, so we need
|
197
|
+
// to pack everything in an array, and then unpack it in `process_helper`.
|
195
198
|
static VALUE process_helper(VALUE data)
|
196
199
|
{
|
197
200
|
VALUE *args = (VALUE *) data;
|
@@ -221,7 +224,14 @@ static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
|
|
221
224
|
case T_STRING: {
|
222
225
|
latex_code = StringValueCStr(rb_Input);
|
223
226
|
latex_size = (unsigned long) strlen(latex_code);
|
224
|
-
|
227
|
+
|
228
|
+
VALUE args[4];
|
229
|
+
args[0] = self;
|
230
|
+
args[1] = ULONG2NUM(maxsize);
|
231
|
+
args[2] = rb_Input;
|
232
|
+
args[3] = ULONG2NUM(latex_size);
|
233
|
+
|
234
|
+
output = rb_rescue(process_helper, args, process_rescue, rb_Input);
|
225
235
|
break;
|
226
236
|
}
|
227
237
|
case T_ARRAY: {
|
@@ -238,14 +248,12 @@ static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
|
|
238
248
|
latex_code = StringValueCStr(math);
|
239
249
|
latex_size = (unsigned long) strlen(latex_code);
|
240
250
|
|
241
|
-
|
242
|
-
// the call in a rescue. And `rb_rescue` only takes one argument, so we need
|
243
|
-
// to pack everything in an array, and then unpack it in `process_helper`.
|
244
|
-
VALUE args[5];
|
251
|
+
VALUE args[4];
|
245
252
|
args[0] = self;
|
246
253
|
args[1] = ULONG2NUM(maxsize);
|
247
254
|
args[2] = math;
|
248
255
|
args[3] = ULONG2NUM(latex_size);
|
256
|
+
|
249
257
|
hash = rb_rescue(process_helper, args, process_rescue, math);
|
250
258
|
|
251
259
|
rb_ary_store(output, i, hash);
|
data/lib/mathematical.rb
CHANGED
@@ -33,19 +33,13 @@ class Mathematical
|
|
33
33
|
def render(maths)
|
34
34
|
maths = validate_content(maths)
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
format_data(result_data)
|
44
|
-
end
|
45
|
-
rescue ParseError, DocumentCreationError, DocumentReadError => e
|
46
|
-
# an error in the C code, probably a bad TeX parse
|
47
|
-
$stderr.puts "#{e.message}: #{maths}"
|
48
|
-
maths
|
36
|
+
result_data = @processer.process(maths)
|
37
|
+
fail RuntimeError if !result_data.is_a?(Hash) && !result_data.is_a?(Array)
|
38
|
+
|
39
|
+
if result_data.is_a? Array
|
40
|
+
result_data.map { |d| format_data(d) }
|
41
|
+
else
|
42
|
+
format_data(result_data)
|
49
43
|
end
|
50
44
|
end
|
51
45
|
|
data/lib/mathematical/version.rb
CHANGED
@@ -7,7 +7,8 @@ class Mathematical::MaliciousnessTest < Test::Unit::TestCase
|
|
7
7
|
output = nil
|
8
8
|
# In mtex2MML, we raise a ParseError, but Mathematical suppresses it and returns the string.
|
9
9
|
assert_nothing_raised { output = render.render('$not__thisisnotreal$') }
|
10
|
-
|
10
|
+
assert_equal output[:data], '$not__thisisnotreal$'
|
11
|
+
assert_equal output[:exception].class, Mathematical::ParseError
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_it_does_not_blow_up_on_bad_arguments
|
@@ -25,42 +26,37 @@ class Mathematical::MaliciousnessTest < Test::Unit::TestCase
|
|
25
26
|
|
26
27
|
def test_it_does_not_blow_up_on_bad_options
|
27
28
|
assert_raise TypeError do
|
28
|
-
|
29
|
+
Mathematical.new({:ppi => 'not a number'})
|
29
30
|
end
|
30
31
|
|
31
32
|
assert_raise TypeError do
|
32
|
-
render = Mathematical.new({:zoom =>
|
33
|
+
render = Mathematical.new({:zoom => 'not a number'})
|
33
34
|
end
|
34
35
|
|
35
36
|
assert_raise TypeError do
|
36
|
-
|
37
|
+
Mathematical.new({:maxsize => 'not a number'})
|
37
38
|
end
|
38
39
|
|
39
40
|
assert_raise TypeError do
|
40
|
-
|
41
|
+
Mathematical.new({:maxsize => -23})
|
41
42
|
end
|
42
43
|
|
43
44
|
assert_raise TypeError do
|
44
|
-
|
45
|
+
Mathematical.new({:maxsize => 5.3})
|
45
46
|
end
|
46
47
|
|
47
48
|
assert_raise TypeError do
|
48
|
-
|
49
|
+
Mathematical.new({:format => 123})
|
49
50
|
end
|
50
51
|
|
51
52
|
assert_raise TypeError do
|
52
|
-
|
53
|
+
Mathematical.new({:format => 'something amazing'})
|
53
54
|
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
assert_nothing_raised RangeError do
|
61
|
-
render = Mathematical.new({:maxsize => 2147483647}) # signed long max
|
62
|
-
render.render('$a \ne b$')
|
63
|
-
end
|
56
|
+
render = Mathematical.new({:maxsize => 2})
|
57
|
+
output = render.render '$a \ne b$'
|
58
|
+
assert_equal output[:data], '$a \ne b$'
|
59
|
+
assert_equal output[:exception].class, Mathematical::MaxsizeError
|
64
60
|
|
65
61
|
assert_raise RangeError do
|
66
62
|
render = Mathematical.new({ :maxsize => 4_294_967_295 }) # unsigned long max
|
@@ -94,7 +90,8 @@ class Mathematical::MaliciousnessTest < Test::Unit::TestCase
|
|
94
90
|
# Much like above, this fails in mtx2MML, but should do nothing here
|
95
91
|
text = '$\Huge \sqrt\sqrt\sqrt\sqrt\sqrt\sqrt\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\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{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}'
|
96
92
|
assert_nothing_raised { output = render.render(text) }
|
97
|
-
|
93
|
+
assert_equal output[:data], text
|
94
|
+
assert_equal output[:exception].class, Mathematical::ParseError
|
98
95
|
end
|
99
96
|
|
100
97
|
def test_it_parses_all_possible_array_elements
|