mathematical 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -9
  3. data/Rakefile +5 -24
  4. data/ext/mathematical/extconf.rb +8 -3
  5. data/ext/mathematical/lasem/src/lsmdomparser.c +7 -7
  6. data/ext/mathematical/lasem/src/lsmdomparser.h +2 -2
  7. data/ext/mathematical/lasem/src/lsmitex.c +8 -5
  8. data/ext/mathematical/lasem/src/lsmitex.h +1 -1
  9. data/ext/mathematical/lasem/src/lsmmathmldocument.c +2 -2
  10. data/ext/mathematical/lasem/src/lsmmathmldocument.h +1 -1
  11. data/ext/mathematical/lasem/tests/lsmtest.c +2 -2
  12. data/ext/mathematical/mathematical.c +119 -10
  13. data/ext/mathematical/mtex2MML/ext/extconf.rb +4 -0
  14. data/ext/mathematical/mtex2MML/src/lex.yy.c +6791 -0
  15. data/ext/mathematical/mtex2MML/src/mtex2MML.h +59 -0
  16. data/ext/mathematical/mtex2MML/src/parse_extras.c +306 -0
  17. data/ext/mathematical/mtex2MML/src/parse_extras.h +78 -0
  18. data/ext/mathematical/mtex2MML/src/stack.c +80 -0
  19. data/ext/mathematical/mtex2MML/src/stack.h +101 -0
  20. data/ext/mathematical/mtex2MML/src/string_extras.c +211 -0
  21. data/ext/mathematical/mtex2MML/src/string_extras.h +46 -0
  22. data/ext/mathematical/mtex2MML/src/y.tab.c +7090 -0
  23. data/ext/mathematical/mtex2MML/src/y.tab.h +393 -0
  24. data/lib/mathematical.rb +1 -0
  25. data/lib/mathematical/corrections.rb +34 -0
  26. data/lib/mathematical/render.rb +21 -12
  27. data/lib/mathematical/version.rb +1 -1
  28. data/mathematical.gemspec +2 -2
  29. data/test/mathematical/corrections_test.rb +56 -0
  30. data/test/mathematical/fixtures/png/pmatrix.png +0 -0
  31. data/test/mathematical/fixtures_test.rb +11 -1
  32. data/test/mathematical/maliciousness_test.rb +10 -2
  33. data/test/mathematical/mathml_test.rb +22 -0
  34. data/test/mathematical/png_test.rb +26 -0
  35. data/test/test_helper.rb +13 -0
  36. metadata +150 -134
  37. data/ext/mathematical/itexToMML/itex2MML.h +0 -63
  38. data/ext/mathematical/itexToMML/lex.yy.c +0 -6548
  39. data/ext/mathematical/itexToMML/y.tab.c +0 -6179
  40. data/ext/mathematical/itexToMML/y.tab.h +0 -389
@@ -0,0 +1,393 @@
1
+ /* A Bison parser, made by GNU Bison 2.3. */
2
+
3
+ /* Skeleton interface for Bison's Yacc-like parsers in C
4
+
5
+ Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
6
+ Free Software Foundation, Inc.
7
+
8
+ This program is free software; you can redistribute it and/or modify
9
+ it under the terms of the GNU General Public License as published by
10
+ the Free Software Foundation; either version 2, or (at your option)
11
+ any later version.
12
+
13
+ This program is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU General Public License for more details.
17
+
18
+ You should have received a copy of the GNU General Public License
19
+ along with this program; if not, write to the Free Software
20
+ Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
+ Boston, MA 02110-1301, USA. */
22
+
23
+ /* As a special exception, you may create a larger work that contains
24
+ part or all of the Bison parser skeleton and distribute that work
25
+ under terms of your choice, so long as that work isn't itself a
26
+ parser generator using the skeleton or a modified version thereof
27
+ as a parser skeleton. Alternatively, if you modify or redistribute
28
+ the parser skeleton itself, you may (at your option) remove this
29
+ special exception, which will cause the skeleton and the resulting
30
+ Bison output files to be licensed under the GNU General Public
31
+ License without this special exception.
32
+
33
+ This special exception was added by the Free Software Foundation in
34
+ version 2.2 of Bison. */
35
+
36
+ /* Tokens. */
37
+ #ifndef YYTOKENTYPE
38
+ # define YYTOKENTYPE
39
+ /* Put the tokens into the symbol table, so that GDB and other debuggers
40
+ know about them. */
41
+ enum yytokentype {
42
+ TEXATOP = 258,
43
+ TEXOVER = 259,
44
+ CHAR = 260,
45
+ STARTMATH = 261,
46
+ STARTDMATH = 262,
47
+ ENDMATH = 263,
48
+ MI = 264,
49
+ MIB = 265,
50
+ MN = 266,
51
+ MO = 267,
52
+ SUP = 268,
53
+ SUB = 269,
54
+ MROWOPEN = 270,
55
+ MROWCLOSE = 271,
56
+ LEFT = 272,
57
+ RIGHT = 273,
58
+ BIG = 274,
59
+ BBIG = 275,
60
+ BIGG = 276,
61
+ BBIGG = 277,
62
+ BIGL = 278,
63
+ BBIGL = 279,
64
+ BIGGL = 280,
65
+ BBIGGL = 281,
66
+ FRAC = 282,
67
+ TFRAC = 283,
68
+ OPERATORNAME = 284,
69
+ MATHOP = 285,
70
+ MATHBIN = 286,
71
+ MATHREL = 287,
72
+ MOP = 288,
73
+ MOL = 289,
74
+ MOLL = 290,
75
+ MOF = 291,
76
+ MOR = 292,
77
+ PERIODDELIM = 293,
78
+ OTHERDELIM = 294,
79
+ LEFTDELIM = 295,
80
+ RIGHTDELIM = 296,
81
+ MOS = 297,
82
+ MOB = 298,
83
+ SQRT = 299,
84
+ ROOT = 300,
85
+ BINOM = 301,
86
+ TBINOM = 302,
87
+ UNDER = 303,
88
+ OVER = 304,
89
+ OVERBRACE = 305,
90
+ UNDERLINE = 306,
91
+ UNDERBRACE = 307,
92
+ UNDEROVER = 308,
93
+ TENSOR = 309,
94
+ MULTI = 310,
95
+ ARRAYALIGN = 311,
96
+ ROWSPACINGDEF = 312,
97
+ ROWLINESDEF = 313,
98
+ COLUMNALIGN = 314,
99
+ ARRAY = 315,
100
+ COLSEP = 316,
101
+ ROWSEP = 317,
102
+ ARRAYOPTS = 318,
103
+ COLLAYOUT = 319,
104
+ COLALIGN = 320,
105
+ ROWALIGN = 321,
106
+ ALIGN = 322,
107
+ EQROWS = 323,
108
+ EQCOLS = 324,
109
+ ROWLINES = 325,
110
+ COLLINES = 326,
111
+ FRAME = 327,
112
+ PADDING = 328,
113
+ ATTRLIST = 329,
114
+ ITALICS = 330,
115
+ SANS = 331,
116
+ TT = 332,
117
+ BOLD = 333,
118
+ BOXED = 334,
119
+ SLASHED = 335,
120
+ RM = 336,
121
+ BB = 337,
122
+ ST = 338,
123
+ END = 339,
124
+ BBLOWERCHAR = 340,
125
+ BBUPPERCHAR = 341,
126
+ BBDIGIT = 342,
127
+ CALCHAR = 343,
128
+ FRAKCHAR = 344,
129
+ CAL = 345,
130
+ FRAK = 346,
131
+ CLAP = 347,
132
+ LLAP = 348,
133
+ RLAP = 349,
134
+ ROWOPTS = 350,
135
+ TEXTSIZE = 351,
136
+ SCSIZE = 352,
137
+ SCSCSIZE = 353,
138
+ DISPLAY = 354,
139
+ TEXTSTY = 355,
140
+ TEXTBOX = 356,
141
+ TEXTSTRING = 357,
142
+ XMLSTRING = 358,
143
+ CELLOPTS = 359,
144
+ ROWSPAN = 360,
145
+ COLSPAN = 361,
146
+ THINSPACE = 362,
147
+ MEDSPACE = 363,
148
+ THICKSPACE = 364,
149
+ QUAD = 365,
150
+ QQUAD = 366,
151
+ NEGSPACE = 367,
152
+ NEGMEDSPACE = 368,
153
+ NEGTHICKSPACE = 369,
154
+ PHANTOM = 370,
155
+ HREF = 371,
156
+ UNKNOWNCHAR = 372,
157
+ EMPTYMROW = 373,
158
+ STATLINE = 374,
159
+ TOOLTIP = 375,
160
+ TOGGLE = 376,
161
+ TOGGLESTART = 377,
162
+ TOGGLEEND = 378,
163
+ FGHIGHLIGHT = 379,
164
+ BGHIGHLIGHT = 380,
165
+ SPACE = 381,
166
+ INTONE = 382,
167
+ INTTWO = 383,
168
+ INTTHREE = 384,
169
+ BAR = 385,
170
+ WIDEBAR = 386,
171
+ VEC = 387,
172
+ WIDEVEC = 388,
173
+ HAT = 389,
174
+ WIDEHAT = 390,
175
+ CHECK = 391,
176
+ WIDECHECK = 392,
177
+ TILDE = 393,
178
+ WIDETILDE = 394,
179
+ DOT = 395,
180
+ DDOT = 396,
181
+ DDDOT = 397,
182
+ DDDDOT = 398,
183
+ UNARYMINUS = 399,
184
+ UNARYPLUS = 400,
185
+ BEGINENV = 401,
186
+ ENDENV = 402,
187
+ MATRIX = 403,
188
+ PMATRIX = 404,
189
+ BMATRIX = 405,
190
+ BBMATRIX = 406,
191
+ VMATRIX = 407,
192
+ VVMATRIX = 408,
193
+ SVG = 409,
194
+ ENDSVG = 410,
195
+ SMALLMATRIX = 411,
196
+ CASES = 412,
197
+ ALIGNED = 413,
198
+ GATHERED = 414,
199
+ SUBSTACK = 415,
200
+ PMOD = 416,
201
+ RMCHAR = 417,
202
+ COLOR = 418,
203
+ BGCOLOR = 419,
204
+ XARROW = 420,
205
+ OPTARGOPEN = 421,
206
+ OPTARGCLOSE = 422,
207
+ MTEXNUM = 423,
208
+ RAISEBOX = 424,
209
+ NEG = 425
210
+ };
211
+ #endif
212
+ /* Tokens. */
213
+ #define TEXATOP 258
214
+ #define TEXOVER 259
215
+ #define CHAR 260
216
+ #define STARTMATH 261
217
+ #define STARTDMATH 262
218
+ #define ENDMATH 263
219
+ #define MI 264
220
+ #define MIB 265
221
+ #define MN 266
222
+ #define MO 267
223
+ #define SUP 268
224
+ #define SUB 269
225
+ #define MROWOPEN 270
226
+ #define MROWCLOSE 271
227
+ #define LEFT 272
228
+ #define RIGHT 273
229
+ #define BIG 274
230
+ #define BBIG 275
231
+ #define BIGG 276
232
+ #define BBIGG 277
233
+ #define BIGL 278
234
+ #define BBIGL 279
235
+ #define BIGGL 280
236
+ #define BBIGGL 281
237
+ #define FRAC 282
238
+ #define TFRAC 283
239
+ #define OPERATORNAME 284
240
+ #define MATHOP 285
241
+ #define MATHBIN 286
242
+ #define MATHREL 287
243
+ #define MOP 288
244
+ #define MOL 289
245
+ #define MOLL 290
246
+ #define MOF 291
247
+ #define MOR 292
248
+ #define PERIODDELIM 293
249
+ #define OTHERDELIM 294
250
+ #define LEFTDELIM 295
251
+ #define RIGHTDELIM 296
252
+ #define MOS 297
253
+ #define MOB 298
254
+ #define SQRT 299
255
+ #define ROOT 300
256
+ #define BINOM 301
257
+ #define TBINOM 302
258
+ #define UNDER 303
259
+ #define OVER 304
260
+ #define OVERBRACE 305
261
+ #define UNDERLINE 306
262
+ #define UNDERBRACE 307
263
+ #define UNDEROVER 308
264
+ #define TENSOR 309
265
+ #define MULTI 310
266
+ #define ARRAYALIGN 311
267
+ #define ROWSPACINGDEF 312
268
+ #define ROWLINESDEF 313
269
+ #define COLUMNALIGN 314
270
+ #define ARRAY 315
271
+ #define COLSEP 316
272
+ #define ROWSEP 317
273
+ #define ARRAYOPTS 318
274
+ #define COLLAYOUT 319
275
+ #define COLALIGN 320
276
+ #define ROWALIGN 321
277
+ #define ALIGN 322
278
+ #define EQROWS 323
279
+ #define EQCOLS 324
280
+ #define ROWLINES 325
281
+ #define COLLINES 326
282
+ #define FRAME 327
283
+ #define PADDING 328
284
+ #define ATTRLIST 329
285
+ #define ITALICS 330
286
+ #define SANS 331
287
+ #define TT 332
288
+ #define BOLD 333
289
+ #define BOXED 334
290
+ #define SLASHED 335
291
+ #define RM 336
292
+ #define BB 337
293
+ #define ST 338
294
+ #define END 339
295
+ #define BBLOWERCHAR 340
296
+ #define BBUPPERCHAR 341
297
+ #define BBDIGIT 342
298
+ #define CALCHAR 343
299
+ #define FRAKCHAR 344
300
+ #define CAL 345
301
+ #define FRAK 346
302
+ #define CLAP 347
303
+ #define LLAP 348
304
+ #define RLAP 349
305
+ #define ROWOPTS 350
306
+ #define TEXTSIZE 351
307
+ #define SCSIZE 352
308
+ #define SCSCSIZE 353
309
+ #define DISPLAY 354
310
+ #define TEXTSTY 355
311
+ #define TEXTBOX 356
312
+ #define TEXTSTRING 357
313
+ #define XMLSTRING 358
314
+ #define CELLOPTS 359
315
+ #define ROWSPAN 360
316
+ #define COLSPAN 361
317
+ #define THINSPACE 362
318
+ #define MEDSPACE 363
319
+ #define THICKSPACE 364
320
+ #define QUAD 365
321
+ #define QQUAD 366
322
+ #define NEGSPACE 367
323
+ #define NEGMEDSPACE 368
324
+ #define NEGTHICKSPACE 369
325
+ #define PHANTOM 370
326
+ #define HREF 371
327
+ #define UNKNOWNCHAR 372
328
+ #define EMPTYMROW 373
329
+ #define STATLINE 374
330
+ #define TOOLTIP 375
331
+ #define TOGGLE 376
332
+ #define TOGGLESTART 377
333
+ #define TOGGLEEND 378
334
+ #define FGHIGHLIGHT 379
335
+ #define BGHIGHLIGHT 380
336
+ #define SPACE 381
337
+ #define INTONE 382
338
+ #define INTTWO 383
339
+ #define INTTHREE 384
340
+ #define BAR 385
341
+ #define WIDEBAR 386
342
+ #define VEC 387
343
+ #define WIDEVEC 388
344
+ #define HAT 389
345
+ #define WIDEHAT 390
346
+ #define CHECK 391
347
+ #define WIDECHECK 392
348
+ #define TILDE 393
349
+ #define WIDETILDE 394
350
+ #define DOT 395
351
+ #define DDOT 396
352
+ #define DDDOT 397
353
+ #define DDDDOT 398
354
+ #define UNARYMINUS 399
355
+ #define UNARYPLUS 400
356
+ #define BEGINENV 401
357
+ #define ENDENV 402
358
+ #define MATRIX 403
359
+ #define PMATRIX 404
360
+ #define BMATRIX 405
361
+ #define BBMATRIX 406
362
+ #define VMATRIX 407
363
+ #define VVMATRIX 408
364
+ #define SVG 409
365
+ #define ENDSVG 410
366
+ #define SMALLMATRIX 411
367
+ #define CASES 412
368
+ #define ALIGNED 413
369
+ #define GATHERED 414
370
+ #define SUBSTACK 415
371
+ #define PMOD 416
372
+ #define RMCHAR 417
373
+ #define COLOR 418
374
+ #define BGCOLOR 419
375
+ #define XARROW 420
376
+ #define OPTARGOPEN 421
377
+ #define OPTARGCLOSE 422
378
+ #define MTEXNUM 423
379
+ #define RAISEBOX 424
380
+ #define NEG 425
381
+
382
+
383
+
384
+
385
+ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
386
+ typedef int YYSTYPE;
387
+ # define yystype YYSTYPE /* obsolescent; will be withdrawn */
388
+ # define YYSTYPE_IS_DECLARED 1
389
+ # define YYSTYPE_IS_TRIVIAL 1
390
+ #endif
391
+
392
+ extern YYSTYPE mtex2MML_yylval;
393
+
data/lib/mathematical.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "mathematical/mathematical"
2
2
 
3
+ require "mathematical/corrections"
3
4
  require "mathematical/render"
4
5
  require "mathematical/version"
5
6
 
@@ -0,0 +1,34 @@
1
+ module Mathematical
2
+ module Corrections
3
+
4
+ def apply_corrections(maths)
5
+ maths = adjust_alignment(maths)
6
+ maths = adjust_frac(maths)
7
+ maths = adjust_lt_gt(maths)
8
+ maths = adjust_limits(maths)
9
+ end
10
+
11
+ # `{align}` *should* be valid, according to AMS-Latex, but it seems itex@1.5.1 does not like it.
12
+ def adjust_alignment(maths)
13
+ maths = maths.gsub(/\\begin\{align\}/, "\\begin{aligned}")
14
+ maths.gsub(/\\end\{align\}/, "\\end{aligned}")
15
+ end
16
+
17
+ # seems like KaTeX/MathJax supports \frac\d\d, but itex does not
18
+ def adjust_frac(maths)
19
+ maths.gsub(/\\frac(\d)(\d)/, '\frac{\1}{\2}')
20
+ end
21
+
22
+ # from the itex website:
23
+ # It is possible (though probably not recommended) to insert MathML markup
24
+ # inside itex equations. So "<" and ">" are significant.
25
+ # To obtain a less-than or greater-than sign, you should use \lt or \gt, respectively.
26
+ def adjust_lt_gt(maths)
27
+ maths.gsub(/</, '\lt').gsub(/>/, '\gt')
28
+ end
29
+
30
+ def adjust_limits(maths)
31
+ maths.gsub(/\\int\\limits_/, '\int_')
32
+ end
33
+ end
34
+ end
@@ -4,37 +4,46 @@ require 'tempfile'
4
4
 
5
5
  module Mathematical
6
6
  class Render
7
+ include Corrections
8
+
9
+ FORMAT_TYPES = %w(svg png mathml)
10
+
7
11
  DEFAULT_OPTS = {
8
12
  :ppi => 72.0,
9
13
  :zoom => 1.0,
10
14
  :base64 => false,
11
- :maxsize => 0
15
+ :maxsize => 0,
16
+ :format => "svg"
12
17
  }
13
18
 
14
19
  def initialize(opts = {})
15
20
  @config = DEFAULT_OPTS.merge(opts)
21
+
16
22
  raise(TypeError, "maxsize must be an integer!") unless @config[:maxsize].is_a? Fixnum
17
23
  raise(TypeError, "maxsize cannot be less than 0!") if @config[:maxsize] < 0
24
+ raise(TypeError, "format must be a string!") unless @config[:format].is_a? String
25
+ raise(TypeError, "format type must be one of the following formats: #{FORMAT_TYPES.join(', ')}") unless FORMAT_TYPES.include?(@config[:format])
26
+
18
27
  @processer = Mathematical::Process.new(@config)
19
28
  end
20
29
 
21
30
  def render(maths)
22
31
  raise(TypeError, "text must be a string!") unless maths.is_a? String
32
+ maths = maths.strip
23
33
  raise(ArgumentError, "text must be in itex format (`$...$` or `$$...$$`)!") unless maths =~ /\A\${1,2}/
24
34
 
25
- # seems to be a bug in itex@1.5.1 where the "Vertical spacing and page breaks in multiline display" (\\)
26
- # do not work, and yield an "unknown character" error
27
- maths.gsub!(/\\\\/, "\\\\\\\\")
28
-
29
- # `{align}` *should* be valid, according to AMS-Latex, but it seems itex@1.5.1 does not like it.
30
- maths.gsub!(/\\begin\{align\}/, "\\begin{aligned}")
31
- maths.gsub!(/\\end\{align\}/, "\\end{aligned}")
35
+ maths = apply_corrections(maths)
32
36
 
33
37
  begin
34
- raise RuntimeError unless svg_hash = @processer.process(maths)
35
- svg_hash["svg"] = svg_hash["svg"][xml_header.length..-1] # remove starting <?xml...> tag
36
- svg_hash["svg"] = svg_to_base64(svg_hash["svg"]) if @config[:base64]
37
- svg_hash
38
+ raise RuntimeError unless data_hash = @processer.process(maths)
39
+ case @config[:format]
40
+ when "svg"
41
+ data_hash["svg"] = data_hash["svg"][xml_header.length..-1] # remove starting <?xml...> tag
42
+ data_hash["svg"] = svg_to_base64(data_hash["svg"]) if @config[:base64]
43
+ data_hash
44
+ when "png", "mathml"
45
+ data_hash
46
+ end
38
47
  rescue ParseError, DocumentCreationError, DocumentReadError => e # an error in the C code, probably a bad TeX parse
39
48
  $stderr.puts "#{e.message}: #{maths}"
40
49
  maths