Subversion Repositories wimsdev

Rev

Rev 11125 | Rev 12229 | 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[]={
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",  "",""},
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
{
135
    char *p1, *p2, *p;
136
    rm_uservars=rm_userfns=0;
137
    p=getvar("wims_rawmath_variables");
138
    if(p!=NULL && *p!=0) {
5503 bpr 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;
145
       }
10 reyssat 146
    }
147
    p=getvar("wims_rawmath_functions");
148
    if(p!=NULL && *p!=0) {
5503 bpr 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;
155
      }
10 reyssat 156
    }
157
}
158
 
5503 bpr 159
/* Try to split a word into recognizable variables */
10 reyssat 160
char *mathname_split(char *p)
161
{
162
    int c,i,j,type;
163
 
164
    c=0;
165
    beg: for(i=get_evalcnt()-1;
5503 bpr 166
    i>=0 && strncmp(p,get_evalname(i),strlen(get_evalname(i)))!=0;
167
    i--);
10 reyssat 168
    if(i>=0 && get_evaltype(i)>0) {
5503 bpr 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;
10 reyssat 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);
182
      i--);
10 reyssat 183
    if(i>=0) {
5503 bpr 184
      type=mathname[i].style;
185
      j=strlen(mathname[i].name);
186
      goto gotit;
10 reyssat 187
    }
7673 bpr 188
    for(i=0;i<rm_uservars &&
5503 bpr 189
      strncmp(rm_uservar[i],p,strlen(rm_uservar[i]));i++);
10 reyssat 190
    if(i<rm_uservars) {
5503 bpr 191
      type=RM_VAR;
192
      j=strlen(rm_uservar[i]); goto gotit;
10 reyssat 193
    }
7673 bpr 194
    for(i=0;i<rm_userfns &&
5503 bpr 195
      strncmp(p,rm_userfn[i],strlen(rm_userfn[i]));i++);
10 reyssat 196
    if(i<rm_userfns) {
5503 bpr 197
      type=RM_FN;
198
      j=strlen(rm_userfn[i]); goto gotit;
10 reyssat 199
    }
7673 bpr 200
    return NULL;
10 reyssat 201
}
202
 
5465 bpr 203
int __replace_badchar (char *p, char *old, char *new)
204
{ int cnt = 0;
205
  char *p1 ;
206
  while((p1=strstr(p,old))!=NULL) {
5503 bpr 207
    string_modify(p,p1,p1+strlen(old),"%s",new);
7673 bpr 208
    cnt++;
5465 bpr 209
  }
210
  return cnt ;
211
}
10 reyssat 212
 
5517 bpr 213
/* translate |x| into abs(x)*/
5465 bpr 214
int __replace_abs ( char *p )
7673 bpr 215
{
5465 bpr 216
    char *p1, *p2 ;
10 reyssat 217
    while((p1=strchr(p,'|'))!=NULL) {
5503 bpr 218
      p2=find_matching(p1+1,'|');
219
      if(p2==NULL) { return 1; break;} /* error; drop it. */
220
      *p2=')'; string_modify(p,p1,p1+1,"abs(");
10 reyssat 221
    }
5465 bpr 222
    return 0;
223
}
224
 
225
/* signs: translate ++, +-, -+, ... into one sign. */
5491 kbelabas 226
void __replace_plusminus ( char *p )
7673 bpr 227
{
5465 bpr 228
   char *p1, *p2;
229
   for(p1=p;*p1!=0;p1++) {
5503 bpr 230
     int sign, redundant;
231
     if(*p1!='+' && *p1!='-') continue;
232
     if(*p1=='+') sign=1; else sign=-1;
233
     redundant=0;
234
     for(p2=find_word_start(p1+1);*p2=='+' || *p2=='-';
235
        p2=find_word_start(p2+1)) {
236
         if(*p2=='-') sign*=-1;
237
         redundant=1;
238
     }
239
     if(redundant && *p2!='>' && strncmp(p2,"&gt;",4)!=0) {
240
        if(sign==1) *p1='+'; else *p1='-';
241
        ovlstrcpy(p1+1,p2);
242
     }
5465 bpr 243
   }
244
}
245
 
246
/* dangling decimal points
8155 bpr 247
 * 4. --> 4.0  4.x -> 4.0x
248
 * .5 -> 0.5
249
 * another treatment is done in insmath (will replace .. by , )
250
 */
5465 bpr 251
void __treat_decimal(char *p)
252
{ char *p1 ;
253
  for(p1=strchr(p,'.'); p1!=NULL; p1=strchr(p1+1,'.')) {
5503 bpr 254
        /* multiple .. is conserved */
255
    if(*(p1+1)=='.') {
256
        do p1++; while(*p1=='.'); continue;
10 reyssat 257
    }
5503 bpr 258
    if(p1>p && myisdigit(*(p1-1)) && myisdigit(*(p1+1))) continue;
259
        /* Non-digit dangling '.' is removed */
260
    if((p1<=p || !myisdigit(*(p1-1))) && !myisdigit(*(p1+1))) {
261
        ovlstrcpy(p1,p1+1); p1--; continue;
262
    }
263
    if(p1==p || !myisdigit(*(p1-1))) { // nondigit.digit
264
        string_modify(p,p1,p1,"0"); p1++; //add zero before point
265
    }
266
    if(!myisdigit(*(p1+1))) string_modify(p,p1+1,p1+1,"0"); //add zero after point
267
  }
5465 bpr 268
}
269
 
5517 bpr 270
/* replace new-lines, tabs, " */
5465 bpr 271
void __replace_space(char *p)
7673 bpr 272
{
5465 bpr 273
 char *p1 ;
5503 bpr 274
 for(p1=p;*p1!=0; p1++) {
5517 bpr 275
    if(*p1=='\\' || isspace(*p1)) *p1=' ';// replace \ and all spaces by a simple space -
11736 bpr 276
    if(*p1=='\"') { string_modify(p,p1,p1+1,"''"); p1++;} // replace " by ''
5465 bpr 277
  }
278
}
279
 
8155 bpr 280
/* Error-tolerante raw math translation routine
281
 * Translate error-laden raw math into machine-understandable form.
282
 * do nothing if there is some { or \\
283
 */
5465 bpr 284
void rawmath(char *p)
285
{
286
    char *p1, *p2, *p3, *p4;
287
    char warnbuf[1024];
288
    int ambiguous=0,unknown=0,flatpower=0,badprec=0,unmatch=0;// for warning
289
 
8155 bpr 290
/* looks like a TeX source : do nothing */
5517 bpr 291
    if( (strchr(p,'\\')!=NULL || strchr(p,'{')!=NULL)) return;
5465 bpr 292
    if(strchr(p,'^')==NULL) flatpower=-1;
293
    if(strlen(p)>=MAX_LINELEN) {*p=0; return;}
294
    p1=find_word_start(p);if(*p1==0) return;
295
    while(*p1=='+') p1++;
296
    if(p1>p) ovlstrcpy(p,p1);
5491 kbelabas 297
    (void)__replace_badchar(p,"**", "^");
8649 bpr 298
    (void)__replace_badchar(p,"\xa0", " ");
5491 kbelabas 299
    if (__replace_badchar(p,"²", "^2 ")) flatpower=1;
300
    if (__replace_badchar(p,"³", "^3 ")) flatpower=1;
5517 bpr 301
    unmatch=__replace_abs(p);
7673 bpr 302
    __replace_plusminus(p) ;
5465 bpr 303
    __replace_space(p);
304
    __treat_decimal(p);
5491 kbelabas 305
    if (rawmath_easy) return;
5465 bpr 306
 
8155 bpr 307
/* Principal translation: justapositions to multiplications */
10 reyssat 308
    if(strstr(p,"^1/")!=NULL) badprec=1;
309
    getuservar();
310
    for(p1=p;*p1;p1++) {
5503 bpr 311
      if(!isalnum(*p1) && *p1!=')' && *p1!=']') continue;
312
      if(*p1==')' || *p1==']') {
313
        p2=find_word_start(++p1);
314
        add_star:
315
        if(isalnum(*p2) || *p2=='(' || *p2=='[') {
316
        if(*p2=='(' && *p1==')') ambiguous=1;
317
        if(p2>p1) *p1='*';
318
        else string_modify(p,p1,p1,"*");
319
        }
320
        p1--;continue;
10 reyssat 321
    }
5503 bpr 322
    p2=find_mathvar_end(p1); p3=find_word_start(p2);
323
    if(myisdigit(*p1)) {
324
        p1=p2; p2=p3; goto add_star;
325
    }
326
    else {
327
        char buf[MAX_LINELEN+1];
328
        int i;
329
        if(p2-p2>30) goto ambig;
330
        memcpy(buf,p1,p2-p1);buf[p2-p1]=0;
331
        i=search_evaltab(buf);
332
        if(i>=0 && get_evaltype(i)>0) {
333
          fnname1:
334
          p1=p2;p2=p3;
8155 bpr 335
/*fnname:*/
5503 bpr 336
          if(*p2 && *p2!='(' && *p2!='*' && *p2!='/') {
337
            char hatbuf[MAX_LINELEN+1];
338
            hatbuf[0]=')'; hatbuf[1]=0;
339
            if(*p2=='^') {
340
              p3=p2+1;while(*p3==' ' || *p3=='+' || *p3=='-') p3++;
341
              if(*p3=='(') {
342
                p3=find_matching(p3+1,')');
343
                if(p3==NULL) {unmatch=1; p3=p+strlen(p);}
344
                else p3++;
345
              }
346
              else p3=find_mathvar_end(p3);
347
              memmove(hatbuf+1,p2,p3-p2);hatbuf[p3-p2+1]=0;
348
              ovlstrcpy(p2,p3);
349
              while(*p2==' ') p2++;
350
              if(*p2=='*' || *p2=='/') {
351
                p1--;continue;
352
              }
353
              if(*p2=='(') {
354
                p3=find_matching(p2+1,')');
355
                if(p3==NULL) {unmatch=1; p3=p+strlen(p);}
356
                else p3++;
357
                string_modify(p,p3,p3,"%s",hatbuf+1);
358
                goto dd2;
359
              }
360
            }
361
            p3=p2;if(*p3=='+' || *p3=='-') p3++;
362
            while(isalnum(*p3) || *p3=='*' || *p3=='/' || *p3=='.')
363
              p3++;
364
            for(p4=p2; p4<p3 && !isalnum(*p4); p4++);
365
            if(p4>=p3) {
366
              if(hatbuf[1]) string_modify(p,p2,p2,"%s",hatbuf+1);
367
            }
368
            else {
369
              string_modify(p,p3,p3,"%s",hatbuf);
370
              if(p1==p2) string_modify(p,p2,p2,"(");
371
              else *p1='(';
372
            }
373
            dd2:
374
            ambiguous=1;
375
          }
376
          p1--;continue;
377
        }
378
        i=search_list(mathname,mathname_no,sizeof(mathname[0]),buf);
379
        if(i>=0) {
380
          if(mathname[i].replace[0]!=0) {
381
            string_modify(p,p1,p2,mathname[i].replace);
382
            p2=p1+strlen(mathname[i].replace);
383
            p3=find_word_start(p2);
384
          }
385
          switch(mathname[i].style) {
386
            case RM_FN:
387
              goto fnname1;
7673 bpr 388
 
5503 bpr 389
            case RM_VAR:
390
              p1=p2;p2=p3; goto add_star;
7673 bpr 391
 
5503 bpr 392
            case RM_PREFIX:
7673 bpr 393
              if(*p3!='c' && *p3!='s' && *p3!='t' &&
5503 bpr 394
             *p3!='C' && *p3!='S' && *p3!='T') break;
395
              ambiguous=1;
396
              ovlstrcpy(p2,p3); p1--; continue;
397
          }
398
        }
399
        i=search_list(hmname,hmname_no,sizeof(hmname[0]),buf);
400
        if(i>=0) {
401
          p1=p2; p2=p3; goto add_star;
402
        }
403
        for(i=0;i<rm_uservars && strcmp(buf,rm_uservar[i]);i++);
404
        if(i<rm_uservars) {
405
          p1=p2;p2=p3;goto add_star;
406
        }
407
        for(i=0;i<rm_userfns && strcmp(buf,rm_userfn[i]);i++);
408
        if(i<rm_userfns) goto fnname1;
409
        if(p2-p1>8) goto ambig;
410
        if(mathname_split(buf)!=NULL) {
411
          ambiguous=1;
412
          string_modify(p,p1,p2,"%s",buf);
413
          p1--; continue;
414
        }
8155 bpr 415
/* unknown name */
5503 bpr 416
        ambig: p1=p2;p2=p3;
417
        if(strlen(buf)>1) {
418
          for(p3=buf;*p3!=0 && !myisdigit(*p3);p3++);
7673 bpr 419
          if(*p3!=0 && flatpower!=0) flatpower=1;
5503 bpr 420
          else {
421
            unknown=1;
422
            force_setvar("wims_warn_rawmath_parm",buf);
423
          }
424
        }
425
        else {
426
          if(*p2=='(') ambiguous=1;
427
        }
428
        if(isalnum(*p2)) {
429
          if(p2>p1) *p1='*';
430
          else string_modify(p,p1,p1,"*");
431
        }
432
        p1--;continue;
433
      }
434
    }
10 reyssat 435
    warnbuf[0]=0;
436
    if(ambiguous) strcat(warnbuf," ambiguous");
437
    if(unknown) strcat(warnbuf," unknown");
438
    if(flatpower>0) strcat(warnbuf," flatpower");
439
    if(badprec>0) strcat(warnbuf," badprec");
440
    if(unmatch>0) strcat(warnbuf," unmatched_parentheses");
441
    if(warnbuf[0]) {
5503 bpr 442
    char buf[MAX_LINELEN+1],*p;
443
    p=getvar("wims_warn_rawmath");
444
    if(p!=NULL && *p!=0) {
445
        snprintf(buf,sizeof(buf),"%s %s",p,warnbuf);
446
        force_setvar("wims_warn_rawmath",buf);
10 reyssat 447
    }
5503 bpr 448
    else force_setvar("wims_warn_rawmath",warnbuf);
449
    }
10 reyssat 450
}
451
 
8155 bpr 452
 /* replace < and > by html code if htmlmath_gtlt=yes
453
  * is only used in deduc - not documented
454
  */
7673 bpr 455
void __replace_htmlmath_gtlt (char *p)
5465 bpr 456
{   char *pp;
7673 bpr 457
    char *p1=getvar("htmlmath_gtlt");
5503 bpr 458
    if(p1!=NULL && strcmp(p1,"yes")==0) {
459
      for(pp=strchr(p,'<'); pp!=NULL; pp=strchr(pp+1,'<'))
460
      string_modify(p,pp,pp+1,"&lt;");
461
      for(pp=strchr(p,'>'); pp!=NULL; pp=strchr(pp+1,'>'))
462
      string_modify(p,pp,pp+1,"&gt;");
5465 bpr 463
   }
464
}
465
 
7673 bpr 466
/* exponents or indices :
5551 bpr 467
 *  all digits or + or - following a ^ or _ are considered as in exponent/subscript
468
 *  expression with ( ) following a ^ or _ are  considered as in exponent/subscript
469
 *  the parenthesis are suppressed except in case of exponent and only digits.
8155 bpr 470
 *  if int n != 0, use html code, else use tex code
471
 */
5551 bpr 472
void __replace_exponent(char *p, int n)
5465 bpr 473
{
5503 bpr 474
   char *p1;
475
   char *SUPBEG, *SUPEND;
7673 bpr 476
   if (n) { SUPBEG = "<sup>"; SUPEND = "</sup>";}
5503 bpr 477
   else { SUPBEG = "^{"; SUPEND = "}";}
5465 bpr 478
 
10 reyssat 479
    for(p1=strchr(p,'^');p1!=NULL;p1=strchr(p1+1,'^')) {
5465 bpr 480
        char *p2, *p3, *pp;
481
        char c;
482
        p3 = p2 = find_word_start(p1+1);
483
        if(*p2=='+' || *p2=='-') p2++;
484
        p2 = find_word_start(p2);
8500 bpr 485
/* add '}' to recognized parenthesis in exponent
7673 bpr 486
 * !mathmlmath 2 \cdot x^{3} will now produce correct exponent...
487
 * !mathmlmath should convert LaTeX input into correct MathML
488
 */
7415 schaersvoo 489
        if(*p2=='(' || *p2 == '{') { /* ^[+-]( */
7673 bpr 490
             if(*p2 == '('){ p2 = find_matching(p2+1,')');}else { if(*p2 == '{'){ p2 = find_matching(p2+1,'}');}}
5465 bpr 491
            /* no matching ')' : p2 = end of line; otherwise just after ')' */
492
            if (p2==NULL) p2=p+strlen(p); else p2++;
493
            /* ^( followed by any number of digits/letters, up to p2
494
             * [FIXME: no sign?] */
495
            /* do we remove parentheses containing exponent group ? */
496
            if (*p3=='(') for(pp=p3+1;pp<p2-1;pp++) {
497
                if(!isalnum(*pp)) {
7673 bpr 498
                    /* not a digit/letter. Remove (). */
5465 bpr 499
                    p3++;*(p2-1)=0;break;
500
                }
5517 bpr 501
                /* x^(2), parentheses not removed */
5465 bpr 502
            }
8155 bpr 503
/* p3: start of word after ^
504
 * matching parentheses from exponent group. : f^(4) 4-derivative
505
 * don't  ignore / remove a leading + sign in exponent group : Cu^+
506
 */
5465 bpr 507
        } else { /* ^[+-] */
508
            char *ptt=p2;
509
            p2=find_word_start(find_mathvar_end(p2));
510
            if(*p2=='(' && isalpha(*ptt)) {
511
                /* ^[+-]var( */
512
                char *p2t;
513
                p2t=find_matching(p2+1,')'); if(p2t!=NULL) p2=p2t+1;
514
                /* FIXME: what if no matching ) ? */
515
            }
8155 bpr 516
/* ^[+-]var(...): p2 points after closing ')'
517
 * FIXME: I don't think this 'else' branch is sensible. One
518
 * should NOT accept x^b(c+1) as meaning x^[b(c+1)]. I would
519
 * remove it altogether.
520
 */
5465 bpr 521
        }
8155 bpr 522
/* p1 points at ^ before exponent group
523
 * p2 points at end of exponent group
524
 * p3 = exponent group (sometimes stripped, without parentheses)
525
 *
526
 * truncate string p at p2 [ c = char deleted by truncation ]
527
 */
5465 bpr 528
        c = *p2;if(c!=0) *p2++=0;
529
        /* replace ^<exponent group>. Add back missing character 'c' */
5503 bpr 530
        string_modify(p,p1,p2, "%s%s%s%c",SUPBEG,p3,SUPEND,c);
10 reyssat 531
    }
5465 bpr 532
}
533
 
5551 bpr 534
/* if int n != 0, use html code, else use tex code */
535
void __replace_subscript(char *p, int n)
5465 bpr 536
{
537
   char *p1, *p2;
5503 bpr 538
   char *SUBBEG, *SUBEND;
7673 bpr 539
   if (n) {SUBBEG = "<sub>"; SUBEND = "</sub>";}
5503 bpr 540
   else {SUBBEG = "_{"; SUBEND = "}";}
5465 bpr 541
   for(p1=strchr(p,'_');p1!=NULL;p1=strchr(p1+1,'_')) {
5503 bpr 542
     char buff[256];
543
     p2=p1+1;
544
     if(*p2=='(') p2=find_matching(p2+1,')');
545
     else p2=find_mathvar_end(p2);
546
     if(p2==NULL || p2>p1+64) continue;
547
     if(*(p1+1)=='(') p2++;
548
     memmove(buff,p1+1,p2-p1-1); buff[p2-p1-1]=0;
549
     strip_enclosing_par(buff);
550
     string_modify(p,p1,p2,"%s%s%s",SUBBEG,buff,SUBEND);}
5465 bpr 551
}
552
 
5517 bpr 553
/* get rid of 1*.. ..*1  exemple : 1 * x, x/1 */
5465 bpr 554
void __replace_getrid1 (char *p)
7673 bpr 555
{   char *p1, *p2, *p3 ;
10 reyssat 556
    for(p1=p;*p1;p1++) {
5503 bpr 557
      if(*p1!='1') continue;
558
      if(myisdigit(*(p1+1)) || *(p1+1)=='.' ||
559
       (p1>p && (isalnum(*(p1-1)) || *(p1-1)=='.')) ) continue;
560
      p2=find_word_start(p1+1);
561
      if(p1>p+2 && (*(p1-1)=='-' || *(p1-1)=='+')) {
562
        for(p3=p1-2; p3>p && isspace(*p3); p3--);
7673 bpr 563
        if(p3>p+1 && (*p3=='E' || *p3=='e')) { /* ??? */
5503 bpr 564
        p3--; while(p3>p && isspace(*p3)) p3--;
565
        if(myisdigit(*p3) || *p3=='.') continue;
566
        }
567
      }
7673 bpr 568
      if(p1==p) p3="+";
5503 bpr 569
      else for(p3=p1-1;p3>p && isspace(*p3);p3--);
570
      if(*p2=='*' && *p3!='/') {/* delete 1 if 1* or /1 */
571
        ovlstrcpy(p1,p2+1);continue;
572
      }
573
      if(isalpha(*p2) && *p3!='/') {
574
        ovlstrcpy(p1,p2);continue;
575
      }
576
      if(*p3=='/' && *p2!='<') ovlstrcpy(p3,p2);
10 reyssat 577
 
578
    }
5465 bpr 579
}
580
 
581
/* get rid of '*' */
582
void __replace_getridstar (char *p)
583
{ char *p1 ;
584
 for(p1=strchr(p,'*');p1!=NULL;p1=strchr(p1+1,'*')) {
5503 bpr 585
    char *pq;
586
    pq=find_word_start(p1+1);
587
    if(myisdigit(*pq)) {
588
        string_modify(p,p1,pq,"&times;");
589
        continue;
10 reyssat 590
    }
5503 bpr 591
    if(p1>p && (isalpha(*(p1-1)) || *(p1-1)==')' || *(p1-1)=='>')
592
       && (isalnum(*pq) || *pq=='$')) *p1=' ';
593
    else {
594
        ovlstrcpy(p1,p1+1);p1--;
595
    }
596
  }
5465 bpr 597
}
598
 
7673 bpr 599
/* <=, >=, ->, =>, <=>
8155 bpr 600
 * if int n != 0, use html code, else use tex code
601
 */
5551 bpr 602
 
603
void __replace_arrow ( char *p, int n)
5503 bpr 604
{   char *p1, *p2, *m_prefix;
5551 bpr 605
    if (n) m_prefix="$m_"; else m_prefix="\\";
7673 bpr 606
 
10 reyssat 607
    for(p1=strstr(p,"&lt;="); p1!=NULL; p1=strstr(p1+1,"&lt;=")) {
7673 bpr 608
      if(*(p1+5)!='&' && *(p1+5)!='=')
5503 bpr 609
        string_modify(p,p1,p1+5, "%sle",m_prefix);
610
      else {
611
          for(p2=p1+5; *p2=='='; p2++);
612
          if(strncmp(p2,"&gt;",4)==0) {
7673 bpr 613
            if(p2>p1+5) { string_modify(p,p1,p2+4,"%sLongleftrightarrow",m_prefix);}
614
            else { string_modify(p,p1,p2+4,"%sLeftrightarrow",m_prefix);}
5503 bpr 615
          }
616
          else { string_modify(p,p1,p2,"%sLongleftarrow",m_prefix);}
617
      }
10 reyssat 618
    }
619
    for(p1=strstr(p,"&gt;="); p1!=NULL; p1=strstr(p1+1,"&gt;=")) {
5503 bpr 620
      if(*(p1+5)!='=') { string_modify(p,p1,p1+5,"%sge",m_prefix);}
10 reyssat 621
    }
622
    for(p1=strstr(p,"-&gt;"); p1; p1=strstr(p1+1,"-&gt;")) {
5503 bpr 623
      for(p2=p1; p2>p && *(p2-1)=='-'; p2--);
624
      if(p2>p && *(p2-1)==';') continue;
625
      if(p2<p1) { string_modify(p,p2,p1+5,"%slongrightarrow",m_prefix);}
626
      else {  string_modify(p,p1,p1+5,"%srightarrow",m_prefix);}
10 reyssat 627
    }
628
    for(p1=strstr(p,"=&gt;"); p1; p1=strstr(p1+1,"=&gt;")) {
5503 bpr 629
      for(p2=p1; p2>p && *(p2-1)=='='; p2--);
630
      if(p2>p && *(p2-1)==';') continue;
631
      if(p2<p1) { string_modify(p,p2,p1+5,"%sLongrightarrow",m_prefix);}
632
      else { string_modify(p,p1,p1+5,"%sRightarrow",m_prefix) ;}
10 reyssat 633
    }
5503 bpr 634
/* Not equal */
10 reyssat 635
    for(p1=strstr(p,"!="); p1; p1=strstr(p1+1,"!=")) {
5503 bpr 636
      if(p1>p && !isspace(*(p1-1))) continue;
637
       string_modify(p,p1,p1+2,"%sneq",m_prefix);
10 reyssat 638
    }
5465 bpr 639
}
640
 
5503 bpr 641
/* why <tt> is used sometimes ? replace single characters by italics one
642
 * is it useful in mathml ?
8155 bpr 643
 */
5551 bpr 644
void __replace_italics (char *p, int n)
5465 bpr 645
{ char *p1, *p2, *p3, pbuf[16];
5503 bpr 646
  char *ITBEG, *ITEND, *ITtBEG, *ITtEND;
5551 bpr 647
   if (n) {
5503 bpr 648
     ITBEG = "<i>";ITtBEG = "<i><tt>";
649
     ITEND = "</i>";ITtEND = "</tt></i>";
650
  } else {;
651
     ITBEG = "" ; ITtBEG = "";
652
     ITEND = ""; ITtEND = "";
653
  }
7673 bpr 654
 
5503 bpr 655
  for(p1=p;*p1;p1++) {
656
    if(*p1=='<') {
657
        p1=strchr(p1,'>'); if(p1==NULL) break; //recognize an html tag < >
658
        else continue;
10 reyssat 659
    }
5503 bpr 660
    if(*p1=='=' && *(p1+1)=='-') {
661
        string_modify(p,p1+1,p1+1," "); p1+=2; continue;
662
    }
663
    if(*p1=='\'') {
664
        for(p2=p1+1;*p2=='\'';p2++);
665
        memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0;
666
        string_modify(p,p1,p2,"%s%s%s",ITtBEG,pbuf,ITtEND);
667
        p1=p2+strlen(ITtBEG)+strlen(ITtEND)-1;
668
        continue;
669
    }
670
    if(!isalpha(*p1)) continue;
671
/* unique letter.*/
672
    for(p2=p1+1;isalpha(*p2);p2++);
673
    p3=find_word_start(p2);
674
    if(p2>p1+5 ||
675
       (p2>p1+1 && (*p3==';' || *p3=='(' || myisdigit(*p2))))
676
        {p1=p2-1; continue;}
677
    if(strncasecmp(p3,"</i>",strlen("</i>"))==0) continue;
678
    memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0;
679
    string_modify(p,p1,p2,"%s%s%s",ITBEG,pbuf,ITEND);
680
    p1=p2+strlen(ITBEG)+strlen(ITEND)-1;
681
    }
10 reyssat 682
}
5466 bpr 683
 
5551 bpr 684
/* float (1.2 E-03) : 3E+021 -> 3 × 10^{21} - 3E-21 -> 3 × 10^{-21}
685
 * or replace variable name (alpha)
8155 bpr 686
 * if int n != 0, use html code, else use tex code
687
 */
5551 bpr 688
void __replace_mathvar(char *p,int n)
5466 bpr 689
{ char *p1, *p2, *p3;
5503 bpr 690
  char *EXPBEG, *EXPEND, *EXPBEGMINUS, *SUBBEG, *SUBEND, *m_prefix;
7673 bpr 691
 
5551 bpr 692
  if ( n ) {
5503 bpr 693
     EXPBEG = " &times; 10<sup>";
694
     EXPBEGMINUS = " &times; 10<sup>-";
695
     EXPEND = "</sup>";
696
     SUBBEG = "<sub>";
697
     SUBEND = "</sub>";
698
     m_prefix="$m_";
699
  } else {
700
     EXPBEG = " \\times 10^{" ;
701
     EXPBEGMINUS = " \\times 10^{-" ;
702
     EXPEND = "}";
703
     SUBBEG = "_{";
704
     SUBEND = "}";
705
     m_prefix="\\";
5466 bpr 706
  }
5503 bpr 707
  for (p1=find_mathvar_start(p);*p1!=0;p1=find_mathvar_start(p2)) {
708
    char buf[MAX_LINELEN+1];
8155 bpr 709
/* if the variable is preceded by \ do nothing
710
 * in fact this should not arrive
711
 */
7673 bpr 712
    if (p1>p && *(p1-1) == '\\' ) break ;
5503 bpr 713
    p2 = find_mathvar_end(p1);
714
        if (p1 == p2) break;
715
    memmove(buf,p1,p2-p1);buf[p2-p1]=0;
5466 bpr 716
 
5503 bpr 717
    if(myisdigit(buf[0])) {
8155 bpr 718
/* number : 3E+021 -> 3 x 10^{21} - 3E-21 -> 3 x 10^{-21} */
5503 bpr 719
        int k = 1, minus = 0;
720
 
721
/* see putnumber in texmath.c*/
722
        if( (p3 = strpbrk(buf, "Ee")) == NULL) continue;
723
        p1 += p3-buf;
724
            //count the number of 0, +, -
5517 bpr 725
        if (*(p1+k) == '-') { minus = 1; k++; }
5503 bpr 726
        while(*(p1+k)=='0' || *(p1+k)=='+') k++;
727
 
5517 bpr 728
        string_modify(p,p1,p1+k, minus? EXPBEGMINUS: EXPBEG);
729
        p2 += strlen(minus? EXPBEGMINUS: EXPBEG)-k;
730
        string_modify(p,p2,p2, EXPEND);
731
        p2 += strlen(EXPEND);
5503 bpr 732
    } else {
8155 bpr 733
/* alphabetic name, replace greek letters and symbols in hmname
734
 * not done in texmath.c
735
 */
5503 bpr 736
        int i = search_list(hmname,hmname_no,sizeof(hmname[0]),buf);
737
        char *n, *r;
738
        if(i<0) { /* not in list */
739
                int k = strlen(buf)-1;
740
            if(myisdigit(buf[k])) {
741
               while(k>0 && myisdigit(buf[k-1])) k--;
742
               string_modify(buf,buf+k,buf+k,SUBBEG);
743
               string_modify(p,p1,p2,"%s%s",buf,SUBEND);
744
               p2+=strlen(SUBBEG)+strlen(SUBEND);
745
            }
746
            continue;
747
        }
748
        n = hmname[i].name;
749
        r = mathalign_base < 2? hmname[i].replace: hmname[i].replacem;
750
        if(r[0]==0) { /* replace = "" */
751
                string_modify(p,p1,p2,"%s%s",m_prefix, n);
752
                p2 = p1+strlen(n)+strlen(m_prefix);
753
        } else {
754
                string_modify(p,p1,p2,"%s",r);
755
                p2 = p1+strlen(r);
756
            }
757
        }
758
  }
5465 bpr 759
}
760
 
7673 bpr 761
/* translate raw math expression coming from calculators into best html way
8155 bpr 762
 * if int n != 0, use html code, else use tex code
763
 */
5551 bpr 764
void __htmlmath(char *p,int n)
5465 bpr 765
{
5503 bpr 766
    if(!rawmath_easy) { rawmath_easy=1; rawmath(p); rawmath_easy=0;}
5466 bpr 767
    __replace_htmlmath_gtlt(p); //only used in deductio
5551 bpr 768
    __replace_exponent(p,n);
769
    __replace_subscript(p,n);
5517 bpr 770
    __replace_getrid1(p);
5551 bpr 771
    __replace_mathvar(p,n);
5517 bpr 772
    __replace_getridstar(p);
5551 bpr 773
    __replace_arrow(p,n);
5503 bpr 774
/* Now make substitutions */
5465 bpr 775
    substit(p);
5503 bpr 776
/* Make single names italic - done automatically by mathml */
5551 bpr 777
   __replace_italics(p,n);
5465 bpr 778
   strip_trailing_spaces(p);
779
}
780
 
7673 bpr 781
void htmlmath(char *p)
5465 bpr 782
{
5551 bpr 783
 __htmlmath(p,1) ;
5544 bpr 784
}
785
 
786
/* if mathml is closed, it will be just htmlmath*/
787
 
7673 bpr 788
void mathmlmath(char *p)
789
{
8500 bpr 790
    /*
791
     if force_mathml variable is set to "yes", do not (never) use 'htmlmath'
792
     in output, so command: !mathmlmath some_LaTeX_string will produce mathml
793
     (mathml \input inputfields may be included)
8499 schaersvoo 794
    */
795
    if( strcmp( getvar("force_mathml"),"yes") == 0 ){ mathalign_base = 2;mathml(p,1);return; }
5551 bpr 796
    if (mathalign_base == 2) { __htmlmath(p,0) ; mathml(p,1);} else { __htmlmath(p,1) ;}
5465 bpr 797
}