-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.jslatex.js
69 lines (65 loc) · 1.86 KB
/
jquery.jslatex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* jsLaTeX v1.2.2 - A jQuery plugin to directly embed LaTeX into your website or blog
*
* Copyright (c) 2009 Andreas Grech
*
* Licensed under the WTFPL license:
* http://www.wtfpl.net/about/
*
* http://dreasgrech.com
*/
(function ($) {
var attachToImage = function () {
return $("<img/>").attr({
src: this.src
});
},
formats = {
'gif': attachToImage,
'png': attachToImage,
'swf': function () {
return $("<embed/>").attr({
src: this.src,
type: 'application/x-shockwave-flash'
});
}
},
sections = {
'{f}': 'format',
'{e}': 'equation'
},
escapes = {
'+': '2B',
'=': '3D'
};
$.fn.latex = function (opts) {
opts = $.extend({},
$.fn.latex.defaults, opts);
opts.format = formats[opts.format] ? opts.format : 'gif';
return this.each(function () {
var $this = $(this),
format, s, element, url = opts.url;
opts.equation = $.trim($this.text());
for (s in sections) {
if (sections.hasOwnProperty(s) && (format = url.indexOf(s)) >= 0) {
url = url.replace(s, opts[sections[s]]);
}
}
for (s in escapes) {
if (escapes.hasOwnProperty(s) && (format = url.indexOf(s)) >= 0) {
url = url.replace(s, '%' + escapes[s]);
}
}
opts.src = url;
element = formats[opts.format].call(opts);
$this.html('').append(element);
if (opts.callback) {
opts.callback.call(element);
}
});
};
$.fn.latex.defaults = {
format: 'gif',
url: 'http://latex.codecogs.com/{f}.latex?{e}'
};
}(jQuery));