Subversion Repositories wimsdev

Rev

Rev 8185 | Rev 11124 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  18. /* Check type of a file */
  19.  
  20. /*************** Customization: change values hereafter ****************/
  21.  
  22. /* limit of data buffers */
  23. #define buflim 1024*1024*16
  24.  
  25. /***************** Nothing should need change hereafter *****************/
  26.  
  27. #include "../Lib/libwims.h"
  28.  
  29. #define hmsame "\\CC\\Delta\\Gamma\\Lambda\\NN\\Omega\\Phi\\Pi\\Psi\\QQ\\RR\\Sigma\\Xi\\ZZ\\alpha\\beta\\cap\\chi\\cup\\delta\\div\\eta\\exists\\forall\\gamma\\in\\infty\\iota\\kappa\\lambda\\mu\\nabla\\nu\\omega\\pi\\pm\\psi\\rho\\sigma\\subset\\subseteq\\tau\\theta\\times\\varepsilon\\varphi\\xi\\zeta\\"
  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[]={
  41.     {"\\ge\\", " >= "},
  42.     {"\\geq\\", " >= "},
  43.     {"\\le\\", " <= "},
  44.     {"\\leq\\", " <= "},
  45.     {"\\to\\", " -> "},
  46.     {"\\rightarrow\\", " -> "},
  47.     {"\\longrightarrow\\", " --> "},
  48.     {"\\Rightarrow\\", " => "},
  49.     {"\\Longrightarrow\\", " ==> "},
  50.     {"\\Leftrightarrow\\", " <=> "},
  51.     {"\\Longleftrightarrow\\", " <==> "},
  52.     {"\\Longleftarrow\\", " <== "},
  53. };
  54.  
  55. #define backtransno (sizeof(backtrans)/sizeof(backtrans[0]))
  56.  
  57. char *find_tag_end(char *p)
  58. {
  59.     char *pp, *old;
  60.     pp=p; if(*pp=='<') pp++;
  61.     for(; *pp && *pp!='>'; pp++) {
  62.       if(*pp=='"') {
  63.           pp=strchr(pp+1,'"');
  64.           if(pp==NULL) {pp=p+strlen(p); break;} else continue;
  65.       }
  66.     }
  67. /* this is probably an syntax error of the page */
  68.     if(*pp==0 && pp>p+2048) {
  69.       old=p; if(*old=='<') old++;
  70.       pp=strchr(old,'>');
  71.       if(pp==NULL) pp=strchr(old,'<');
  72.       if(pp==NULL) pp=find_word_end(find_word_start(old));
  73.     }
  74.     if(*pp=='>') pp++; return pp;
  75. }
  76.  
  77. char *find_tag(char *p, char *tag)
  78. {
  79.     char *pp;
  80.     int len;
  81.     len=strlen(tag);
  82.     for(pp=strchr(p,'<'); pp!=NULL && *pp; pp=strchr(pp+1,'<')) {
  83.       if(strncasecmp(pp+1,tag,len)==0 && !isalnum(*(pp+1+len))) return pp;
  84.     }
  85.     return p+strlen(p);
  86. }
  87.  
  88. void cutamp(char *p)
  89. {
  90.     char *pp;
  91.     for(pp=strchr(p,'&'); pp; pp=strchr(pp+1,'&')) {
  92.       if(strncmp(pp,"&amp;",5)==0) {
  93.           ovlstrcpy(pp+1,pp+5); continue;
  94.       }
  95.       if(strncmp(pp,"&lt;",4)==0) {
  96.           *pp='<'; ovlstrcpy(pp+1,pp+4); continue;
  97.       }
  98.       if(strncmp(pp,"&gt;",4)==0) {
  99.           *pp='>'; ovlstrcpy(pp+1,pp+4); continue;
  100.       }
  101.  
  102.     }
  103. }
  104.  
  105. /* get the file */
  106. void prepare_file(void)
  107. {
  108.     FILE *f;
  109.     long int flen;
  110.  
  111.     filelen=0;
  112.     f=fopen(fn1,"r"); if(f==NULL) return;
  113.     fseek(f,0,SEEK_END);flen=ftell(f); fseek(f,0,SEEK_SET);
  114.     if(flen>buflim) return;
  115.     filebuf=xmalloc(2*flen+1024);flen=fread(filebuf,1,flen,f);
  116.     fclose(f);
  117.     if(flen<0 || flen>=buflim) flen=0; filebuf[flen]=0;
  118.     filelen=flen;
  119.     outf=fopen(fn2,"w"); if(outf==NULL) return;
  120. }
  121.  
  122. void getmath(char *p)
  123. {
  124.     char *pt, *pv;
  125.     char *p1, *p2, buf[256];
  126.  
  127.     mathbuf[0]=0;
  128.     pt=find_word_start(p);
  129.     if(strncmp(pt,"\\begin{displaymath}",
  130.                strlen("\\begin{displaymath}"))==0) {
  131.       pt=strchr(pt,'}')+1;
  132.       pv=strstr(pt,"\\end{displaymath}");
  133.       if(pv==NULL) return;
  134.       goto insmath;
  135.     }
  136.     if(*pt=='%') pt=strchr(pt,'$'); if(pt==NULL) return;
  137.     if(*pt!='$') return; do pt++; while(*pt=='$');
  138.     pv=strchr(pt,'$'); if(pv==NULL) return;
  139.     insmath: if(pv-pt>=MAX_LINELEN-256) return;
  140.     memmove(mathbuf,pt,pv-pt); mathbuf[pv-pt]=0;
  141.     if(strstr(mathbuf,"...\n...")!=NULL) {
  142.       ovlstrcpy(mathbuf,"......"); return;
  143.     }
  144.     cutamp(mathbuf); latex2html=1;
  145.     for(p1=strstr(mathbuf,"\\mathbb");p1;p1=strstr(p1+1,"\\mathbb")) {
  146.       char c,*d;
  147.       p2=find_word_start(p1+strlen("\\mathbb")); c=0;
  148.       if(strchr("NZQRC",*p2)!=NULL) c=*p2;
  149.       else if(*p2=='{' && *(p2+2)=='}' && strchr("NZQRC",*(p2+1))!=NULL) {
  150.             c=*(p2+1); p2+=2;
  151.       }
  152.       if(c) {
  153.           p2=find_word_start(++p2);
  154.           if(isalnum(*p2)) d=" "; else d="";
  155.           string_modify(mathbuf,p1,p2,"\\%c%c%s",c,c,d);
  156.       }
  157.     }
  158.     for(p1=strstr(mathbuf,"{\\"); p1; p1=strstr(p1+1,"{\\")) {
  159.       if(p1>mathbuf && isalpha(*(p1-1))) continue;
  160.       for(p2=p1+2; p2<p1+24 && isalpha(*p2); p2++);
  161.       if(*p2!='}' || isalnum(*(p2+1))) continue;
  162.       memmove(buf,p1+1,p2-p1-1); buf[p2-p1-1]='\\'; buf[p2-p1]=0;
  163.       if(strstr(hmsame,buf)==NULL) continue;
  164.       ovlstrcpy(p2,p2+1); ovlstrcpy(p1,p1+1);
  165.     }
  166.     if(strstr(mathbuf,"\\begin{")!=NULL) return;
  167.     for(p1=strchr(mathbuf,'{'); p1; p1=strchr(p1+1,'{')) {
  168.       if((p1>mathbuf && isalpha(*(p1-1))) ||
  169.          !isalnum(*(p1+1)) || *(p1+2)!='}') continue;
  170.       *p1=*(p1+1); ovlstrcpy(p1+1,p1+3);
  171.     }
  172.     if(strchr(mathbuf,'[')!=NULL) {
  173.         char mbuf[MAX_LINELEN+1];
  174.       snprintf(mbuf,sizeof(mbuf),"{%s}",mathbuf);
  175.       ovlstrcpy(mathbuf,mbuf);
  176.     }
  177. /* try to simplify */
  178.     if(strchr(mathbuf,'{')==NULL && strchr(mathbuf,'\\')!=NULL) {
  179.       int i, tt;
  180.       tt=0;
  181.       for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
  182.           for(p2=p1+1;isalpha(*p2);p2++);
  183.           if(p2==p1+1 || p2>p1+24) {tt=1; break;}
  184.           memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
  185.           for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
  186.           if(i>=backtransno && strstr(hmsame,buf)==NULL) {
  187.             tt=1; break;
  188.           }
  189.       }
  190.       if(tt==0) {
  191.           for(p1=strchr(mathbuf,'\\'); p1; p1=strchr(p1+1,'\\')) {
  192.             for(p2=p1+1;isalpha(*p2);p2++);
  193.             if(p2==p1+1 || p2>p1+24) break;
  194.             memmove(buf,p1,p2-p1);buf[p2-p1]='\\';buf[p2-p1+1]=0;
  195.             for(i=0;i<backtransno && strcmp(buf,backtrans[i].name)!=0;i++);
  196.             if(i<backtransno)
  197.               string_modify(buf,p1,p2,backtrans[i].trans);
  198.             else *p1=' ';
  199.           }
  200.       }
  201.     }
  202. }
  203.  
  204. void output(void)
  205. {
  206.     char *p, *pp, *p2, *pt;
  207.     char buf[MAX_LINELEN+1];
  208.     p=filebuf;
  209.     restart:
  210.     pp=find_tag(p,"body"); if(*pp!=0) {
  211.       p=find_tag_end(pp); goto restart;
  212.     }
  213.     pp=find_tag(p,"html"); if(*pp!=0) {
  214.       p=find_tag_end(pp); goto restart;
  215.     }
  216.     *find_tag(p,"/body")=0; *find_tag(p,"/html")=0;
  217.     for(pp=strstr(p,"\n\n"); pp; pp=strstr(pp+1,"\n\n")) *pp=' ';
  218.     for(pp=strchr(p,'<');pp!=NULL;pp=strchr(find_tag_end(pp),'<')) {
  219.       if(pp>p) {fwrite(p,1,pp-p,outf); p=pp;}
  220.       if(latex2html && strncasecmp(pp,"<br><hr>",8)==0 &&
  221.          *find_word_start(pp+8)==0) break;
  222.       if(strncasecmp(pp+1,"!-- MATH",8)==0) {
  223.           p2=strstr(pp+8,"-->"); if(p2==NULL) continue;
  224.           *p2=0; getmath(pp+9); *p2='-';
  225.           p=p2+3; pt=find_word_start(p);
  226.           if(mathbuf[0] && strncasecmp(pt,"<IMG",4)==0 && isspace(*(pt+4))) {
  227.             p=find_tag_end(pt); pp=pt;
  228.             fprintf(outf,"\\(%s\\)",mathbuf);
  229.           }
  230.           continue;
  231.       }
  232.       if(strncasecmp(pp+1,"a",1)==0 && isspace(*(pp+2))) {
  233.  
  234.  
  235.  
  236.           continue;
  237.       }
  238.       if(strncasecmp(pp+1,"img",3)==0 && isspace(*(pp+4))) {
  239.           p2=find_tag_end(pp);
  240.           if(p2-pp>=MAX_LINELEN-256) continue;
  241.           memmove(buf,pp+1,p2-pp-2); buf[p2-pp-2]=0;
  242.           pt=strstr(buf,"ALT=\""); if(pt==NULL) pt=strstr(buf,"alt=\"");
  243.           if(pt!=NULL) {
  244.             pt+=strlen("ALT=\"");
  245.             getmath(pt); if(mathbuf[0]) {
  246.                 fprintf(outf,"\\(%s\\)",mathbuf); p=p2;
  247.             }
  248.           }
  249.       }
  250.     }
  251.     if(pp==NULL) fprintf(outf,"%s",p);
  252. }
  253.  
  254. int main(int argc, char *argv[])
  255. {
  256.     char *p, *pp;
  257.     char *mod;
  258.  
  259.     mod=getenv("w_module");
  260.     if(mod!=NULL && strncmp(mod,"adm/",4)!=0 && strcmp(mod,"home")!=0) return 1;
  261.     if(mod==NULL) p=argv[1]; else p=getenv("wims_exec_parm");
  262.     if(p==NULL || *p==0) return 1;
  263.     p=find_word_start(p); pp=find_word_end(p);
  264.     if(pp<=p || pp-p>sizeof(fn1)-1) return 1;
  265.     memmove(fn1,p,pp-p); fn1[pp-p]=0;
  266.     p=find_word_start(pp); pp=find_word_end(p);
  267.     if(pp<=p || pp-p>sizeof(fn2)-1) ovlstrcpy(fn2,fn1);
  268.     else {memmove(fn2,p,pp-p); fn2[pp-p]=0;}
  269.     prepare_file();
  270.     output();
  271.     fclose(outf);
  272.     return 0;
  273. }
  274.