summaryrefslogtreecommitdiff
path: root/js/latexit.js
blob: f6d76b93f71a912e65931d8e7f43d07121d67e0a (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* LaTeX IT - JavaScript to Convert Latex within an HTML page into Equations
* Copyright (C) 2009 William Bateman, 2008 Waipot Ngamsaad 

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var LatexIT = {
	mode : 'gif',
	imgnum : 0,
	isFirefox:false,
	init : function() {
		// We need to review the support for SVG. Latest released versions are not supporting this as they should
  //  if(document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1"))
	//	  this.mode='svg';

    // browser name
		// svg support in FireFox is not allowing two images to occur currently on the same line. 
		
		var ua = navigator.userAgent.toLowerCase(); 
		if(ua.indexOf("firefox")!=-1)
		{
      // this.isFirefox = true;
		}
  },

	pre : function(txt) {
		if ( !txt.match(/<img.*?>/i) )
		{
			//Clean code
			txt=txt.replace(/<br>/mgi,"");
			txt=txt.replace(/<br \/>/mgi,"");
			//Create img tag
		//	txt = " <img src=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" /> ";
		//	txt = " <object type=\"image/svg+xml\" width=\"20\" data=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" /> ";
		  
		  if(this.mode=='svg')  
			{
				// Best for Firefox
				if(this.isFirefox) 
  		   	txt = " <object type=\"image/svg+xml\" data=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" class=\"latex\" style=\"margin:0; padding:0; border:0\" /> ";
				else // Best for Chrome
  		   //	txt = " <object type=\"image/svg+xml\" data=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" class=\"latex\" /> ";
					txt = " <img src=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" alt=\""+ txt +"\" title=\""+ txt +"\" border=\"0\" class=\"latex\" /> ";
			}
			else 
	   	  txt = " <img src=\"http://latex.codecogs.com/"+this.mode+".latex?"+ txt +"\" alt=\""+txt+"\" border=\"0\" class=\"latex\" /> ";
		}
		return txt;
	},
	
	latex : function(txt) {
		var html, htmlinline;
		if(this.isFirefox) {
		  html=" <object type=\"image/svg+xml\" data=\"http://latex.codecogs.com/"+this.mode+".latex?$2\" class=\"latex\" /> ";
		  htmlinline=" <object type=\"image/svg+xml\" data=\"http://latex.codecogs.com/"+this.mode+".latex?\\inline $2\" class=\"latex\" /> ";
		}
		else {
		  html=" <img src=\"http://latex.codecogs.com/"+this.mode+".latex?$2\" border=\"0\" class=\"latex\" /> ";
		  htmlinline=" <img src=\"http://latex.codecogs.com/"+this.mode+".latex?\\inline $2\" border=\"0\" class=\"latex\" /> ";
		}
	
	
	  txt=txt.replace(/(^\$|[^\\]\$)(.*?[^\\])\$/gm, htmlinline);
		txt=txt.replace(/(^\\|[^\\]\\)\[(.*?[^\\])\\\]/mg," <br/>"+html+"<br/> "); 
		txt=txt.replace(/\\\$/mg,"\$"); 
		txt=txt.replace(/\\\\(\[|\])/mg,"$1");
		
		return txt;
	},	
	
	render : function(tag, latexmode) {
		var eqn = window.document.getElementsByTagName(tag);
		for (var i=0; i<eqn.length; i++) {
			if(latexmode)
			  eqn[i].innerHTML = LatexIT.latex(eqn[i].innerHTML);
			else if (eqn[i].getAttribute("lang") == "latex" || eqn[i].getAttribute("xml:lang") == "latex") 
			  eqn[i].innerHTML = LatexIT.pre(eqn[i].innerHTML);
		} 
	},

  add : function(tag, latexmode)
	{
		if(typeof(latexmode)=='undefined') latexmode=false; 
		if(window.addEventListener) 
			window.addEventListener('load', new Function('LatexIT.render("'+tag+'", '+latexmode+')'),false);
		else 
		  window.attachEvent('onload', new Function('LatexIT.render("'+tag+'", '+latexmode+')') );
	},

	scale : function(e,scale)
	{
		e.width =(e.width*scale);
		e.height=(e.height*scale);
	}
};

LatexIT.init();
LatexIT.add('*');