Subversion Repositories wimsdev

Rev

Rev 7676 | Rev 8100 | 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
 */
17
 
7676 bpr 18
/* Check type of a file */
10 reyssat 19
 
20
/*************** Customization: change values hereafter ****************/
21
 
7676 bpr 22
/* limit of data buffers */
10 reyssat 23
#define buflim 1024*1024*16
24
 
25
/***************** Nothing should need change hereafter *****************/
26
#include "../wims.h"
8094 bpr 27
#include "../Lib/libwims.h"
10 reyssat 28
#include "../hmname.c"
29
 
30
char fn1[1024]="", fn2[1024]="";
31
char mathbuf[MAX_LINELEN+1];
32
char *filebuf;
33
int filelen=0;
34
int latex2html=0;
35
FILE *outf;
36
 
37
struct {
38
    char *name, *trans;
39
} backtrans[]={
7676 bpr 40
    {"\\ge\\", " >= "},
41
    {"\\geq\\", " >= "},
42
    {"\\le\\", " <= "},
43
    {"\\leq\\", " <= "},
44
    {"\\to\\", " -> "},
45
    {"\\rightarrow\\", " -> "},
10 reyssat 46
    {"\\longrightarrow\\", " --> "},
7676 bpr 47
    {"\\Rightarrow\\", " => "},
10 reyssat 48
    {"\\Longrightarrow\\", " ==> "},
49
    {"\\Leftrightarrow\\", " <=> "},
50
    {"\\Longleftrightarrow\\", " <==> "},
51
    {"\\Longleftarrow\\", " <== "},
52
};
53
 
54
#define backtransno (sizeof(backtrans)/sizeof(backtrans[0]))
55
 
8094 bpr 56
/*void *xmalloc(size_t n)
10 reyssat 57
{
58
    void *p;
59
    p=malloc(n);
60
    if(p==NULL) exit(1);
61
    return p;
62
}
8094 bpr 63
*/
10 reyssat 64
 
7676 bpr 65
/* Points to the end of the word */
8094 bpr 66
/*
10 reyssat 67
char *find_word_end(char *p)
68
{
69
    int i;
70
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
71
    return p;
72
}
8094 bpr 73
*/
7676 bpr 74
/* Strips leading spaces */
8094 bpr 75
/*char *find_word_start(char *p)
10 reyssat 76
{
77
    int i;
78
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
79
    return p;
80
}
8094 bpr 81
*/
10 reyssat 82
char *find_tag_end(char *p)
83
{
84
    char *pp, *old;
85
    pp=p; if(*pp=='<') pp++;
86
    for(; *pp && *pp!='>'; pp++) {
7676 bpr 87
      if(*pp=='"') {
88
          pp=strchr(pp+1,'"');
89
          if(pp==NULL) {pp=p+strlen(p); break;} else continue;
90
      }
10 reyssat 91
    }
7676 bpr 92
/* this is probably an syntax error of the page */
10 reyssat 93
    if(*pp==0 && pp>p+2048) {
7676 bpr 94
      old=p; if(*old=='<') old++;
95
      pp=strchr(old,'>');
96
      if(pp==NULL) pp=strchr(old,'<');
97
      if(pp==NULL) pp=find_word_end(find_word_start(old));
10 reyssat 98
    }
99
    if(*pp=='>') pp++; return pp;
100
}
101
 
102
char *find_tag(char *p, char *tag)
103
{
104
    char *pp;
105
    int len;
106
    len=strlen(tag);
107
    for(pp=strchr(p,'<'); pp!=NULL && *pp; pp=strchr(pp+1,'<')) {
7676 bpr 108
      if(strncasecmp(pp+1,tag,len)==0 && !isalnum(*(pp+1+len))) return pp;
10 reyssat 109
    }
110
    return p+strlen(p);
111
}
112
 
8094 bpr 113
/* modify a string. Bufferlen must be ast least MAX_LINELEN
10 reyssat 114
void string_modify(char *start, char *bad_beg, char *bad_end, char *good,...)
115
{
116
    char buf[MAX_LINELEN+1];
117
    va_list vp;
7676 bpr 118
 
10 reyssat 119
    va_start(vp,good);
120
    vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
121
    if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN) {
7676 bpr 122
      return;
10 reyssat 123
    }
124
    strcat(buf,bad_end);
3718 reyssat 125
    ovlstrcpy(bad_beg,buf);
10 reyssat 126
}
8094 bpr 127
*/
10 reyssat 128
 
129
void cutamp(char *p)
130
{
131
    char *pp;
132
    for(pp=strchr(p,'&'); pp; pp=strchr(pp+1,'&')) {
7676 bpr 133
      if(strncmp(pp,"&amp;",5)==0) {
134
          ovlstrcpy(pp+1,pp+5); continue;
135
      }
136
      if(strncmp(pp,"&lt;",4)==0) {
137
          *pp='<'; ovlstrcpy(pp+1,pp+4); continue;
138
      }
139
      if(strncmp(pp,"&gt;",4)==0) {
140
          *pp='>'; ovlstrcpy(pp+1,pp+4); continue;
141
      }
142
 
10 reyssat 143
    }
144
}
145
 
7676 bpr 146
/* get the file */
10 reyssat 147
void prepare_file(void)
148
{
149
    FILE *f;
150
    long int flen;
151
 
152
    filelen=0;
153
    f=fopen(fn1,"r"); if(f==NULL) return;
154
    fseek(f,0,SEEK_END);flen=ftell(f); fseek(f,0,SEEK_SET);
155
    if(flen>buflim) return;
156
    filebuf=xmalloc(2*flen+1024);flen=fread(filebuf,1,flen,f);
157
    fclose(f);
158
    if(flen<0 || flen>=buflim) flen=0; filebuf[flen]=0;
159
    filelen=flen;
160
    outf=fopen(fn2,"w"); if(outf==NULL) return;
161
}
162
 
163
void getmath(char *p)
164
{
165
    char *pt, *pv;
166
    char *p1, *p2, buf[256];
167
 
168
    mathbuf[0]=0;
169
    pt=find_word_start(p);
170
    if(strncmp(pt,"\\begin{displaymath}",
7676 bpr 171
               strlen("\\begin{displaymath}"))==0) {
172
      pt=strchr(pt,'}')+1;
173
      pv=strstr(pt,"\\end{displaymath}");
174
      if(pv==NULL) return;
175
      goto insmath;
10 reyssat 176
    }
177
    if(*pt=='%') pt=strchr(pt,'$'); if(pt==NULL) return;
178
    if(*pt!='$') return; do pt++; while(*pt=='$');
179
    pv=strchr(pt,'$'); if(pv==NULL) return;
180
    insmath: if(pv-pt>=MAX_LINELEN-256) return;
181
    memmove(mathbuf,pt,pv-pt); mathbuf[pv-pt]=0;
182
    if(strstr(mathbuf,"...\n...")!=NULL) {
7676 bpr 183
      ovlstrcpy(mathbuf,"......"); return;
10 reyssat 184
    }
185
    cutamp(mathbuf); latex2html=1;
186
    for(p1=strstr(mathbuf,"\\mathbb");p1;p1=strstr(p1+1,"\\mathbb")) {
7676 bpr 187
      char c,*d;
188
      p2=find_word_start(p1+strlen("\\mathbb")); c=0;
189
      if(strchr("NZQRC",*p2)!=NULL) c=*p2;
190
      else if(*p2=='{' && *(p2+2)=='}' && strchr("NZQRC",*(p2+1))!=NULL) {
191
            c=*(p2+1); p2+=2;
192
      }
193
      if(c) {
194
          p2=find_word_start(++p2);
195
          if(isalnum(*p2)) d=" "; else d="";
196
          string_modify(mathbuf,p1,p2,"\\%c%c%s",c,c,d);
197
      }
10 reyssat 198
    }
199
    for(p1=strstr(mathbuf,"{\\"); p1; p1=strstr(p1+1,"{\\")) {
7676 bpr 200
      if(p1>mathbuf && isalpha(*(p1-1))) continue;
201
      for(p2=p1+2; p2<p1+24 && isalpha(*p2); p2++);
202
      if(*p2!='}' || isalnum(*(p2+1))) continue;
203
      memmove(buf,p1+1,p2-p1-1); buf[p2-p1-1]='\\'; buf[p2-p1]=0;
204
      if(strstr(hmsame,buf)==NULL) continue;
205
      ovlstrcpy(p2,p2+1); ovlstrcpy(p1,p1+1);
10 reyssat 206
    }
207
    if(strstr(mathbuf,"\\begin{")!=NULL) return;
208
    for(p1=strchr(mathbuf,'{'); p1; p1=strchr(p1+1,'{')) {
7676 bpr 209
      if((p1>mathbuf && isalpha(*(p1-1))) ||
210
         !isalnum(*(p1+1)) || *(p1+2)!='}') continue;
211
      *p1=*(p1+1); ovlstrcpy(p1+1,p1+3);
10 reyssat 212
    }
213
    if(strchr(mathbuf,'[')!=NULL) {
214
        char mbuf[MAX_LINELEN+1];
7676 bpr 215
      snprintf(mbuf,sizeof(mbuf),"{%s}",mathbuf);
216
      ovlstrcpy(mathbuf,mbuf);
10 reyssat 217
    }
7676 bpr 218
/* try to simplify */
10 reyssat 219
    if(strchr(mathbuf,'{')==NULL && strchr(mathbuf,'\\')!=NULL) {
7676 bpr 220
      int i, tt;
221
      tt=0;
222
      for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
223
          for(p2=p1+1;isalpha(*p2);p2++);
224
          if(p2==p1+1 || p2>p1+24) {tt=1; break;}
225
          memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
226
          for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
227
          if(i>=backtransno && strstr(hmsame,buf)==NULL) {
228
            tt=1; break;
229
          }
230
      }
231
      if(tt==0) {
232
          for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
233
            for(p2=p1+1;isalpha(*p2);p2++);
234
            if(p2==p1+1 || p2>p1+24) break;
235
            memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
236
            for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
237
            if(i<backtransno)
238
              string_modify(buf,p1,p2,backtrans[i].trans);
239
            else *p1=' ';
240
          }
241
      }
10 reyssat 242
    }
243
}
244
 
245
void output(void)
246
{
247
    char *p, *pp, *p2, *pt;
248
    char buf[MAX_LINELEN+1];
249
    p=filebuf;
250
    restart:
251
    pp=find_tag(p,"body"); if(*pp!=0) {
7676 bpr 252
      p=find_tag_end(pp); goto restart;
10 reyssat 253
    }
254
    pp=find_tag(p,"html"); if(*pp!=0) {
7676 bpr 255
      p=find_tag_end(pp); goto restart;
10 reyssat 256
    }
257
    *find_tag(p,"/body")=0; *find_tag(p,"/html")=0;
258
    for(pp=strstr(p,"\n\n"); pp; pp=strstr(pp+1,"\n\n")) *pp=' ';
259
    for(pp=strchr(p,'<');pp!=NULL;pp=strchr(find_tag_end(pp),'<')) {
7676 bpr 260
      if(pp>p) {fwrite(p,1,pp-p,outf); p=pp;}
261
      if(latex2html && strncasecmp(pp,"<br><hr>",8)==0 &&
262
         *find_word_start(pp+8)==0) break;
263
      if(strncasecmp(pp+1,"!-- MATH",8)==0) {
264
          p2=strstr(pp+8,"-->"); if(p2==NULL) continue;
265
          *p2=0; getmath(pp+9); *p2='-';
266
          p=p2+3; pt=find_word_start(p);
267
          if(mathbuf[0] && strncasecmp(pt,"<IMG",4)==0 && isspace(*(pt+4))) {
268
            p=find_tag_end(pt); pp=pt;
269
            fprintf(outf,"\\(%s\\)",mathbuf);
270
          }
271
          continue;
272
      }
273
      if(strncasecmp(pp+1,"a",1)==0 && isspace(*(pp+2))) {
10 reyssat 274
 
7676 bpr 275
 
276
 
277
          continue;
278
      }
279
      if(strncasecmp(pp+1,"img",3)==0 && isspace(*(pp+4))) {
280
          p2=find_tag_end(pp);
281
          if(p2-pp>=MAX_LINELEN-256) continue;
282
          memmove(buf,pp+1,p2-pp-2); buf[p2-pp-2]=0;
283
          pt=strstr(buf,"ALT=\""); if(pt==NULL) pt=strstr(buf,"alt=\"");
284
          if(pt!=NULL) {
285
            pt+=strlen("ALT=\"");
286
            getmath(pt); if(mathbuf[0]) {
287
                fprintf(outf,"\\(%s\\)",mathbuf); p=p2;
288
            }
289
          }
290
      }
10 reyssat 291
    }
292
    if(pp==NULL) fprintf(outf,"%s",p);
293
}
294
 
295
int main(int argc, char *argv[])
296
{
297
    char *p, *pp;
298
    char *mod;
299
 
300
    mod=getenv("w_module");
301
    if(mod!=NULL && strncmp(mod,"adm/",4)!=0 && strcmp(mod,"home")!=0) return 1;
302
    if(mod==NULL) p=argv[1]; else p=getenv("wims_exec_parm");
303
    if(p==NULL || *p==0) return 1;
304
    p=find_word_start(p); pp=find_word_end(p);
305
    if(pp<=p || pp-p>sizeof(fn1)-1) return 1;
306
    memmove(fn1,p,pp-p); fn1[pp-p]=0;
307
    p=find_word_start(pp); pp=find_word_end(p);
7676 bpr 308
    if(pp<=p || pp-p>sizeof(fn2)-1) ovlstrcpy(fn2,fn1);
10 reyssat 309
    else {memmove(fn2,p,pp-p); fn2[pp-p]=0;}
310
    prepare_file();
311
    output();
312
    fclose(outf);
313
    return 0;
314
}