added jsMath files
[lambda.git] / jsMath / extensions / HTML.js
1 /*
2  *  extensions/HTML.js
3  *  
4  *  Part of the jsMath package for mathematics on the web.
5  *
6  *  This file implements a number of HTML-specific extensions to TeX,
7  *  including \color, \style, \class, \unicode, etc.  It will be loaded
8  *  automatically when needed, or can be loaded by
9  *  
10  *    jsMath.Extension.Require('HTML');
11  *
12  *  ---------------------------------------------------------------------
13  *
14  *  Copyright 2005-2006 by Davide P. Cervone
15  * 
16  *  Licensed under the Apache License, Version 2.0 (the "License");
17  *  you may not use this file except in compliance with the License.
18  *  You may obtain a copy of the License at
19  * 
20  *      http://www.apache.org/licenses/LICENSE-2.0
21  * 
22  *  Unless required by applicable law or agreed to in writing, software
23  *  distributed under the License is distributed on an "AS IS" BASIS,
24  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  *  See the License for the specific language governing permissions and
26  *  limitations under the License.
27  */
28
29 /********************************************************************/
30
31 jsMath.Package(jsMath.Parser,{
32
33   macros: {
34     color:      'Color',
35     href:       'Href',
36     'class':    'Class',
37     style:      'Style',
38     cssId:      'CSSId',
39     unicode:    'Unicode'
40   },
41   
42   /*
43    *  Show the argument in a particular color
44    */
45   Color: function (name) {
46     var color = this.GetArgument(this.cmd+name); if (this.error) return;
47     this.CheckHTML(color,name); if (this.error) return;
48     // check that it looks like a color?
49     this.AddHTML(name,['<span style="color: '+color+'">','</span>']);
50   },
51   
52   /*
53    *  Make the argument be a link
54    */
55   Href: function (name) {
56     var href = this.GetArgument(this.cmd+name); if (this.error) return;
57     this.CheckHTML(href,name); if (this.error) return;
58     this.AddHTML(name,['<a class="link" href="'+href+'">','</a>']);
59   },
60   
61   /*
62    *  Apply a CSS class to the argument
63    */
64   Class: function (name) {
65     var clss = this.GetArgument(this.cmd+name); if (this.error) return;
66     this.CheckHTML(clss,name); if (this.error) return;
67     this.AddHTML(name,['<span class="'+clss+'">','</span>']);
68   },
69   
70   /*
71    *  Apply a CSS style to the argument
72    */
73   Style: function (name) {
74     var style = this.GetArgument(this.cmd+name); if (this.error) return;
75     this.CheckHTML(style,name); if (this.error) return;
76     this.AddHTML(name,['<span style="'+style+'">','</span>']);
77   },
78   
79   /*
80    *  Add a CSS element ID to the argument
81    */
82   CSSId: function (name) {
83     var id = this.GetArgument(this.cmd+name); if (this.error) return;
84     this.CheckHTML(id,name); if (this.error) return;
85     this.AddHTML(name,['<span id="'+id+'">','</span>']);
86   },
87   
88   /*
89    *  Insert some raw HTML around the argument (this will not affect
90    *  the spacing or other TeX features)
91    */
92   AddHTML: function (name,params) {
93     var data = this.mlist.data;
94     var arg = this.GetArgument(this.cmd+name); if (this.error) return;
95     arg = jsMath.Parse(arg,data.font,data.size,data.style);
96       if (arg.error) {this.Error(arg); return}
97     this.mlist.Add(jsMath.mItem.HTML(params[0]));
98     for (var i = 0; i < arg.mlist.Length(); i++) {this.mlist.Add(arg.mlist.Get(i))}
99     this.mlist.Add(jsMath.mItem.HTML(params[1]));
100   },
101   
102   CheckHTML: function (data,name) {
103     if (data.match(/[<>&"]/))
104       {this.Error("Can't include raw HTML in first argument of "+this.cmd+name)}
105   },
106   
107   /*
108    *  Insert a unicode reference as an Ord atom.  Its argument should
109    *  be the unicode code point, e.g. \unicode{8211}, or \unicode{x203F}.
110    *  You can also specify the height (offset from the x height) and depth
111    *  in ems, together with a CSS class for the character, e.g.,
112    *  \unicode{8211,class,.2,-.3}
113    */
114   Unicode: function (name) {
115     var arg = this.GetArgument(this.cmd+name); if (this.error) return;
116     this.CheckHTML(arg,name); if (this.error) return;
117     arg = arg.split(','); arg[0] = '&#'+arg[0]+';';
118     if (!arg[1]) {arg[1] = 'normal'}
119     this.mlist.Add(jsMath.mItem.TextAtom('ord',arg[0],arg[1],arg[2],arg[3]));
120   }
121
122 });