Rev 12246 | Rev 12259 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
8185 | bpr | 17 | #include "wims.h" |
10 | reyssat | 18 | |
19 | /* internal variable */ |
||
20 | int rawmath_easy=0; |
||
21 | |||
8185 | bpr | 22 | struct hmname hmname[]={ |
12229 | bpr | 23 | {"CC", "", ""}, |
24 | {"Delta", "", ""}, |
||
25 | {"Gamma","", ""}, |
||
26 | {"Inf", "$(m_infty)","\\infty"}, |
||
27 | {"Lambda","", ""}, |
||
28 | {"NN", "", ""}, |
||
29 | {"Omega", "", ""}, |
||
30 | {"Phi", "", ""}, |
||
31 | {"Pi", "", ""}, |
||
32 | {"Psi", "", ""}, |
||
33 | {"QQ", "",""}, |
||
34 | {"RR", "",""}, |
||
35 | {"Sigma", "",""}, |
||
36 | {"Xi", "",""}, |
||
37 | {"ZZ", "",""}, |
||
38 | {"alpha", "", ""}, |
||
39 | {"beta", "", ""}, |
||
40 | {"cap", "",""}, |
||
41 | {"chi", "",""}, |
||
42 | {"cup", "",""}, |
||
43 | {"delta", "",""}, |
||
44 | {"div", "÷", "÷"}, |
||
45 | {"divide","÷","÷"}, |
||
46 | {"epsilon","$(m_varepsilon)","\\varepsilon"}, |
||
47 | {"eta", "",""}, |
||
48 | {"exist", "$(m_exists)","\\exists"}, |
||
49 | {"exists","", ""}, |
||
50 | {"forall","",""}, |
||
51 | {"gamma", "",""}, |
||
52 | {"in", "",""}, |
||
53 | {"inf", "$(m_infty)","\\infty"}, |
||
54 | {"infinity","$(m_infty)","\\infty"}, |
||
55 | {"infty", "",""}, |
||
56 | {"intersect","$(m_cap)", "\\cap"}, |
||
57 | {"intersection","$(m_cap)", "\\cap"}, |
||
58 | {"iota", "",""}, |
||
59 | {"kappa", "",""}, |
||
60 | {"lambda","", ""}, |
||
61 | {"mu", "",""}, |
||
62 | {"nabla", "",""}, |
||
63 | {"neq", "",""}, |
||
64 | {"nu", "",""}, |
||
65 | {"omega", "",""}, |
||
66 | {"pi", "",""}, |
||
67 | {"pm", "",""}, |
||
68 | {"psi", "",""}, |
||
69 | {"rho", "",""}, |
||
70 | {"sigma", "",""}, |
||
71 | {"subset","", ""}, |
||
72 | {"subseteq","",""}, |
||
73 | {"tau", "",""}, |
||
74 | {"theta", "",""}, |
||
75 | {"times", "×", "\\times"}, |
||
76 | {"union", "$(m_cup)", "\\cup"}, |
||
77 | {"varepsilon","",""}, |
||
78 | {"varphi","", ""}, |
||
79 | {"x", "","x"}, |
||
80 | {"xi","",""}, |
||
81 | {"y", "","y"}, |
||
82 | {"z", "","z"}, |
||
83 | {"zeta","",""}, |
||
8185 | bpr | 84 | }; |
85 | int hmname_no=(sizeof(hmname)/sizeof(hmname[0])); |
||
86 | |||
10 | reyssat | 87 | enum {RM_UNKNOWN, RM_FN, RM_VAR, RM_PREFIX}; |
88 | |||
8185 | bpr | 89 | struct mathname mathname[]={ |
5503 | bpr | 90 | {"Arc", RM_PREFIX, "arc"}, |
91 | {"Arg", RM_PREFIX, "arg"}, |
||
92 | {"Ci", RM_FN, ""}, |
||
93 | {"E", RM_VAR, ""}, |
||
94 | {"Euler", RM_VAR, ""}, |
||
95 | {"I", RM_VAR, ""}, |
||
96 | {"Int", RM_FN, ""}, |
||
97 | {"PI", RM_VAR, ""}, |
||
98 | {"Pi", RM_VAR, ""}, |
||
99 | {"Prod", RM_FN, ""}, |
||
100 | {"Si", RM_FN, ""}, |
||
101 | {"Sum", RM_FN, ""}, |
||
102 | {"arc", RM_PREFIX, ""}, |
||
103 | {"arg", RM_PREFIX, ""}, |
||
104 | {"binomial",RM_FN, ""}, |
||
105 | {"diff", RM_FN, ""}, |
||
106 | {"e", RM_VAR, ""}, |
||
107 | {"erf", RM_FN, ""}, |
||
108 | {"euler", RM_VAR, ""}, |
||
109 | {"i", RM_VAR, ""}, |
||
110 | {"infinity",RM_VAR, ""}, |
||
111 | {"int", RM_FN, ""}, |
||
112 | {"integrate",RM_FN, ""}, |
||
113 | {"neq", RM_VAR, ""}, |
||
114 | {"pi", RM_VAR, ""}, |
||
115 | {"prod", RM_FN, ""}, |
||
116 | {"product", RM_FN, ""}, |
||
117 | {"psi", RM_FN, ""}, |
||
118 | {"sum", RM_FN, ""}, |
||
119 | {"theta", RM_FN, ""}, |
||
120 | {"x", RM_VAR, ""}, |
||
121 | {"y", RM_VAR, ""}, |
||
122 | {"z", RM_VAR, ""}, |
||
123 | {"zeta", RM_FN, ""}, |
||
10 | reyssat | 124 | }; |
8185 | bpr | 125 | int mathname_no=(sizeof(mathname)/sizeof(mathname[0])); |
10 | reyssat | 126 | char rm_vbuf[MAX_LINELEN+1],rm_fbuf[MAX_LINELEN+1]; |
127 | char *rm_uservar[MAX_LINELEN+1],*rm_userfn[MAX_LINELEN+1]; |
||
128 | int rm_uservars,rm_userfns; |
||
129 | |||
5503 | bpr | 130 | /* add user-defined variables and function names, |
8155 | bpr | 131 | * internal, only called by rawmath(). |
132 | */ |
||
10 | reyssat | 133 | void getuservar(void) |
134 | { |
||
12246 | bpr | 135 | char *p1, *p2, *p; |
136 | rm_uservars=rm_userfns=0; |
||
137 | p=getvar("wims_rawmath_variables"); |
||
138 | if(p!=NULL && *p!=0) { |
||
139 | ovlstrcpy(rm_vbuf,p); |
||
140 | for(p=rm_vbuf;*p;p++) if(*p==',') *p=' '; |
||
141 | for(p1=find_word_start(rm_vbuf);*p1;p1=find_word_start(p2)) { |
||
142 | rm_uservar[rm_uservars++]=p1; |
||
143 | p2=find_word_end(p1); |
||
144 | if(*p2!=0) *(p2++)=0; |
||
10 | reyssat | 145 | } |
12246 | bpr | 146 | } |
147 | p=getvar("wims_rawmath_functions"); |
||
148 | if(p!=NULL && *p!=0) { |
||
149 | ovlstrcpy(rm_fbuf,p); |
||
150 | for(p=rm_fbuf;*p;p++) if(*p==',') *p=' '; |
||
151 | for(p1=find_word_start(rm_fbuf);*p1;p1=find_word_start(p2)) { |
||
152 | rm_userfn[rm_userfns++]=p1; |
||
153 | p2=find_word_end(p1); |
||
154 | if(*p2!=0) *(p2++)=0; |
||
10 | reyssat | 155 | } |
12246 | bpr | 156 | } |
10 | reyssat | 157 | } |
158 | |||
5503 | bpr | 159 | /* Try to split a word into recognizable variables */ |
10 | reyssat | 160 | char *mathname_split(char *p) |
161 | { |
||
12246 | bpr | 162 | int c,i,j,type; |
10 | reyssat | 163 | |
12246 | bpr | 164 | c=0; |
165 | beg: for(i=get_evalcnt()-1; |
||
166 | i>=0 && strncmp(p,get_evalname(i),strlen(get_evalname(i)))!=0; |
||
167 | i--); |
||
168 | if(i>=0 && get_evaltype(i)>0) { |
||
169 | type=RM_FN; |
||
170 | j=strlen(get_evalname(i)); |
||
171 | gotit: |
||
172 | if(!*(p+j)) return p; |
||
173 | if(myisdigit(*(p+j)) && type!=RM_FN) return NULL; |
||
174 | if(!c) {string_modify(p,p+j,p+j," "); p+=j+1;} |
||
175 | else p+=j; |
||
176 | c++; goto beg; |
||
177 | } |
||
178 | for(i=mathname_no-1; |
||
7673 | bpr | 179 | i>=0 && |
5503 | bpr | 180 | (strncmp(p,mathname[i].name,strlen(mathname[i].name))!=0 |
181 | || mathname[i].style==RM_PREFIX); |
||
12246 | bpr | 182 | i--); |
183 | if(i>=0) { |
||
184 | type=mathname[i].style; |
||
185 | j=strlen(mathname[i].name); |
||
186 | goto gotit; |
||
187 | } |
||
188 | for(i=0;i<rm_uservars && |
||
189 | strncmp(rm_uservar[i],p,strlen(rm_uservar[i]));i++); |
||
190 | if(i<rm_uservars) { |
||
191 | type=RM_VAR; |
||
192 | j=strlen(rm_uservar[i]); goto gotit; |
||
193 | } |
||
194 | for(i=0;i<rm_userfns && |
||
5503 | bpr | 195 | strncmp(p,rm_userfn[i],strlen(rm_userfn[i]));i++); |
12249 | bpr | 196 | if(i<rm_userfns) { |
197 | type=RM_FN; |
||
198 | j=strlen(rm_userfn[i]); goto gotit; |
||
199 | } |
||
12246 | bpr | 200 | return NULL; |
10 | reyssat | 201 | } |
202 | |||
5465 | bpr | 203 | int __replace_badchar (char *p, char *old, char *new) |
12246 | bpr | 204 | { |
205 | int cnt = 0; |
||
5465 | bpr | 206 | char *p1 ; |
207 | while((p1=strstr(p,old))!=NULL) { |
||
5503 | bpr | 208 | string_modify(p,p1,p1+strlen(old),"%s",new); |
7673 | bpr | 209 | cnt++; |
5465 | bpr | 210 | } |
211 | return cnt ; |
||
212 | } |
||
10 | reyssat | 213 | |
5517 | bpr | 214 | /* translate |x| into abs(x)*/ |
5465 | bpr | 215 | int __replace_abs ( char *p ) |
7673 | bpr | 216 | { |
12246 | bpr | 217 | char *p1, *p2 ; |
218 | while((p1=strchr(p,'|'))!=NULL) { |
||
219 | p2=find_matching(p1+1,'|'); |
||
220 | if(p2==NULL) { return 1; break;} /* error; drop it. */ |
||
221 | *p2=')'; string_modify(p,p1,p1+1,"abs("); |
||
222 | } |
||
223 | return 0; |
||
5465 | bpr | 224 | } |
225 | |||
226 | /* signs: translate ++, +-, -+, ... into one sign. */ |
||
5491 | kbelabas | 227 | void __replace_plusminus ( char *p ) |
7673 | bpr | 228 | { |
12246 | bpr | 229 | char *p1, *p2; |
230 | for(p1=p;*p1!=0;p1++) { |
||
231 | int sign, redundant; |
||
232 | if(*p1!='+' && *p1!='-') continue; |
||
233 | if(*p1=='+') sign=1; else sign=-1; |
||
234 | redundant=0; |
||
235 | for(p2=find_word_start(p1+1);*p2=='+' || *p2=='-'; |
||
236 | p2=find_word_start(p2+1)) { |
||
237 | if(*p2=='-') sign*=-1; |
||
238 | redundant=1; |
||
239 | } |
||
240 | if(redundant && *p2!='>' && strncmp(p2,">",4)!=0) { |
||
241 | if(sign==1) *p1='+'; else *p1='-'; |
||
242 | ovlstrcpy(p1+1,p2); |
||
243 | } |
||
244 | } |
||
5465 | bpr | 245 | } |
246 | |||
247 | /* dangling decimal points |
||
8155 | bpr | 248 | * 4. --> 4.0 4.x -> 4.0x |
249 | * .5 -> 0.5 |
||
250 | * another treatment is done in insmath (will replace .. by , ) |
||
251 | */ |
||
5465 | bpr | 252 | void __treat_decimal(char *p) |
12246 | bpr | 253 | { |
254 | char *p1 ; |
||
5465 | bpr | 255 | for(p1=strchr(p,'.'); p1!=NULL; p1=strchr(p1+1,'.')) { |
5503 | bpr | 256 | /* multiple .. is conserved */ |
257 | if(*(p1+1)=='.') { |
||
12246 | bpr | 258 | do p1++; while(*p1=='.'); continue; |
10 | reyssat | 259 | } |
5503 | bpr | 260 | if(p1>p && myisdigit(*(p1-1)) && myisdigit(*(p1+1))) continue; |
261 | /* Non-digit dangling '.' is removed */ |
||
262 | if((p1<=p || !myisdigit(*(p1-1))) && !myisdigit(*(p1+1))) { |
||
12246 | bpr | 263 | ovlstrcpy(p1,p1+1); p1--; continue; |
5503 | bpr | 264 | } |
265 | if(p1==p || !myisdigit(*(p1-1))) { // nondigit.digit |
||
12246 | bpr | 266 | string_modify(p,p1,p1,"0"); p1++; //add zero before point |
5503 | bpr | 267 | } |
268 | if(!myisdigit(*(p1+1))) string_modify(p,p1+1,p1+1,"0"); //add zero after point |
||
269 | } |
||
5465 | bpr | 270 | } |
271 | |||
5517 | bpr | 272 | /* replace new-lines, tabs, " */ |
5465 | bpr | 273 | void __replace_space(char *p) |
7673 | bpr | 274 | { |
12246 | bpr | 275 | char *p1 ; |
276 | for(p1=p;*p1!=0; p1++) { |
||
5517 | bpr | 277 | if(*p1=='\\' || isspace(*p1)) *p1=' ';// replace \ and all spaces by a simple space - |
11736 | bpr | 278 | if(*p1=='\"') { string_modify(p,p1,p1+1,"''"); p1++;} // replace " by '' |
5465 | bpr | 279 | } |
280 | } |
||
281 | |||
8155 | bpr | 282 | /* Error-tolerante raw math translation routine |
283 | * Translate error-laden raw math into machine-understandable form. |
||
284 | * do nothing if there is some { or \\ |
||
285 | */ |
||
5465 | bpr | 286 | void rawmath(char *p) |
287 | { |
||
12246 | bpr | 288 | char *p1, *p2, *p3, *p4; |
289 | char warnbuf[1024]; |
||
290 | int ambiguous=0,unknown=0,flatpower=0,badprec=0,unmatch=0;// for warning |
||
5465 | bpr | 291 | |
8155 | bpr | 292 | /* looks like a TeX source : do nothing */ |
12246 | bpr | 293 | if( (strchr(p,'\\')!=NULL || strchr(p,'{')!=NULL)) return; |
294 | if(strchr(p,'^')==NULL) flatpower=-1; |
||
295 | if(strlen(p)>=MAX_LINELEN) {*p=0; return;} |
||
296 | p1=find_word_start(p);if(*p1==0) return; |
||
297 | while(*p1=='+') p1++; |
||
298 | if(p1>p) ovlstrcpy(p,p1); |
||
299 | (void)__replace_badchar(p,"**", "^"); |
||
300 | (void)__replace_badchar(p,"\xa0", " "); |
||
301 | if (__replace_badchar(p,"²", "^2 ")) flatpower=1; |
||
302 | if (__replace_badchar(p,"³", "^3 ")) flatpower=1; |
||
303 | unmatch=__replace_abs(p); |
||
304 | __replace_plusminus(p) ; |
||
305 | __replace_space(p); |
||
306 | __treat_decimal(p); |
||
307 | if (rawmath_easy) return; |
||
5465 | bpr | 308 | |
8155 | bpr | 309 | /* Principal translation: justapositions to multiplications */ |
12246 | bpr | 310 | if(strstr(p,"^1/")!=NULL) badprec=1; |
311 | getuservar(); |
||
312 | for(p1=p;*p1;p1++) { |
||
313 | if(!isalnum(*p1) && *p1!=')' && *p1!=']') continue; |
||
314 | if(*p1==')' || *p1==']') { |
||
315 | p2=find_word_start(++p1); |
||
316 | add_star: |
||
317 | if(isalnum(*p2) || *p2=='(' || *p2=='[') { |
||
5503 | bpr | 318 | if(*p2=='(' && *p1==')') ambiguous=1; |
319 | if(p2>p1) *p1='*'; |
||
320 | else string_modify(p,p1,p1,"*"); |
||
12246 | bpr | 321 | } |
322 | p1--;continue; |
||
10 | reyssat | 323 | } |
5503 | bpr | 324 | p2=find_mathvar_end(p1); p3=find_word_start(p2); |
325 | if(myisdigit(*p1)) { |
||
12246 | bpr | 326 | p1=p2; p2=p3; goto add_star; |
5503 | bpr | 327 | } |
328 | else { |
||
12246 | bpr | 329 | char buf[MAX_LINELEN+1]; |
330 | int i; |
||
331 | if(p2-p2>30) goto ambig; |
||
332 | memcpy(buf,p1,p2-p1);buf[p2-p1]=0; |
||
333 | i=search_evaltab(buf); |
||
334 | if(i>=0 && get_evaltype(i)>0) { |
||
335 | fnname1: |
||
336 | p1=p2;p2=p3; |
||
8155 | bpr | 337 | /*fnname:*/ |
12246 | bpr | 338 | if(*p2 && *p2!='(' && *p2!='*' && *p2!='/') { |
339 | char hatbuf[MAX_LINELEN+1]; |
||
340 | hatbuf[0]=')'; hatbuf[1]=0; |
||
341 | if(*p2=='^') { |
||
342 | p3=p2+1;while(*p3==' ' || *p3=='+' || *p3=='-') p3++; |
||
343 | if(*p3=='(') { |
||
344 | p3=find_matching(p3+1,')'); |
||
345 | if(p3==NULL) {unmatch=1; p3=p+strlen(p);} |
||
346 | else p3++; |
||
5503 | bpr | 347 | } |
12246 | bpr | 348 | else p3=find_mathvar_end(p3); |
349 | memmove(hatbuf+1,p2,p3-p2);hatbuf[p3-p2+1]=0; |
||
350 | ovlstrcpy(p2,p3); |
||
351 | while(*p2==' ') p2++; |
||
352 | if(*p2=='*' || *p2=='/') { |
||
353 | p1--;continue; |
||
5503 | bpr | 354 | } |
12246 | bpr | 355 | if(*p2=='(') { |
356 | p3=find_matching(p2+1,')'); |
||
357 | if(p3==NULL) {unmatch=1; p3=p+strlen(p);} |
||
358 | else p3++; |
||
359 | string_modify(p,p3,p3,"%s",hatbuf+1); |
||
360 | goto dd2; |
||
5503 | bpr | 361 | } |
362 | } |
||
12246 | bpr | 363 | p3=p2;if(*p3=='+' || *p3=='-') p3++; |
364 | while(isalnum(*p3) || *p3=='*' || *p3=='/' || *p3=='.') |
||
365 | p3++; |
||
366 | for(p4=p2; p4<p3 && !isalnum(*p4); p4++); |
||
367 | if(p4>=p3) { |
||
368 | if(hatbuf[1]) string_modify(p,p2,p2,"%s",hatbuf+1); |
||
369 | } |
||
370 | else { |
||
371 | string_modify(p,p3,p3,"%s",hatbuf); |
||
372 | if(p1==p2) string_modify(p,p2,p2,"("); |
||
373 | else *p1='('; |
||
374 | } |
||
375 | dd2: |
||
376 | ambiguous=1; |
||
5503 | bpr | 377 | } |
12246 | bpr | 378 | p1--;continue; |
379 | } |
||
380 | i=search_list(mathname,mathname_no,sizeof(mathname[0]),buf); |
||
381 | if(i>=0) { |
||
382 | if(mathname[i].replace[0]!=0) { |
||
383 | string_modify(p,p1,p2,mathname[i].replace); |
||
384 | p2=p1+strlen(mathname[i].replace); |
||
385 | p3=find_word_start(p2); |
||
386 | } |
||
387 | switch(mathname[i].style) { |
||
388 | case RM_FN: |
||
389 | goto fnname1; |
||
7673 | bpr | 390 | |
12246 | bpr | 391 | case RM_VAR: |
392 | p1=p2;p2=p3; goto add_star; |
||
7673 | bpr | 393 | |
12246 | bpr | 394 | case RM_PREFIX: |
395 | if(*p3!='c' && *p3!='s' && *p3!='t' && |
||
396 | *p3!='C' && *p3!='S' && *p3!='T') break; |
||
397 | ambiguous=1; |
||
398 | ovlstrcpy(p2,p3); p1--; continue; |
||
5503 | bpr | 399 | } |
12246 | bpr | 400 | } |
401 | i=search_list(hmname,hmname_no,sizeof(hmname[0]),buf); |
||
402 | if(i>=0) { |
||
403 | p1=p2; p2=p3; goto add_star; |
||
404 | } |
||
405 | for(i=0;i<rm_uservars && strcmp(buf,rm_uservar[i]);i++); |
||
406 | if(i<rm_uservars) { |
||
5503 | bpr | 407 | p1=p2;p2=p3;goto add_star; |
12246 | bpr | 408 | } |
409 | for(i=0;i<rm_userfns && strcmp(buf,rm_userfn[i]);i++); |
||
410 | if(i<rm_userfns) goto fnname1; |
||
411 | if(p2-p1>8) goto ambig; |
||
412 | if(mathname_split(buf)!=NULL) { |
||
413 | ambiguous=1; |
||
414 | string_modify(p,p1,p2,"%s",buf); |
||
415 | p1--; continue; |
||
416 | } |
||
8155 | bpr | 417 | /* unknown name */ |
12246 | bpr | 418 | ambig: p1=p2;p2=p3; |
419 | if(strlen(buf)>1) { |
||
420 | for(p3=buf;*p3!=0 && !myisdigit(*p3);p3++); |
||
421 | if(*p3!=0 && flatpower!=0) flatpower=1; |
||
5503 | bpr | 422 | else { |
12246 | bpr | 423 | unknown=1; |
424 | force_setvar("wims_warn_rawmath_parm",buf); |
||
5503 | bpr | 425 | } |
426 | } |
||
12246 | bpr | 427 | else { |
428 | if(*p2=='(') ambiguous=1; |
||
429 | } |
||
430 | if(isalnum(*p2)) { |
||
431 | if(p2>p1) *p1='*'; |
||
432 | else string_modify(p,p1,p1,"*"); |
||
433 | } |
||
434 | p1--;continue; |
||
5503 | bpr | 435 | } |
12246 | bpr | 436 | } |
437 | warnbuf[0]=0; |
||
438 | if(ambiguous) strcat(warnbuf," ambiguous"); |
||
439 | if(unknown) strcat(warnbuf," unknown"); |
||
440 | if(flatpower>0) strcat(warnbuf," flatpower"); |
||
441 | if(badprec>0) strcat(warnbuf," badprec"); |
||
442 | if(unmatch>0) strcat(warnbuf," unmatched_parentheses"); |
||
443 | if(warnbuf[0]) { |
||
5503 | bpr | 444 | char buf[MAX_LINELEN+1],*p; |
445 | p=getvar("wims_warn_rawmath"); |
||
446 | if(p!=NULL && *p!=0) { |
||
12246 | bpr | 447 | snprintf(buf,sizeof(buf),"%s %s",p,warnbuf); |
448 | force_setvar("wims_warn_rawmath",buf); |
||
10 | reyssat | 449 | } |
5503 | bpr | 450 | else force_setvar("wims_warn_rawmath",warnbuf); |
12246 | bpr | 451 | } |
10 | reyssat | 452 | } |
453 | |||
8155 | bpr | 454 | /* replace < and > by html code if htmlmath_gtlt=yes |
455 | * is only used in deduc - not documented |
||
456 | */ |
||
7673 | bpr | 457 | void __replace_htmlmath_gtlt (char *p) |
12246 | bpr | 458 | { |
459 | char *pp; |
||
460 | char *p1=getvar("htmlmath_gtlt"); |
||
461 | if(p1!=NULL && strcmp(p1,"yes")==0) { |
||
462 | for(pp=strchr(p,'<'); pp!=NULL; pp=strchr(pp+1,'<')) |
||
5503 | bpr | 463 | string_modify(p,pp,pp+1,"<"); |
12246 | bpr | 464 | for(pp=strchr(p,'>'); pp!=NULL; pp=strchr(pp+1,'>')) |
5503 | bpr | 465 | string_modify(p,pp,pp+1,">"); |
12246 | bpr | 466 | } |
5465 | bpr | 467 | } |
468 | |||
7673 | bpr | 469 | /* exponents or indices : |
5551 | bpr | 470 | * all digits or + or - following a ^ or _ are considered as in exponent/subscript |
471 | * expression with ( ) following a ^ or _ are considered as in exponent/subscript |
||
472 | * the parenthesis are suppressed except in case of exponent and only digits. |
||
8155 | bpr | 473 | * if int n != 0, use html code, else use tex code |
474 | */ |
||
5551 | bpr | 475 | void __replace_exponent(char *p, int n) |
5465 | bpr | 476 | { |
12246 | bpr | 477 | char *p1; |
478 | char *SUPBEG, *SUPEND; |
||
479 | if (n) { SUPBEG = "<sup>"; SUPEND = "</sup>";} |
||
480 | else { SUPBEG = "^{"; SUPEND = "}";} |
||
5465 | bpr | 481 | |
12246 | bpr | 482 | for(p1=strchr(p,'^');p1!=NULL;p1=strchr(p1+1,'^')) { |
483 | char *p2, *p3, *pp; |
||
484 | char c; |
||
485 | p3 = p2 = find_word_start(p1+1); |
||
486 | if(*p2=='+' || *p2=='-') p2++; |
||
487 | p2 = find_word_start(p2); |
||
8500 | bpr | 488 | /* add '}' to recognized parenthesis in exponent |
7673 | bpr | 489 | * !mathmlmath 2 \cdot x^{3} will now produce correct exponent... |
490 | * !mathmlmath should convert LaTeX input into correct MathML |
||
491 | */ |
||
12246 | bpr | 492 | if(*p2=='(' || *p2 == '{') { /* ^[+-]( */ |
493 | if(*p2 == '('){ p2 = find_matching(p2+1,')');}else { if(*p2 == '{'){ p2 = find_matching(p2+1,'}');}} |
||
5465 | bpr | 494 | /* no matching ')' : p2 = end of line; otherwise just after ')' */ |
12246 | bpr | 495 | if (p2==NULL) p2=p+strlen(p); else p2++; |
5465 | bpr | 496 | /* ^( followed by any number of digits/letters, up to p2 |
497 | * [FIXME: no sign?] */ |
||
498 | /* do we remove parentheses containing exponent group ? */ |
||
12246 | bpr | 499 | if (*p3=='(') for(pp=p3+1;pp<p2-1;pp++) { |
500 | if(!isalnum(*pp)) { |
||
7673 | bpr | 501 | /* not a digit/letter. Remove (). */ |
12246 | bpr | 502 | p3++;*(p2-1)=0;break; |
503 | } |
||
5517 | bpr | 504 | /* x^(2), parentheses not removed */ |
12246 | bpr | 505 | } |
8155 | bpr | 506 | /* p3: start of word after ^ |
507 | * matching parentheses from exponent group. : f^(4) 4-derivative |
||
508 | * don't ignore / remove a leading + sign in exponent group : Cu^+ |
||
509 | */ |
||
12246 | bpr | 510 | } |
511 | else { /* ^[+-] */ |
||
512 | char *ptt=p2; |
||
513 | p2=find_word_start(find_mathvar_end(p2)); |
||
514 | if(*p2=='(' && isalpha(*ptt)) { |
||
515 | /* ^[+-]var( */ |
||
516 | char *p2t; |
||
517 | p2t=find_matching(p2+1,')'); if(p2t!=NULL) p2=p2t+1; |
||
518 | /* FIXME: what if no matching ) ? */ |
||
519 | } |
||
8155 | bpr | 520 | /* ^[+-]var(...): p2 points after closing ')' |
521 | * FIXME: I don't think this 'else' branch is sensible. One |
||
522 | * should NOT accept x^b(c+1) as meaning x^[b(c+1)]. I would |
||
523 | * remove it altogether. |
||
524 | */ |
||
12246 | bpr | 525 | } |
8155 | bpr | 526 | /* p1 points at ^ before exponent group |
527 | * p2 points at end of exponent group |
||
528 | * p3 = exponent group (sometimes stripped, without parentheses) |
||
529 | * |
||
530 | * truncate string p at p2 [ c = char deleted by truncation ] |
||
531 | */ |
||
12246 | bpr | 532 | c = *p2;if(c!=0) *p2++=0; |
5465 | bpr | 533 | /* replace ^<exponent group>. Add back missing character 'c' */ |
12246 | bpr | 534 | string_modify(p,p1,p2, "%s%s%s%c",SUPBEG,p3,SUPEND,c); |
535 | } |
||
5465 | bpr | 536 | } |
537 | |||
5551 | bpr | 538 | /* if int n != 0, use html code, else use tex code */ |
539 | void __replace_subscript(char *p, int n) |
||
5465 | bpr | 540 | { |
541 | char *p1, *p2; |
||
5503 | bpr | 542 | char *SUBBEG, *SUBEND; |
7673 | bpr | 543 | if (n) {SUBBEG = "<sub>"; SUBEND = "</sub>";} |
5503 | bpr | 544 | else {SUBBEG = "_{"; SUBEND = "}";} |
5465 | bpr | 545 | for(p1=strchr(p,'_');p1!=NULL;p1=strchr(p1+1,'_')) { |
5503 | bpr | 546 | char buff[256]; |
547 | p2=p1+1; |
||
548 | if(*p2=='(') p2=find_matching(p2+1,')'); |
||
549 | else p2=find_mathvar_end(p2); |
||
550 | if(p2==NULL || p2>p1+64) continue; |
||
551 | if(*(p1+1)=='(') p2++; |
||
552 | memmove(buff,p1+1,p2-p1-1); buff[p2-p1-1]=0; |
||
553 | strip_enclosing_par(buff); |
||
554 | string_modify(p,p1,p2,"%s%s%s",SUBBEG,buff,SUBEND);} |
||
5465 | bpr | 555 | } |
556 | |||
5517 | bpr | 557 | /* get rid of 1*.. ..*1 exemple : 1 * x, x/1 */ |
5465 | bpr | 558 | void __replace_getrid1 (char *p) |
12246 | bpr | 559 | { |
560 | char *p1, *p2, *p3 ; |
||
561 | for(p1=p;*p1;p1++) { |
||
562 | if(*p1!='1') continue; |
||
563 | if(myisdigit(*(p1+1)) || *(p1+1)=='.' || |
||
564 | (p1>p && (isalnum(*(p1-1)) || *(p1-1)=='.')) ) continue; |
||
565 | p2=find_word_start(p1+1); |
||
566 | if(p1>p+2 && (*(p1-1)=='-' || *(p1-1)=='+')) { |
||
567 | for(p3=p1-2; p3>p && isspace(*p3); p3--); |
||
568 | if(p3>p+1 && (*p3=='E' || *p3=='e')) { /* ??? */ |
||
5503 | bpr | 569 | p3--; while(p3>p && isspace(*p3)) p3--; |
570 | if(myisdigit(*p3) || *p3=='.') continue; |
||
571 | } |
||
12246 | bpr | 572 | } |
573 | if(p1==p) p3="+"; |
||
574 | else for(p3=p1-1;p3>p && isspace(*p3);p3--); |
||
575 | if(*p2=='*' && *p3!='/') {/* delete 1 if 1* or /1 */ |
||
576 | ovlstrcpy(p1,p2+1);continue; |
||
577 | } |
||
578 | if(isalpha(*p2) && *p3!='/') { |
||
579 | ovlstrcpy(p1,p2);continue; |
||
580 | } |
||
581 | if(*p3=='/' && *p2!='<') ovlstrcpy(p3,p2); |
||
10 | reyssat | 582 | |
12246 | bpr | 583 | } |
5465 | bpr | 584 | } |
585 | |||
586 | /* get rid of '*' */ |
||
587 | void __replace_getridstar (char *p) |
||
12246 | bpr | 588 | { |
589 | char *p1 ; |
||
590 | for(p1=strchr(p,'*');p1!=NULL;p1=strchr(p1+1,'*')) { |
||
5503 | bpr | 591 | char *pq; |
592 | pq=find_word_start(p1+1); |
||
593 | if(myisdigit(*pq)) { |
||
12246 | bpr | 594 | string_modify(p,p1,pq,"×"); |
595 | continue; |
||
10 | reyssat | 596 | } |
5503 | bpr | 597 | if(p1>p && (isalpha(*(p1-1)) || *(p1-1)==')' || *(p1-1)=='>') |
598 | && (isalnum(*pq) || *pq=='$')) *p1=' '; |
||
599 | else { |
||
600 | ovlstrcpy(p1,p1+1);p1--; |
||
601 | } |
||
602 | } |
||
5465 | bpr | 603 | } |
604 | |||
7673 | bpr | 605 | /* <=, >=, ->, =>, <=> |
8155 | bpr | 606 | * if int n != 0, use html code, else use tex code |
607 | */ |
||
5551 | bpr | 608 | |
609 | void __replace_arrow ( char *p, int n) |
||
12246 | bpr | 610 | { |
611 | char *p1, *p2, *m_prefix; |
||
612 | if (n) m_prefix="$m_"; else m_prefix="\\"; |
||
7673 | bpr | 613 | |
12246 | bpr | 614 | for(p1=strstr(p,"<="); p1!=NULL; p1=strstr(p1+1,"<=")) { |
615 | if(*(p1+5)!='&' && *(p1+5)!='=') |
||
616 | string_modify(p,p1,p1+5, "%sle",m_prefix); |
||
617 | else { |
||
618 | for(p2=p1+5; *p2=='='; p2++); |
||
619 | if(strncmp(p2,">",4)==0) { |
||
620 | if(p2>p1+5) { string_modify(p,p1,p2+4,"%sLongleftrightarrow",m_prefix);} |
||
621 | else { string_modify(p,p1,p2+4,"%sLeftrightarrow",m_prefix);} |
||
5503 | bpr | 622 | } |
12246 | bpr | 623 | else { string_modify(p,p1,p2,"%sLongleftarrow",m_prefix);} |
10 | reyssat | 624 | } |
12246 | bpr | 625 | } |
626 | for(p1=strstr(p,">="); p1!=NULL; p1=strstr(p1+1,">=")) { |
||
627 | if(*(p1+5)!='=') { string_modify(p,p1,p1+5,"%sge",m_prefix);} |
||
628 | } |
||
629 | for(p1=strstr(p,"->"); p1; p1=strstr(p1+1,"->")) { |
||
630 | for(p2=p1; p2>p && *(p2-1)=='-'; p2--); |
||
631 | if(p2>p && *(p2-1)==';') continue; |
||
632 | if(p2<p1) { string_modify(p,p2,p1+5,"%slongrightarrow",m_prefix);} |
||
633 | else { string_modify(p,p1,p1+5,"%srightarrow",m_prefix);} |
||
634 | } |
||
635 | for(p1=strstr(p,"=>"); p1; p1=strstr(p1+1,"=>")) { |
||
636 | for(p2=p1; p2>p && *(p2-1)=='='; p2--); |
||
637 | if(p2>p && *(p2-1)==';') continue; |
||
638 | if(p2<p1) { string_modify(p,p2,p1+5,"%sLongrightarrow",m_prefix);} |
||
639 | else { string_modify(p,p1,p1+5,"%sRightarrow",m_prefix) ;} |
||
640 | } |
||
5503 | bpr | 641 | /* Not equal */ |
12246 | bpr | 642 | for(p1=strstr(p,"!="); p1; p1=strstr(p1+1,"!=")) { |
643 | if(p1>p && !isspace(*(p1-1))) continue; |
||
644 | string_modify(p,p1,p1+2,"%sneq",m_prefix); |
||
645 | } |
||
5465 | bpr | 646 | } |
647 | |||
5503 | bpr | 648 | /* why <tt> is used sometimes ? replace single characters by italics one |
649 | * is it useful in mathml ? |
||
8155 | bpr | 650 | */ |
5551 | bpr | 651 | void __replace_italics (char *p, int n) |
12246 | bpr | 652 | { |
653 | char *p1, *p2, *p3, pbuf[16]; |
||
5503 | bpr | 654 | char *ITBEG, *ITEND, *ITtBEG, *ITtEND; |
5551 | bpr | 655 | if (n) { |
5503 | bpr | 656 | ITBEG = "<i>";ITtBEG = "<i><tt>"; |
657 | ITEND = "</i>";ITtEND = "</tt></i>"; |
||
658 | } else {; |
||
659 | ITBEG = "" ; ITtBEG = ""; |
||
660 | ITEND = ""; ITtEND = ""; |
||
661 | } |
||
7673 | bpr | 662 | |
5503 | bpr | 663 | for(p1=p;*p1;p1++) { |
664 | if(*p1=='<') { |
||
12246 | bpr | 665 | p1=strchr(p1,'>'); |
666 | if(p1==NULL) break; //recognize an html tag < > |
||
667 | else continue; |
||
10 | reyssat | 668 | } |
5503 | bpr | 669 | if(*p1=='=' && *(p1+1)=='-') { |
670 | string_modify(p,p1+1,p1+1," "); p1+=2; continue; |
||
671 | } |
||
672 | if(*p1=='\'') { |
||
12246 | bpr | 673 | for(p2=p1+1;*p2=='\'';p2++); |
674 | memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0; |
||
675 | string_modify(p,p1,p2,"%s%s%s",ITtBEG,pbuf,ITtEND); |
||
676 | p1=p2+strlen(ITtBEG)+strlen(ITtEND)-1; |
||
677 | continue; |
||
5503 | bpr | 678 | } |
679 | if(!isalpha(*p1)) continue; |
||
680 | /* unique letter.*/ |
||
681 | for(p2=p1+1;isalpha(*p2);p2++); |
||
682 | p3=find_word_start(p2); |
||
683 | if(p2>p1+5 || |
||
684 | (p2>p1+1 && (*p3==';' || *p3=='(' || myisdigit(*p2)))) |
||
12246 | bpr | 685 | {p1=p2-1; continue;} |
5503 | bpr | 686 | if(strncasecmp(p3,"</i>",strlen("</i>"))==0) continue; |
687 | memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0; |
||
688 | string_modify(p,p1,p2,"%s%s%s",ITBEG,pbuf,ITEND); |
||
689 | p1=p2+strlen(ITBEG)+strlen(ITEND)-1; |
||
12246 | bpr | 690 | } |
10 | reyssat | 691 | } |
5466 | bpr | 692 | |
5551 | bpr | 693 | /* float (1.2 E-03) : 3E+021 -> 3 × 10^{21} - 3E-21 -> 3 × 10^{-21} |
694 | * or replace variable name (alpha) |
||
8155 | bpr | 695 | * if int n != 0, use html code, else use tex code |
696 | */ |
||
5551 | bpr | 697 | void __replace_mathvar(char *p,int n) |
12246 | bpr | 698 | { |
699 | char *p1, *p2, *p3; |
||
5503 | bpr | 700 | char *EXPBEG, *EXPEND, *EXPBEGMINUS, *SUBBEG, *SUBEND, *m_prefix; |
7673 | bpr | 701 | |
5551 | bpr | 702 | if ( n ) { |
12246 | bpr | 703 | EXPBEG = " × 10<sup>"; |
704 | EXPBEGMINUS = " × 10<sup>-"; |
||
705 | EXPEND = "</sup>"; |
||
706 | SUBBEG = "<sub>"; |
||
707 | SUBEND = "</sub>"; |
||
708 | m_prefix="$m_"; |
||
5503 | bpr | 709 | } else { |
12246 | bpr | 710 | EXPBEG = " \\times 10^{" ; |
711 | EXPBEGMINUS = " \\times 10^{-" ; |
||
712 | EXPEND = "}"; |
||
713 | SUBBEG = "_{"; |
||
714 | SUBEND = "}"; |
||
715 | m_prefix="\\"; |
||
5466 | bpr | 716 | } |
5503 | bpr | 717 | for (p1=find_mathvar_start(p);*p1!=0;p1=find_mathvar_start(p2)) { |
718 | char buf[MAX_LINELEN+1]; |
||
8155 | bpr | 719 | /* if the variable is preceded by \ do nothing |
720 | * in fact this should not arrive |
||
721 | */ |
||
7673 | bpr | 722 | if (p1>p && *(p1-1) == '\\' ) break ; |
5503 | bpr | 723 | p2 = find_mathvar_end(p1); |
12246 | bpr | 724 | if (p1 == p2) break; |
5503 | bpr | 725 | memmove(buf,p1,p2-p1);buf[p2-p1]=0; |
5466 | bpr | 726 | |
5503 | bpr | 727 | if(myisdigit(buf[0])) { |
8155 | bpr | 728 | /* number : 3E+021 -> 3 x 10^{21} - 3E-21 -> 3 x 10^{-21} */ |
12246 | bpr | 729 | int k = 1, minus = 0; |
5503 | bpr | 730 | |
731 | /* see putnumber in texmath.c*/ |
||
12246 | bpr | 732 | if( (p3 = strpbrk(buf, "Ee")) == NULL) continue; |
733 | p1 += p3-buf; |
||
5503 | bpr | 734 | //count the number of 0, +, - |
12246 | bpr | 735 | if (*(p1+k) == '-') { minus = 1; k++; } |
736 | while(*(p1+k)=='0' || *(p1+k)=='+') k++; |
||
5503 | bpr | 737 | |
12246 | bpr | 738 | string_modify(p,p1,p1+k, minus? EXPBEGMINUS: EXPBEG); |
739 | p2 += strlen(minus? EXPBEGMINUS: EXPBEG)-k; |
||
740 | string_modify(p,p2,p2, EXPEND); |
||
741 | p2 += strlen(EXPEND); |
||
742 | } |
||
743 | else { |
||
8155 | bpr | 744 | /* alphabetic name, replace greek letters and symbols in hmname |
745 | * not done in texmath.c |
||
746 | */ |
||
12246 | bpr | 747 | int i = search_list(hmname,hmname_no,sizeof(hmname[0]),buf); |
748 | char *n, *r; |
||
749 | if(i<0) { /* not in list */ |
||
750 | int k = strlen(buf)-1; |
||
751 | if(myisdigit(buf[k])) { |
||
752 | while(k>0 && myisdigit(buf[k-1])) k--; |
||
753 | string_modify(buf,buf+k,buf+k,SUBBEG); |
||
754 | string_modify(p,p1,p2,"%s%s",buf,SUBEND); |
||
755 | p2+=strlen(SUBBEG)+strlen(SUBEND); |
||
5503 | bpr | 756 | } |
12246 | bpr | 757 | continue; |
758 | } |
||
759 | n = hmname[i].name; |
||
760 | r = mathalign_base < 2? hmname[i].replace: hmname[i].replacem; |
||
761 | if(r[0]==0) { /* replace = "" */ |
||
762 | string_modify(p,p1,p2,"%s%s",m_prefix, n); |
||
763 | p2 = p1+strlen(n)+strlen(m_prefix); |
||
764 | } else { |
||
765 | string_modify(p,p1,p2,"%s",r); |
||
766 | p2 = p1+strlen(r); |
||
767 | } |
||
768 | } |
||
5503 | bpr | 769 | } |
5465 | bpr | 770 | } |
771 | |||
7673 | bpr | 772 | /* translate raw math expression coming from calculators into best html way |
8155 | bpr | 773 | * if int n != 0, use html code, else use tex code |
774 | */ |
||
5551 | bpr | 775 | void __htmlmath(char *p,int n) |
5465 | bpr | 776 | { |
12246 | bpr | 777 | if(!rawmath_easy) { rawmath_easy=1; rawmath(p); rawmath_easy=0;} |
778 | __replace_htmlmath_gtlt(p); //only used in deductio |
||
779 | __replace_exponent(p,n); |
||
780 | __replace_subscript(p,n); |
||
781 | __replace_getrid1(p); |
||
782 | __replace_mathvar(p,n); |
||
783 | __replace_getridstar(p); |
||
784 | __replace_arrow(p,n); |
||
5503 | bpr | 785 | /* Now make substitutions */ |
12246 | bpr | 786 | substit(p); |
5503 | bpr | 787 | /* Make single names italic - done automatically by mathml */ |
5551 | bpr | 788 | __replace_italics(p,n); |
5465 | bpr | 789 | strip_trailing_spaces(p); |
790 | } |
||
791 | |||
7673 | bpr | 792 | void htmlmath(char *p) |
5465 | bpr | 793 | { |
5551 | bpr | 794 | __htmlmath(p,1) ; |
5544 | bpr | 795 | } |
796 | |||
797 | /* if mathml is closed, it will be just htmlmath*/ |
||
798 | |||
7673 | bpr | 799 | void mathmlmath(char *p) |
800 | { |
||
8500 | bpr | 801 | /* |
802 | if force_mathml variable is set to "yes", do not (never) use 'htmlmath' |
||
803 | in output, so command: !mathmlmath some_LaTeX_string will produce mathml |
||
804 | (mathml \input inputfields may be included) |
||
8499 | schaersvoo | 805 | */ |
12246 | bpr | 806 | if( strcmp( getvar("force_mathml"),"yes") == 0 ){ mathalign_base = 2;mathml(p,1);return; } |
807 | if (mathalign_base == 2) { __htmlmath(p,0) ; mathml(p,1);} else { __htmlmath(p,1) ;} |
||
5465 | bpr | 808 | } |