/* * eDiv Compiler * Copyleft (C) 2000-2002 Sion Entertainment * http://www.sion-e.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include // esta es la tabla donde se almacenan los nombres de las directivas y los textos // por los que deben ser sustituidas, definidos en template.txt struct _sust { char de[20]; char a[1024]; } sust[20]; // esta funcion recibe un nombre de fichero (actual) y, consultando el index.dok, // devuelve el documento anterior, el siguiente y el superior. Evidentemente esto // se usa para generar los enlaces de navegacion. void situame(char* actual, char* ant, char* sig, char* sup); // esta funcion abre el fichero indicado en fixero1 y busca en el el texto // encerrado entre las directivas <%title%> y <%/title%>, y lo devuelve en // el puntero "titulo" void lee_titulo(char* fixero1, char* titulo); // esta es llamada cuando nos encontramos con la directiva <%index%>. Parsea // el arbolito ese con los signos '+' y genera una unordered list mu potita. void procesa_indice(); // esta es llamada cuando encontramos <%subindex%>. Se le indica el fichero // actual y busca la entrada correspondiente en el index.dok. Entonces parsea // la porcion de arbol que engloba y genera un subíndice como los de SGML. void procesa_subindice(char* actual); FILE *f; int i,tamano; char* buffer; int main(int argc, char* argv[]) { int c,num=-1,j,sw; char nomfich[256]; char tag[20]; int flag=0; if(argc!=2) { printf("Modo de uso: %s \n",argv[0]); exit(1); } for(i=0;i') i+=2; c=0; if(!strcmp(sust[num].de,"index") || !strcmp(sust[num].de,"/index") || !strcmp(sust[num].de,"subindex")) { printf("Error en template.txt: \"%s\" es una directiva reservada\n",sust[num].de); exit(10); } } if(buffer[i]=='"') { i++; while(1) { if(buffer[i]=='"' && buffer[i-1]!='\\') break; if(buffer[i]=='\\' && buffer[i+1]=='n') buffer[++i]='\n'; else if(buffer[i]=='\\' && buffer[i+1]=='"') buffer[++i]='"'; sust[num].a[c++]=buffer[i++]; } } } free(buffer); strcpy(nomfich,argv[1]); strcat(nomfich,".dok"); if((f=fopen(nomfich,"r"))==NULL) { printf("Error abriendo fichero %s\n",nomfich); exit(4); } fseek(f,0,SEEK_END); tamano=ftell(f); fseek(f,0,SEEK_SET); if((buffer=(char*)malloc(tamano+1))==NULL) { printf("Error: memoria insuficiente\n"); exit(5); } tamano=fread(buffer,1,tamano,f); fclose(f); buffer[tamano]=0; strcpy(nomfich,argv[1]); strcat(nomfich,".html"); if((f=fopen(nomfich,"w"))==NULL) { printf("Error creando fichero %s\n",nomfich); exit(7); } printf("Procesando...\n"); fprintf(f,"\n"); for(i=0;i') i+=2; else { fclose(f); printf("Error en tag %s, byte %d\n",tag,i); exit(8); } flag=i; sw=0; for(j=0;j<=num;j++) if(!strcmp(sust[j].de,tag)) { sw=1; fwrite(sust[j].a,1,strlen(sust[j].a),f); break; } if(!sw) { if(!strcmp(tag,"index")) { procesa_indice(); flag=i; } else if(!strcmp(tag,"subindex")) { procesa_subindice(argv[1]); } else { fclose(f); printf("Error: tag no reconocido %s en byte %d\n",tag,i); exit(9); } } if(!strcmp(tag,"/title") && strcmp(argv[1],"index")) { char ant[256], sig[256], sup[256]; char tant[512], tsig[512], tsup[512], tidx[512]; situame(argv[1],ant,sig,sup); lee_titulo(ant,tant); lee_titulo(sig,tsig); lee_titulo(sup,tsup); lee_titulo("index",tidx); fprintf(f,"\n\n\n\n\n
"); if(*ant!=0) fprintf(f,"Anterior
%s",ant,tant); else fprintf(f,"Anterior
 "); fprintf(f,"
"); if(*sup!=0) fprintf(f,"Subir
%s",sup,tsup); else fprintf(f,"Subir
%s",tidx); fprintf(f,"
Contenido" "
%s
",tidx); if(*sig!=0) fprintf(f,"Siguiente
%s",sig,tsig); else fprintf(f,"Siguiente
 "); fprintf(f,"

\n"); } } } fwrite(&buffer[flag],1,strlen(&buffer[flag]),f); fclose(f); printf("Guardado %s\n",nomfich); return 0; } void situame(char* actual, char* ant, char* sig, char* sup) { FILE* fi; char* buf,tag[20],fixero[256]; int tam,p,c; int nivel=0,ni; char anterior[256],superior[20][256]; const char fin[]="<%/index%>"; int j,sw=0,pillasig=0; *ant=0; *sig=0; *sup=0; if((fi=fopen("index.dok","r"))==NULL) { printf("Error: no puedo abrir index.dok\n"); exit(13); } fseek(fi,0,SEEK_END); tam=ftell(fi); fseek(fi,0,SEEK_SET); if((buf=(char*)malloc(tam+1))==NULL) { printf("Error: memoria insuficiente\n"); exit(14); } tam=fread(buf,1,tam,fi); fclose(fi); buf[tam]=0; for(p=0;p') p+=2; else { printf("Error en fichero index.dok, tag %s, byte %d\n",tag,p); exit(15); } if(!strcmp(tag,"index")) { sw=1; break; } } } if(!sw) { printf("Error: no se encuentra directiva <%index%> en index.dok\n"); exit(16); } memset(fixero,0,256); memset(anterior,0,256); memset(superior,0,256*20); for(;pnivel) { if(ni==20) { printf("Error: el indice no puede tener mas de 20 niveles\n"); exit(17); } if(!pillasig) { nivel=ni; strcpy(superior[nivel],fixero); } } if(ni31 && buf[p]!='<') fixero[c++]=buf[p++]; fixero[c]=0; if(buf[p]=='<') p--; if(pillasig) { strcpy(ant,anterior); strcpy(sig,fixero); strcpy(sup,superior[nivel]); fclose(fi); free(buf); return; } if(!strcmp(actual,fixero)) pillasig=1; else strcpy(anterior,fixero); } if(pillasig) { strcpy(ant,anterior); strcpy(sup,superior[nivel]); fclose(fi); free(buf); return; } printf("Error: el documento no esta listado en el indice\n"); exit(18); } void lee_titulo(char* fixero1, char* titulo) { FILE* fi; char* buf,tag[20],fixero[256]; int tam,p,c; strcpy(fixero,fixero1); strcat(fixero,".dok"); if((fi=fopen(fixero,"rb"))==NULL) { sprintf(titulo,"[Error al abrir \"%s\"]",fixero); return; } fseek(fi,0,SEEK_END); tam=ftell(fi); fseek(fi,0,SEEK_SET); if((buf=(char*)malloc(tam+1))==NULL) { printf("Error: memoria insuficiente\n"); exit(11); } tam=fread(buf,1,tam,fi); fclose(fi); buf[tam]=0; for(p=0;p') p+=2; else { printf("Error en fichero %s, tag %s, byte %d\n",fixero,tag,p); exit(13); } if(!strcmp(tag,"title")) { c=0; while(p"); fprintf(f,"\n"); i+=10; return; } while(buffer[i]=='+') { ni++; i++; } if(ni>nivel) { if(ni==20) { printf("Error: el indice no puede tener mas de 20 niveles\n"); exit(25); } for(j=nivel;j"); fprintf(f,"\n"); nivel=ni; } if(ni"); fprintf(f,"\n"); nivel=ni; } c=0; while(buffer[i]>31 && buffer[i]!='<') fixero[c++]=buffer[i++]; fixero[c]=0; if(buffer[i]=='<') i--; lee_titulo(fixero,titulo); if(nivel==0) fprintf(f,"

%s

\n",fixero,titulo); else fprintf(f,"
  • %s
  • \n",fixero,titulo); } } void procesa_subindice(char* actual) { FILE* fi; char* buf,tag[20],fixero[256],titulo[512]; int tam,p,c; int nivel=0,ni,iniv; const char fin[]="<%/index%>"; int j,sw=0,pilla=0; if((fi=fopen("index.dok","r"))==NULL) { printf("Error: no puedo abrir index.dok\n"); exit(19); } fseek(fi,0,SEEK_END); tam=ftell(fi); fseek(fi,0,SEEK_SET); if((buf=(char*)malloc(tam+1))==NULL) { printf("Error: memoria insuficiente\n"); exit(20); } tam=fread(buf,1,tam,fi); fclose(fi); buf[tam]=0; for(p=0;p') p+=2; else { printf("Error en fichero index.dok, tag %s, byte %d\n",tag,p); exit(21); } if(!strcmp(tag,"index")) { sw=1; break; } } } if(!sw) { printf("Error: no se encuentra directiva <%index%> en index.dok\n"); exit(22); } for(;pnivel) { if(ni==20) { printf("Error: el indice no puede tener mas de 20 niveles\n"); exit(23); } if(pilla && ni==iniv+1) { //for(j=nivel;j"); fprintf(f,"\n"); } nivel=ni; } if(ni"); // fprintf(f,"\n"); //} nivel=ni; if(pilla && nivel<=iniv) break; } c=0; while(buf[p]>31 && buf[p]!='<') fixero[c++]=buf[p++]; fixero[c]=0; if(buf[p]=='<') p--; if(!strcmp(actual,fixero)) { pilla=1; fprintf(f,"
      "); iniv=nivel; } if(pilla && nivel<=iniv+1) { lee_titulo(fixero,titulo); fprintf(f,"
    • %s
    • \n",fixero,titulo); } } if(pilla) { //for(j=iniv;j<=nivel;j++) fprintf(f,"
    "); /*if(nivel"); fprintf(f,"\n"); return; } printf("Error: el documento no esta listado en el indice\n"); exit(24); }