Subversion Repositories wimsdev

Rev

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