mathematical 1.3.0 → 1.4.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 +14 -7
- data/Rakefile +4 -1
- data/ext/mathematical/cairo_callbacks.h +5 -0
- data/ext/mathematical/extconf.rb +34 -8
- data/ext/mathematical/lasem_overrides.c +32 -3
- data/ext/mathematical/lasem_overrides.h +11 -2
- data/ext/mathematical/mathematical.c +27 -26
- data/ext/mathematical/mathematical.h +5 -1
- data/ext/mathematical/mtex2MML/deps/strdup/strdup.c +24 -0
- data/ext/mathematical/mtex2MML/deps/strdup/strdup.h +24 -0
- data/ext/mathematical/mtex2MML/deps/uthash/utarray.h +1 -0
- data/ext/mathematical/mtex2MML/src/{color_definitions.c → colors.c} +2 -3
- data/ext/mathematical/mtex2MML/src/colors.h +23 -0
- data/ext/mathematical/mtex2MML/src/em.c +99 -0
- data/ext/mathematical/mtex2MML/src/em.h +31 -0
- data/ext/mathematical/mtex2MML/src/{parse_extras.c → environment.c} +73 -157
- data/ext/mathematical/mtex2MML/src/environment.h +85 -0
- data/ext/mathematical/mtex2MML/src/{mtex2MML.l → lexer.l} +26 -15
- data/ext/mathematical/mtex2MML/src/main.c +31 -10
- data/ext/mathematical/mtex2MML/src/mtex2MML.h +19 -7
- data/ext/mathematical/mtex2MML/src/{mtex2MML.y → parser.y} +451 -446
- data/ext/mathematical/mtex2MML/src/string_extras.c +4 -3
- data/ext/mathematical/mtex2MML/src/string_extras.h +7 -7
- data/ext/mathematical/mtex2MML/tests/array.c +11 -11
- data/ext/mathematical/mtex2MML/tests/basic.c +14 -13
- data/ext/mathematical/mtex2MML/tests/clar.c +2 -0
- data/ext/mathematical/mtex2MML/tests/clar/fs.h +3 -1
- data/ext/mathematical/mtex2MML/tests/clar/sandbox.h +3 -1
- data/ext/mathematical/mtex2MML/tests/cornercases.c +3 -2
- data/ext/mathematical/mtex2MML/tests/delimiters.c +126 -0
- data/ext/mathematical/mtex2MML/tests/deps/file2str/file2str.c +2 -4
- data/ext/mathematical/mtex2MML/tests/deps/mkdtemp/mkdtemp.c +151 -0
- data/ext/mathematical/mtex2MML/tests/deps/mkdtemp/mkdtemp.h +10 -0
- data/ext/mathematical/mtex2MML/tests/deps/trim/trim.c +4 -1
- data/ext/mathematical/mtex2MML/tests/env.c +35 -34
- data/ext/mathematical/mtex2MML/tests/functions.c +3 -2
- data/ext/mathematical/mtex2MML/tests/helpers.c +6 -4
- data/ext/mathematical/mtex2MML/tests/helpers.h +2 -2
- data/ext/mathematical/mtex2MML/tests/main.c +1 -1
- data/ext/mathematical/mtex2MML/tests/maliciousness.c +35 -9
- data/ext/mathematical/mtex2MML/tests/mathjax.c +202 -202
- data/ext/mathematical/mtex2MML/tests/numbered_equations.c +6 -6
- data/ext/mathematical/mtex2MML/tests/performance.c +39 -0
- data/lib/mathematical.rb +38 -5
- data/lib/mathematical/configuration.rb +19 -0
- data/lib/mathematical/validator.rb +17 -31
- data/lib/mathematical/version.rb +1 -1
- data/mathematical.gemspec +3 -1
- data/test/mathematical/basic_test.rb +60 -7
- data/test/mathematical/corrections_test.rb +1 -1
- data/test/mathematical/delimiters_test.rb +58 -0
- data/test/mathematical/fixtures_test.rb +3 -2
- data/test/mathematical/maliciousness_test.rb +19 -27
- data/test/mathematical/mathjax_test.rb +6 -5
- data/test/mathematical/mathml_test.rb +1 -1
- data/test/mathematical/multiples_test.rb +1 -1
- data/test/mathematical/performance_test.rb +3 -1
- data/test/mathematical/png_test.rb +3 -3
- data/test/test_helper.rb +4 -1
- metadata +35 -21
- data/ext/mathematical/lasem/src/lsmdomenumtypes.c +0 -99
- data/ext/mathematical/lasem/src/lsmdomenumtypes.h +0 -26
- data/ext/mathematical/lasem/src/lsmmathmlenumtypes.c +0 -737
- data/ext/mathematical/lasem/src/lsmmathmlenumtypes.h +0 -93
- data/ext/mathematical/lasem/src/lsmsvgenumtypes.c +0 -1083
- data/ext/mathematical/lasem/src/lsmsvgenumtypes.h +0 -111
- data/ext/mathematical/mtex2MML/Makefile +0 -100
- data/ext/mathematical/mtex2MML/build/libmtex2MML.a +0 -0
- data/ext/mathematical/mtex2MML/build/mtex2MML.h +0 -73
- data/ext/mathematical/mtex2MML/src/lex.yy.c +0 -8845
- data/ext/mathematical/mtex2MML/src/parse_extras.h +0 -111
- data/ext/mathematical/mtex2MML/src/y.tab.c +0 -10178
- data/ext/mathematical/mtex2MML/src/y.tab.h +0 -617
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9cecd3bb1d038dc96a7e15af2d68aa05d414800
|
4
|
+
data.tar.gz: e80c42d9ac225acb937d4158e5ca1664659d734c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc5aaccb3879afc81b320f7fe17af73c430c7c465b48a32108641c85ca690f1f743b46e5b7eb3cc1a2911e52ccfaed739aecbd843e61d36d1604b763980df6e8
|
7
|
+
data.tar.gz: 15ce4f72ce909e0700594a4a47f28421535bb76f957b4793dd2119f4aeaf46d32df612c7676bf504fbd9a9d69ac0a8191d1b6b63560e7d0e429ec5a7798fe06d
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ require 'mathematical'
|
|
30
30
|
Mathematical.new.render(string_with_math)
|
31
31
|
```
|
32
32
|
|
33
|
-
`string_with_math` should just be a string of math
|
33
|
+
`string_with_math` should just be a string of TeX math. The default delimiters are `$..$` for inline and `$$..$$` for display. These can be changed by options--see below.
|
34
34
|
|
35
35
|
The output will be a hash, with keys that depend on the format you want:
|
36
36
|
|
@@ -44,11 +44,17 @@ The output will be a hash, with keys that depend on the format you want:
|
|
44
44
|
* `:data`: the PNG data
|
45
45
|
* If you asked for MathML, you'll get:
|
46
46
|
* `:data`: the MathML data
|
47
|
-
* If you pass in invalid
|
48
|
-
* `:data`: the original invalid
|
47
|
+
* If you pass in invalid TeX, you'll get:
|
48
|
+
* `:data`: the original invalid TeX
|
49
49
|
* `:exception`: the error class (with message)
|
50
50
|
|
51
|
-
**Note**: If you pass in invalid
|
51
|
+
**Note**: If you pass in invalid TeX, an error is not raised, but a message *is* printed to STDERR. It is the caller's responsibility to check for `:exception` and act on it.
|
52
|
+
|
53
|
+
`render` just converts a single equation. There are several other methods you can use:
|
54
|
+
|
55
|
+
* `filter`: Given a string with a mix of TeX math and non-math elements, this returns a single string containing just the converted math elements.
|
56
|
+
* `text_filter`: Given a string with a mix of TeX math and non-math elements, this converts all the math and leaves the rest of the string unmodified.
|
57
|
+
* `strict_filter`: Given a string with a mix of TeX math and non-math elements, this converts all the math and leaves the rest of the string unmodified. HTML tags are removed completely.
|
52
58
|
|
53
59
|
### Array of equations
|
54
60
|
|
@@ -69,7 +75,7 @@ This returns an array of hashes, rendering the indices. For example, for the abo
|
|
69
75
|
[ {:data => "...", :width => ... }, { :data => '$not__thisisnotreal$', :exception => "...", {:data => "...", :width => ... }]
|
70
76
|
```
|
71
77
|
|
72
|
-
That is, while the first and last elements are valid
|
78
|
+
That is, while the first and last elements are valid TeX 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.
|
73
79
|
|
74
80
|
### Options
|
75
81
|
|
@@ -82,12 +88,13 @@ That is, while the first and last elements are valid LaTeX math, the middle one
|
|
82
88
|
| `:base64` | A boolean determining whether Mathematical's output should be a base64-encoded SVG string | `false`
|
83
89
|
| `:maxsize` | A numeral indicating the `MAXSIZE` the output string can be. | `unsigned long`
|
84
90
|
| `:format` | A symbol indicating whether you want an `:svg`, `:png`, or `:mathml` output. | `:svg`
|
91
|
+
| `:delimiter` | A symbol indicating whether you want an `:dollar` for inline (`$..$`), `:double` for display (`$$..$$`), `:parens` for inline (`\(..\)`), or `:brackets` for display (`[..\]`). You can also pass in an array of symbols to have multiple delimiters considered. | `[:dollar, :double]`
|
85
92
|
|
86
93
|
Pass these in like this:
|
87
94
|
|
88
95
|
``` ruby
|
89
|
-
|
90
|
-
renderer = Mathematical.new(
|
96
|
+
options = { :ppi => 200.0, :zoom => 5.0, :base64 => true }
|
97
|
+
renderer = Mathematical.new(options)
|
91
98
|
renderer.render('$a \ne b$')
|
92
99
|
```
|
93
100
|
|
data/Rakefile
CHANGED
@@ -28,7 +28,7 @@ Rake::Task['clean'].enhance do
|
|
28
28
|
Rake::Task['destroy_copies'].invoke
|
29
29
|
end
|
30
30
|
|
31
|
-
Rake::Task[:test].prerequisites
|
31
|
+
Rake::Task[:test].prerequisites
|
32
32
|
|
33
33
|
task default: [:test]
|
34
34
|
|
@@ -51,6 +51,9 @@ task :destroy_copies do
|
|
51
51
|
File.delete(f)
|
52
52
|
end
|
53
53
|
Dir.glob("#{ext_dir}/{lib,src,test,ext,deps,uthash}").select { |d| FileUtils.rm_rf d }
|
54
|
+
FileUtils.rm_rf(File.join(ext_dir, 'mtex2MML', 'build'))
|
55
|
+
FileUtils.rm_rf(File.join(ext_dir, 'lib'))
|
56
|
+
FileUtils.rm_rf(File.join(ext_dir, 'Testing'))
|
54
57
|
end
|
55
58
|
|
56
59
|
desc 'Pretty format C code'
|
@@ -1,3 +1,6 @@
|
|
1
|
+
#ifndef CAIRO_OVERRIDES_H
|
2
|
+
#define CAIRO_OVERRIDES_H
|
3
|
+
|
1
4
|
#include "ruby.h"
|
2
5
|
|
3
6
|
#include <cairo-svg.h>
|
@@ -5,3 +8,5 @@
|
|
5
8
|
extern cairo_status_t cairoSvgSurfaceCallback (void *closure, const unsigned char *data, unsigned int length);
|
6
9
|
|
7
10
|
extern cairo_status_t cairoPngSurfaceCallback (void *closure, const unsigned char *data, unsigned int length);
|
11
|
+
|
12
|
+
#endif
|
data/ext/mathematical/extconf.rb
CHANGED
@@ -1,11 +1,24 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
require 'rbconfig'
|
3
|
-
host_os = RbConfig::CONFIG['host_os']
|
4
3
|
|
4
|
+
HOST_OS = RbConfig::CONFIG['host_os']
|
5
|
+
LIBDIR = RbConfig::CONFIG['libdir']
|
6
|
+
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
7
|
+
HEADER_DIRS = [INCLUDEDIR]
|
8
|
+
|
9
|
+
unless find_executable('cmake')
|
10
|
+
$stderr.puts "\n\n\n[ERROR]: cmake is required and not installed. Get it here: http://www.cmake.org/\n\n"
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
|
14
|
+
ROOT_TMP = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp'))
|
5
15
|
LASEM_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'lasem', 'src'))
|
6
16
|
MTEX2MML_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'mtex2MML'))
|
17
|
+
MTEX2MML_BUILD_DIR = File.join(MTEX2MML_DIR, 'build')
|
18
|
+
MTEX2MML_LIB_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
|
19
|
+
MTEX2MML_SRC_DIR = File.expand_path(File.join(MTEX2MML_DIR, 'src'))
|
7
20
|
|
8
|
-
if
|
21
|
+
if HOST_OS =~ /darwin|mac os/
|
9
22
|
ENV['PKG_CONFIG_PATH'] = "/opt/X11/lib/pkgconfig:#{ENV['PKG_CONFIG_PATH']}"
|
10
23
|
end
|
11
24
|
|
@@ -22,15 +35,28 @@ find_header('libxml/xpathInternals.h', '/include/libxml2', '/usr/include/libxml2
|
|
22
35
|
FileUtils.cp_r(Dir.glob("#{LASEM_DIR}/*"), File.dirname(__FILE__))
|
23
36
|
File.delete(File.join(File.dirname(__FILE__), 'lasemrender.c'))
|
24
37
|
|
38
|
+
# TODO: we need to clear out the build dir that's erroneously getting packaged
|
39
|
+
# this causes problems, as Linux installation is expecting OS X output
|
40
|
+
if File.directory?(MTEX2MML_BUILD_DIR) && !File.exist?(ROOT_TMP)
|
41
|
+
FileUtils.rm_rf(MTEX2MML_BUILD_DIR)
|
42
|
+
end
|
43
|
+
FileUtils.mkdir_p(MTEX2MML_BUILD_DIR)
|
44
|
+
|
25
45
|
# build mtex2MML library
|
26
|
-
Dir.chdir(
|
27
|
-
system '
|
46
|
+
Dir.chdir(MTEX2MML_BUILD_DIR) do
|
47
|
+
system 'cmake ..'
|
48
|
+
system 'make libmtex2MML_static'
|
28
49
|
end
|
29
50
|
|
30
|
-
FileUtils.
|
51
|
+
FileUtils.mkdir_p(MTEX2MML_LIB_DIR)
|
52
|
+
FileUtils.cp_r(File.join(MTEX2MML_BUILD_DIR, 'libmtex2MML.a'), MTEX2MML_LIB_DIR)
|
53
|
+
FileUtils.cp_r(File.join(MTEX2MML_SRC_DIR, 'mtex2MML.h'), File.dirname(__FILE__))
|
54
|
+
|
55
|
+
LIB_DIRS = [LIBDIR, MTEX2MML_LIB_DIR]
|
56
|
+
HEADER_DIRS << MTEX2MML_BUILD_DIR
|
57
|
+
dir_config('mathematical', HEADER_DIRS, LIB_DIRS)
|
31
58
|
|
32
|
-
$LDFLAGS << " #{`pkg-config --static --libs glib-2.0 gdk-pixbuf-2.0 cairo pango`.chomp}"
|
33
|
-
$CFLAGS << "
|
34
|
-
$LOCAL_LIBS << "#{MTEX2MML_DIR}/build/libmtex2MML.a"
|
59
|
+
$LDFLAGS << " #{`pkg-config --static --libs glib-2.0 gdk-pixbuf-2.0 cairo pango`.chomp} -lmtex2MML"
|
60
|
+
$CFLAGS << " #{`pkg-config --cflags glib-2.0 gdk-pixbuf-2.0 cairo pango`.chomp} -I#{LASEM_DIR}"
|
35
61
|
|
36
62
|
create_makefile('mathematical/mathematical')
|
@@ -1,10 +1,15 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
1
3
|
#include <lasem_overrides.h>
|
4
|
+
#include <string.h>
|
5
|
+
#include "mtex2MML.h"
|
2
6
|
|
3
7
|
char *
|
4
|
-
lsm_mtex_to_mathml (const char *mtex, gssize size, int
|
8
|
+
lsm_mtex_to_mathml (const char *mtex, gssize size, int delimiter, int render_type)
|
5
9
|
{
|
6
10
|
gsize usize;
|
7
11
|
char *mathml;
|
12
|
+
int status = 0;
|
8
13
|
|
9
14
|
if (mtex == NULL) {
|
10
15
|
return NULL;
|
@@ -16,8 +21,32 @@ lsm_mtex_to_mathml (const char *mtex, gssize size, int global_start)
|
|
16
21
|
usize = size;
|
17
22
|
}
|
18
23
|
|
19
|
-
|
20
|
-
|
24
|
+
switch (render_type) {
|
25
|
+
case PARSE:
|
26
|
+
mathml = mtex2MML_parse(mtex, usize, delimiter);
|
27
|
+
if (mathml == NULL) {
|
28
|
+
status = 1;
|
29
|
+
}
|
30
|
+
break;
|
31
|
+
case FILTER:
|
32
|
+
status = mtex2MML_filter(mtex, usize, delimiter);
|
33
|
+
mathml = mtex2MML_output();
|
34
|
+
break;
|
35
|
+
case TEXT_FILTER:
|
36
|
+
status = mtex2MML_text_filter(mtex, usize, delimiter);
|
37
|
+
mathml = mtex2MML_output();
|
38
|
+
break;
|
39
|
+
case STRICT_FILTER:
|
40
|
+
status = mtex2MML_strict_filter(mtex, usize, delimiter);
|
41
|
+
mathml = mtex2MML_output();
|
42
|
+
break;
|
43
|
+
default:
|
44
|
+
/* should be impossible, Ruby code prevents this */
|
45
|
+
print_and_raise(rb_eTypeError, "not valid render format");
|
46
|
+
break;
|
47
|
+
}
|
48
|
+
|
49
|
+
if (status) {
|
21
50
|
return NULL;
|
22
51
|
}
|
23
52
|
|
@@ -1,5 +1,12 @@
|
|
1
|
+
#ifndef LASEM_OVERRIDES_H
|
2
|
+
#define LASEM_OVERRIDES_H
|
3
|
+
|
1
4
|
#include <glib.h>
|
2
|
-
|
5
|
+
|
6
|
+
#define PARSE 0
|
7
|
+
#define FILTER 1
|
8
|
+
#define TEXT_FILTER 2
|
9
|
+
#define STRICT_FILTER 3
|
3
10
|
|
4
11
|
/**
|
5
12
|
* lsm_mtex_to_mathml:
|
@@ -11,7 +18,7 @@
|
|
11
18
|
* Return value: a newly allocated string, NULL on parse error. The returned data must be freed using @lsm_mtex_free_mathml_buffer.
|
12
19
|
*/
|
13
20
|
|
14
|
-
extern char * lsm_mtex_to_mathml (const char *mtex, gssize size, int
|
21
|
+
extern char * lsm_mtex_to_mathml (const char *mtex, gssize size, int delimiter, int render_type);
|
15
22
|
|
16
23
|
/**
|
17
24
|
* lsm_mtex_free_mathml_buffer:
|
@@ -21,3 +28,5 @@ extern char * lsm_mtex_to_mathml (const char *mtex, gssize size, int global_star
|
|
21
28
|
*/
|
22
29
|
|
23
30
|
extern void lsm_mtex_free_mathml_buffer (char *mathml);
|
31
|
+
|
32
|
+
#endif
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/****************************************************************************
|
2
|
-
*
|
2
|
+
* Mathematical Copyright(c) 2014, Garen J. Torikian, All rights reserved.
|
3
3
|
* --------------------------------------------------------------------------
|
4
4
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
5
|
* of this software and associated documentation files (the "Software"), to deal
|
@@ -36,28 +36,28 @@ static VALUE rb_eDocumentCreationError;
|
|
36
36
|
// Raised when the SVG document could not be read
|
37
37
|
static VALUE rb_eDocumentReadError;
|
38
38
|
|
39
|
-
// defines the numeric equation label
|
40
|
-
static int global_start = 1;
|
41
|
-
|
42
39
|
static VALUE MATHEMATICAL_init(VALUE self, VALUE rb_Options)
|
43
40
|
{
|
44
41
|
Check_Type (rb_Options, T_HASH);
|
45
|
-
VALUE rb_ppi, rb_zoom, rb_maxsize, rb_format;
|
42
|
+
VALUE rb_ppi, rb_zoom, rb_maxsize, rb_format, rb_delimiter;
|
46
43
|
|
47
44
|
rb_ppi = rb_hash_aref(rb_Options, CSTR2SYM("ppi"));
|
48
45
|
rb_zoom = rb_hash_aref(rb_Options, CSTR2SYM("zoom"));
|
49
46
|
rb_maxsize = rb_hash_aref(rb_Options, CSTR2SYM("maxsize"));
|
50
47
|
rb_format = rb_hash_aref(rb_Options, CSTR2SYM("formatInt"));
|
48
|
+
rb_delimiter = rb_hash_aref(rb_Options, CSTR2SYM("delimiter"));
|
51
49
|
|
52
50
|
Check_Type(rb_ppi, T_FLOAT);
|
53
51
|
Check_Type(rb_zoom, T_FLOAT);
|
54
52
|
Check_Type(rb_maxsize, T_FIXNUM);
|
55
53
|
Check_Type(rb_format, T_FIXNUM);
|
54
|
+
Check_Type(rb_delimiter, T_FIXNUM);
|
56
55
|
|
57
56
|
rb_iv_set(self, "@ppi", rb_ppi);
|
58
57
|
rb_iv_set(self, "@zoom", rb_zoom);
|
59
58
|
rb_iv_set(self, "@maxsize", rb_maxsize);
|
60
59
|
rb_iv_set(self, "@format", rb_format);
|
60
|
+
rb_iv_set(self, "@delimiter", rb_delimiter);
|
61
61
|
|
62
62
|
rb_iv_set(self, "@png", Qnil);
|
63
63
|
rb_iv_set(self, "@svg", Qnil);
|
@@ -86,7 +86,7 @@ static VALUE process_rescue(VALUE args, VALUE exception_object)
|
|
86
86
|
return rescue_hash;
|
87
87
|
}
|
88
88
|
|
89
|
-
VALUE process(VALUE self, unsigned long maxsize, const char *latex_code, unsigned long latex_size)
|
89
|
+
VALUE process(VALUE self, unsigned long maxsize, const char *latex_code, unsigned long latex_size, int delimiter, int parse_type)
|
90
90
|
{
|
91
91
|
if (latex_size > maxsize) {
|
92
92
|
print_and_raise(rb_eMaxsizeError, "Size of latex string is greater than the maxsize");
|
@@ -95,15 +95,10 @@ VALUE process(VALUE self, unsigned long maxsize, const char *latex_code, unsigne
|
|
95
95
|
VALUE result_hash = rb_hash_new();
|
96
96
|
FileFormat format = (FileFormat) FIX2INT(rb_iv_get(self, "@format"));
|
97
97
|
|
98
|
-
|
99
|
-
char * mathml = lsm_mtex_to_mathml(latex_code, latex_size,
|
98
|
+
/* convert the TeX math to MathML */
|
99
|
+
char * mathml = lsm_mtex_to_mathml(latex_code, latex_size, delimiter, parse_type);
|
100
100
|
if (mathml == NULL) { print_and_raise(rb_eParseError, "Failed to parse mtex"); }
|
101
101
|
|
102
|
-
// basically, only update the next equation counter if the last math had a numbered equation
|
103
|
-
if (strstr(mathml, "<mlabeledtr>") != NULL) {
|
104
|
-
global_start++;
|
105
|
-
}
|
106
|
-
|
107
102
|
if (format == FORMAT_MATHML) {
|
108
103
|
rb_hash_aset (result_hash, CSTR2SYM ("data"), rb_str_new2(mathml));
|
109
104
|
mtex2MML_free_string(mathml);
|
@@ -185,28 +180,31 @@ VALUE process(VALUE self, unsigned long maxsize, const char *latex_code, unsigne
|
|
185
180
|
rb_hash_aset (result_hash, CSTR2SYM ("width"), INT2FIX(width_pt));
|
186
181
|
rb_hash_aset (result_hash, CSTR2SYM ("height"), INT2FIX(height_pt));
|
187
182
|
|
188
|
-
|
183
|
+
/* we need to clear out this key when attempting multiple calls. See http://git.io/i1hblQ */
|
189
184
|
rb_iv_set(self, "@svg", Qnil);
|
190
185
|
rb_iv_set(self, "@png", Qnil);
|
191
186
|
|
192
187
|
return result_hash;
|
193
188
|
}
|
194
189
|
|
195
|
-
|
196
|
-
|
197
|
-
|
190
|
+
/* `process` can potentially raise a bunch of exceptions, so we need to wrap
|
191
|
+
the call in a rescue. And `rb_rescue` only takes one argument, so we need
|
192
|
+
to pack everything in an array, and then unpack it in `process_helper`. */
|
198
193
|
static VALUE process_helper(VALUE data)
|
199
194
|
{
|
200
195
|
VALUE *args = (VALUE *) data;
|
201
196
|
|
202
|
-
return process(args[0], NUM2ULONG(args[1]), StringValueCStr(args[2]), NUM2ULONG(args[3]));
|
197
|
+
return process(args[0], NUM2ULONG(args[1]), StringValueCStr(args[2]), NUM2ULONG(args[3]), NUM2INT(args[4]), NUM2INT(args[5]));
|
203
198
|
}
|
204
199
|
|
205
|
-
static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
|
200
|
+
static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input, VALUE rb_ParseType)
|
206
201
|
{
|
202
|
+
Check_Type(rb_ParseType, T_FIXNUM);
|
203
|
+
|
207
204
|
unsigned long maxsize = (unsigned long) FIX2INT(rb_iv_get(self, "@maxsize"));
|
208
205
|
|
209
|
-
|
206
|
+
/* make sure that the passed latex string is not larger than the maximum value of
|
207
|
+
a signed long (or the maxsize option) */
|
210
208
|
if (maxsize == 0) {
|
211
209
|
maxsize = LONG_MAX;
|
212
210
|
}
|
@@ -225,11 +223,13 @@ static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
|
|
225
223
|
latex_code = StringValueCStr(rb_Input);
|
226
224
|
latex_size = (unsigned long) strlen(latex_code);
|
227
225
|
|
228
|
-
VALUE args[
|
226
|
+
VALUE args[6];
|
229
227
|
args[0] = self;
|
230
228
|
args[1] = ULONG2NUM(maxsize);
|
231
229
|
args[2] = rb_Input;
|
232
230
|
args[3] = ULONG2NUM(latex_size);
|
231
|
+
args[4] = rb_iv_get(self, "@delimiter");
|
232
|
+
args[5] = rb_ParseType;
|
233
233
|
|
234
234
|
output = rb_rescue(process_helper, args, process_rescue, rb_Input);
|
235
235
|
break;
|
@@ -238,21 +238,22 @@ static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
|
|
238
238
|
int length = RARRAY_LEN(rb_Input), i;
|
239
239
|
VALUE hash;
|
240
240
|
output = rb_ary_new2(length);
|
241
|
-
global_start = 1;
|
242
241
|
|
243
242
|
for (i = 0; i < length; i++) {
|
244
|
-
|
243
|
+
/* grab the ith element */
|
245
244
|
VALUE math = rb_ary_entry(rb_Input, i);
|
246
245
|
|
247
|
-
|
246
|
+
/* get the string and length */
|
248
247
|
latex_code = StringValueCStr(math);
|
249
248
|
latex_size = (unsigned long) strlen(latex_code);
|
250
249
|
|
251
|
-
VALUE args[
|
250
|
+
VALUE args[6];
|
252
251
|
args[0] = self;
|
253
252
|
args[1] = ULONG2NUM(maxsize);
|
254
253
|
args[2] = math;
|
255
254
|
args[3] = ULONG2NUM(latex_size);
|
255
|
+
args[4] = rb_iv_get(self, "@delimiter");
|
256
|
+
args[5] = rb_ParseType;
|
256
257
|
|
257
258
|
hash = rb_rescue(process_helper, args, process_rescue, math);
|
258
259
|
|
@@ -282,5 +283,5 @@ void Init_mathematical()
|
|
282
283
|
rb_eDocumentReadError = rb_define_class_under(rb_mMathematical, "DocumentReadError", rb_eStandardError);
|
283
284
|
|
284
285
|
rb_define_method(rb_cMathematicalProcess, "initialize", MATHEMATICAL_init, 1);
|
285
|
-
rb_define_method(rb_cMathematicalProcess, "process", MATHEMATICAL_process,
|
286
|
+
rb_define_method(rb_cMathematicalProcess, "process", MATHEMATICAL_process, 2);
|
286
287
|
}
|
@@ -1,3 +1,6 @@
|
|
1
|
+
#ifndef MATHEMATICAL_H
|
2
|
+
#define MATHEMATICAL_H
|
3
|
+
|
1
4
|
#include "ruby.h"
|
2
5
|
|
3
6
|
#include <string.h>
|
@@ -13,7 +16,6 @@
|
|
13
16
|
#include <gio/gio.h>
|
14
17
|
#include <cairo-svg.h>
|
15
18
|
|
16
|
-
#include "mtex2MML.h"
|
17
19
|
#include <cairo_callbacks.h>
|
18
20
|
#include <lasem_overrides.h>
|
19
21
|
|
@@ -22,3 +24,5 @@ typedef enum {
|
|
22
24
|
FORMAT_PNG,
|
23
25
|
FORMAT_MATHML
|
24
26
|
} FileFormat;
|
27
|
+
|
28
|
+
#endif
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
//
|
3
|
+
// strdup.c
|
4
|
+
//
|
5
|
+
// Copyright (c) 2014 Stephen Mathieson
|
6
|
+
// MIT licensed
|
7
|
+
//
|
8
|
+
|
9
|
+
#ifndef HAVE_STRDUP
|
10
|
+
|
11
|
+
#include <stdlib.h>
|
12
|
+
#include <string.h>
|
13
|
+
#include "strdup.h"
|
14
|
+
|
15
|
+
char *
|
16
|
+
strdup(const char *str) {
|
17
|
+
if (!str) return NULL;
|
18
|
+
int len = strlen(str) + 1;
|
19
|
+
char *buf = malloc(len);
|
20
|
+
if (buf) memcpy(buf, str, len);
|
21
|
+
return buf;
|
22
|
+
}
|
23
|
+
|
24
|
+
#endif /* HAVE_STRDUP */
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
//
|
3
|
+
// strdup.h
|
4
|
+
//
|
5
|
+
// Copyright (c) 2014 Stephen Mathieson
|
6
|
+
// MIT licensed
|
7
|
+
//
|
8
|
+
|
9
|
+
#ifndef HAVE_STRDUP
|
10
|
+
#define HAVE_STRDUP
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Drop-in replacement for strdup(3) from libc.
|
14
|
+
*
|
15
|
+
* Creates a copy of `str`. Free when done.
|
16
|
+
*
|
17
|
+
* Returns a pointer to the newly allocated
|
18
|
+
* copy of `str`, or `NULL` on failure.
|
19
|
+
*/
|
20
|
+
|
21
|
+
char *
|
22
|
+
strdup(const char *str);
|
23
|
+
|
24
|
+
#endif /* HAVE_STRDUP */
|