Subversion Repositories wimsdev

Rev

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