Subversion Repositories wimsdev

Rev

Rev 8094 | Rev 8149 | 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
 
8100 bpr 113
/* modify a string. Bufferlen must be at least MAX_LINELEN */
114
void string_modify3(char *start, char *bad_beg, char *bad_end, char *good,...)
10 reyssat 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
}
127
 
128
void cutamp(char *p)
129
{
130
    char *pp;
131
    for(pp=strchr(p,'&'); pp; pp=strchr(pp+1,'&')) {
7676 bpr 132
      if(strncmp(pp,"&amp;",5)==0) {
133
          ovlstrcpy(pp+1,pp+5); continue;
134
      }
135
      if(strncmp(pp,"&lt;",4)==0) {
136
          *pp='<'; ovlstrcpy(pp+1,pp+4); continue;
137
      }
138
      if(strncmp(pp,"&gt;",4)==0) {
139
          *pp='>'; ovlstrcpy(pp+1,pp+4); continue;
140
      }
141
 
10 reyssat 142
    }
143
}
144
 
7676 bpr 145
/* get the file */
10 reyssat 146
void prepare_file(void)
147
{
148
    FILE *f;
149
    long int flen;
150
 
151
    filelen=0;
152
    f=fopen(fn1,"r"); if(f==NULL) return;
153
    fseek(f,0,SEEK_END);flen=ftell(f); fseek(f,0,SEEK_SET);
154
    if(flen>buflim) return;
155
    filebuf=xmalloc(2*flen+1024);flen=fread(filebuf,1,flen,f);
156
    fclose(f);
157
    if(flen<0 || flen>=buflim) flen=0; filebuf[flen]=0;
158
    filelen=flen;
159
    outf=fopen(fn2,"w"); if(outf==NULL) return;
160
}
161
 
162
void getmath(char *p)
163
{
164
    char *pt, *pv;
165
    char *p1, *p2, buf[256];
166
 
167
    mathbuf[0]=0;
168
    pt=find_word_start(p);
169
    if(strncmp(pt,"\\begin{displaymath}",
7676 bpr 170
               strlen("\\begin{displaymath}"))==0) {
171
      pt=strchr(pt,'}')+1;
172
      pv=strstr(pt,"\\end{displaymath}");
173
      if(pv==NULL) return;
174
      goto insmath;
10 reyssat 175
    }
176
    if(*pt=='%') pt=strchr(pt,'$'); if(pt==NULL) return;
177
    if(*pt!='$') return; do pt++; while(*pt=='$');
178
    pv=strchr(pt,'$'); if(pv==NULL) return;
179
    insmath: if(pv-pt>=MAX_LINELEN-256) return;
180
    memmove(mathbuf,pt,pv-pt); mathbuf[pv-pt]=0;
181
    if(strstr(mathbuf,"...\n...")!=NULL) {
7676 bpr 182
      ovlstrcpy(mathbuf,"......"); return;
10 reyssat 183
    }
184
    cutamp(mathbuf); latex2html=1;
185
    for(p1=strstr(mathbuf,"\\mathbb");p1;p1=strstr(p1+1,"\\mathbb")) {
7676 bpr 186
      char c,*d;
187
      p2=find_word_start(p1+strlen("\\mathbb")); c=0;
188
      if(strchr("NZQRC",*p2)!=NULL) c=*p2;
189
      else if(*p2=='{' && *(p2+2)=='}' && strchr("NZQRC",*(p2+1))!=NULL) {
190
            c=*(p2+1); p2+=2;
191
      }
192
      if(c) {
193
          p2=find_word_start(++p2);
194
          if(isalnum(*p2)) d=" "; else d="";
195
          string_modify(mathbuf,p1,p2,"\\%c%c%s",c,c,d);
196
      }
10 reyssat 197
    }
198
    for(p1=strstr(mathbuf,"{\\"); p1; p1=strstr(p1+1,"{\\")) {
7676 bpr 199
      if(p1>mathbuf && isalpha(*(p1-1))) continue;
200
      for(p2=p1+2; p2<p1+24 && isalpha(*p2); p2++);
201
      if(*p2!='}' || isalnum(*(p2+1))) continue;
202
      memmove(buf,p1+1,p2-p1-1); buf[p2-p1-1]='\\'; buf[p2-p1]=0;
203
      if(strstr(hmsame,buf)==NULL) continue;
204
      ovlstrcpy(p2,p2+1); ovlstrcpy(p1,p1+1);
10 reyssat 205
    }
206
    if(strstr(mathbuf,"\\begin{")!=NULL) return;
207
    for(p1=strchr(mathbuf,'{'); p1; p1=strchr(p1+1,'{')) {
7676 bpr 208
      if((p1>mathbuf && isalpha(*(p1-1))) ||
209
         !isalnum(*(p1+1)) || *(p1+2)!='}') continue;
210
      *p1=*(p1+1); ovlstrcpy(p1+1,p1+3);
10 reyssat 211
    }
212
    if(strchr(mathbuf,'[')!=NULL) {
213
        char mbuf[MAX_LINELEN+1];
7676 bpr 214
      snprintf(mbuf,sizeof(mbuf),"{%s}",mathbuf);
215
      ovlstrcpy(mathbuf,mbuf);
10 reyssat 216
    }
7676 bpr 217
/* try to simplify */
10 reyssat 218
    if(strchr(mathbuf,'{')==NULL && strchr(mathbuf,'\\')!=NULL) {
7676 bpr 219
      int i, tt;
220
      tt=0;
221
      for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
222
          for(p2=p1+1;isalpha(*p2);p2++);
223
          if(p2==p1+1 || p2>p1+24) {tt=1; break;}
224
          memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
225
          for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
226
          if(i>=backtransno && strstr(hmsame,buf)==NULL) {
227
            tt=1; break;
228
          }
229
      }
230
      if(tt==0) {
231
          for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
232
            for(p2=p1+1;isalpha(*p2);p2++);
233
            if(p2==p1+1 || p2>p1+24) break;
234
            memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
235
            for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
236
            if(i<backtransno)
237
              string_modify(buf,p1,p2,backtrans[i].trans);
238
            else *p1=' ';
239
          }
240
      }
10 reyssat 241
    }
242
}
243
 
244
void output(void)
245
{
246
    char *p, *pp, *p2, *pt;
247
    char buf[MAX_LINELEN+1];
248
    p=filebuf;
249
    restart:
250
    pp=find_tag(p,"body"); if(*pp!=0) {
7676 bpr 251
      p=find_tag_end(pp); goto restart;
10 reyssat 252
    }
253
    pp=find_tag(p,"html"); if(*pp!=0) {
7676 bpr 254
      p=find_tag_end(pp); goto restart;
10 reyssat 255
    }
256
    *find_tag(p,"/body")=0; *find_tag(p,"/html")=0;
257
    for(pp=strstr(p,"\n\n"); pp; pp=strstr(pp+1,"\n\n")) *pp=' ';
258
    for(pp=strchr(p,'<');pp!=NULL;pp=strchr(find_tag_end(pp),'<')) {
7676 bpr 259
      if(pp>p) {fwrite(p,1,pp-p,outf); p=pp;}
260
      if(latex2html && strncasecmp(pp,"<br><hr>",8)==0 &&
261
         *find_word_start(pp+8)==0) break;
262
      if(strncasecmp(pp+1,"!-- MATH",8)==0) {
263
          p2=strstr(pp+8,"-->"); if(p2==NULL) continue;
264
          *p2=0; getmath(pp+9); *p2='-';
265
          p=p2+3; pt=find_word_start(p);
266
          if(mathbuf[0] && strncasecmp(pt,"<IMG",4)==0 && isspace(*(pt+4))) {
267
            p=find_tag_end(pt); pp=pt;
268
            fprintf(outf,"\\(%s\\)",mathbuf);
269
          }
270
          continue;
271
      }
272
      if(strncasecmp(pp+1,"a",1)==0 && isspace(*(pp+2))) {
10 reyssat 273
 
7676 bpr 274
 
275
 
276
          continue;
277
      }
278
      if(strncasecmp(pp+1,"img",3)==0 && isspace(*(pp+4))) {
279
          p2=find_tag_end(pp);
280
          if(p2-pp>=MAX_LINELEN-256) continue;
281
          memmove(buf,pp+1,p2-pp-2); buf[p2-pp-2]=0;
282
          pt=strstr(buf,"ALT=\""); if(pt==NULL) pt=strstr(buf,"alt=\"");
283
          if(pt!=NULL) {
284
            pt+=strlen("ALT=\"");
285
            getmath(pt); if(mathbuf[0]) {
286
                fprintf(outf,"\\(%s\\)",mathbuf); p=p2;
287
            }
288
          }
289
      }
10 reyssat 290
    }
291
    if(pp==NULL) fprintf(outf,"%s",p);
292
}
293
 
294
int main(int argc, char *argv[])
295
{
296
    char *p, *pp;
297
    char *mod;
298
 
299
    mod=getenv("w_module");
300
    if(mod!=NULL && strncmp(mod,"adm/",4)!=0 && strcmp(mod,"home")!=0) return 1;
301
    if(mod==NULL) p=argv[1]; else p=getenv("wims_exec_parm");
302
    if(p==NULL || *p==0) return 1;
303
    p=find_word_start(p); pp=find_word_end(p);
304
    if(pp<=p || pp-p>sizeof(fn1)-1) return 1;
305
    memmove(fn1,p,pp-p); fn1[pp-p]=0;
306
    p=find_word_start(pp); pp=find_word_end(p);
7676 bpr 307
    if(pp<=p || pp-p>sizeof(fn2)-1) ovlstrcpy(fn2,fn1);
10 reyssat 308
    else {memmove(fn2,p,pp-p); fn2[pp-p]=0;}
309
    prepare_file();
310
    output();
311
    fclose(outf);
312
    return 0;
313
}