#include #include #include #include #include int MAKEALL = 0; // set to 1 if all directories need to be built, otherwise, only the directories for which the .txt file has been updated will be (re)created int WRITE_NUMBER_SUBDIR = 0; typedef struct { char dirname[50][50]; // levels 0.. char fulldir[500]; int update; // 0 if no update, 1 if update char title[500]; long level; long parent; // index of parent page long nbchild; // number of children long nbchild1; // number of children + grandchildren + ... } WEBDIR; int main(int argc, char **argv) { FILE *fp; FILE *fp1; char *token; char fname[200]; char fnamemd[200]; WEBDIR webdir[2000]; char line[200]; char str0[200]; char str1[200]; char strchild[200]; char *sep = "./"; long webdirnb = 0; long webdirnb1; char command[200]; long level,level1; long i,l; long wd,wd1; char fulldirectory[500]; char titleline[200]; int OK; long lastlevel; int hfont; struct stat filestat_html; struct stat filestat_txt; struct stat filestat_introtxt; struct stat filestat_titletxt; char updatetime[ 200 ] = ""; char updatetimetxt[ 200 ] = ""; double tinterval; double tintervalintro; double tintervaltitle; int mdmode = 0; // 1 if .md file is input to webpage -> then, use md2html if(argc==2) { if(strcmp(argv[1],"all")==0) { MAKEALL = 1; } } // establish list of .web directories printf("Scanning directories ...\n"); fflush(stdout); system("ls -d *.web* *.web*/*.web* *.web*/*.web*/*.web* *.web*/*.web*/*.web*/*.web* *.web*/*.web*/*.web*/*.web*/*.web* > listdir.txt"); fp = fopen("listdir.txt", "r"); while ((fscanf(fp,"%s\n",line))!=EOF) { level = 0; strcpy(webdir[webdirnb].fulldir, line); token = strtok(line, sep); // printf("TOKEN = %s\n", token); strcpy(webdir[webdirnb].dirname[level], token); // sprintf(fulldirectory,"%s.web", token); while((token=strtok(NULL,sep))!=NULL) { if(strcmp(token, "web")!=0) { level++; //printf("TOKEN (l=%ld) = %s\n",level,token); strcpy(webdir[webdirnb].dirname[level], token); //printf("CHECK [%ld %ld]%s\n",webdirnb,level,webdir[webdirnb].dirname[level]); } } webdir[webdirnb].level = level; printf(" %s -> level = %ld\n",webdir[webdirnb].fulldir,webdir[webdirnb].level); webdir[webdirnb].update = 0; // CHECK THAT FILES EXIST: (content.txt or content.md) intro.txt title.txt sprintf(fname, "%s/content.txt", webdir[webdirnb].fulldir); sprintf(fnamemd, "%s/content.md", webdir[webdirnb].fulldir); if ((!stat(fname, &filestat_txt)) || (!stat(fnamemd, &filestat_txt))) { //strftime(updatetime, 100, "%d/%m/%Y %H:%M:%S", localtime( &filestat_txt.st_mtime)); //printf("File %s last modified date and time = %s\n", fname, t); } else { printf("ERROR: File %s or %s does not exits.\n", fname, fnamemd); exit(0); } sprintf(fname,"%s/intro.txt", webdir[webdirnb].fulldir); if (!stat(fname, &filestat_introtxt)) { //strftime(updatetime, 100, "%d/%m/%Y %H:%M:%S", localtime( &filestat_txt.st_mtime)); //printf("File %s last modified date and time = %s\n", fname, t); } else { printf("ERROR: File %s does not exist ?\n", fname); exit(0); } sprintf(fname,"%s/title.txt",webdir[webdirnb].fulldir); if (!stat(fname, &filestat_titletxt)) { //strftime(updatetime, 100, "%d/%m/%Y %H:%M:%S", localtime( &filestat_txt.st_mtime)); //printf("File %s last modified date and time = %s\n", fname, t); } else { printf("WARNING: File %s: does not exist.\n", fname); } // if content.html does not exist, force update sprintf(fname,"%s/content.html",webdir[webdirnb].fulldir); if (!stat(fname, &filestat_html)) { //strftime(updatetime, 100, "%d/%m/%Y %H:%M:%S", localtime( &filestat_html.st_mtime)); // printf("File %s last modified date and time = %s\n", fname, t); } else { printf("File %s does not exist: will be created.\n", fname); webdir[webdirnb].update = 1; } tinterval = difftime(filestat_html.st_mtime,filestat_txt.st_mtime); tintervalintro = difftime(filestat_html.st_mtime,filestat_introtxt.st_mtime); tintervaltitle = difftime(filestat_html.st_mtime,filestat_titletxt.st_mtime); // printf(" interval = %g\n",tinterval); if((tinterval < 0.0)||(tintervalintro < 0.0)||(tintervaltitle < 0.0)) { printf("WILL UPDATE %s\n",webdir[webdirnb].fulldir); webdir[webdirnb].update = 1; } sprintf(fname,"%s/title.txt", webdir[webdirnb].fulldir); if((fp1 = fopen(fname,"r"))==NULL) { printf("WARNING: cannot read file \"%s\"\n",fname); sprintf(webdir[webdirnb].title, ""); } else { fgets(titleline, 200, fp1); fclose(fp1); strcpy(webdir[webdirnb].title, titleline); } webdirnb++; } fclose(fp); system("rm listdir.txt"); //exit(0); /* for(wd=0;wdlevel) if(strcmp(webdir[wd1].dirname[level],webdir[wd].dirname[level])==0) webdir[wd].nbchild1++; } } printf("\n"); // make index.html printf("Writing index.html ...\n"); lastlevel = 0; fp = fopen("webpage_sec2.txt", "w"); // this is the menu fprintf(fp,"

Home



"); for(wd=0; wd0)) { if(webdir[wd].nbchild==webdir[wd].nbchild1) sprintf(strchild," (%ld)", webdir[wd].nbchild); else sprintf(strchild," (%ld,%ld)", webdir[wd].nbchild, webdir[wd].nbchild1); } // if(webdir[wd].level==0) if(strlen(webdir[wd].title)>1) fprintf(fp,"
%s %s %s
\n",webdir[wd].level, str0, webdir[wd].fulldir, webdir[wd].title, strchild); //else // fprintf(fp,"%s%s%s
\n",str0,webdir[wd].fulldir,webdir[wd].dirname[0],webdir[wd].title,strchild); } } fclose(fp); sprintf(command, "./scripts/fileupdatetime.txt index.txt > updatetime.txt"); //, webdir[wd].fulldir, webdir[wd].dirname[webdir[wd].level]); system(command); system("cat webpage_sec1.0.txt index.intro.txt webpage_sec1.1.txt webpage_sec2.txt webpage_sec3.txt index.txt webpage_sec5.txt updatetime.txt webpage_sec6.txt > index.html"); system("cp webpic_home.png webpic.png"); system("./scripts/mkwebpicgrad.txt"); system("mv webpic_gd.jpg webpic_home.jpg"); printf("\n"); // make site map // site map is always recreated printf("Making site map ...\n"); fp = fopen("sitemap.txt", "w"); for(wd=0; wd1) { hfont = webdir[wd].level+2; if(hfont > 4) hfont = 4; hfont = 4; sprintf(str0,""); sprintf(str1,""); if(webdir[wd].level==0) strcat(str0,""); for(i=0; i %s
%s\n",str0,webdir[wd].fulldir,webdir[wd].title,str1); else fprintf(fp,"%s %s
%s\n",str0,webdir[wd].fulldir,webdir[wd].title,str1); } fclose(fp); system("mv sitemap.txt ./99sitemap.web/content.txt"); printf("\n"); // make every other page printf("Making web pages ...\n"); for(wd=0; wd Home
\n",str1); for(wd1=0; wd10) { if(webdir[wd1].nbchild==webdir[wd1].nbchild1) sprintf(strchild," (%ld)",webdir[wd1].nbchild); else sprintf(strchild," (%ld,%ld)",webdir[wd1].nbchild,webdir[wd1].nbchild1); } level1 = webdir[wd1].level; for(i=0; i%s%s\n", webdir[wd1].level, str0, webdir[wd1].title); //else // fprintf(fp,"%s%s
\n",str0,webdir[wd1].title); } else { OK = 0; if(webdir[wd1].level == 0) OK = 1; else { if(webdir[wd].level>0) if(strcmp(webdir[wd1].dirname[webdir[wd1].level-1],webdir[wd].dirname[webdir[wd].level-1])==0) OK = 1; // same parent } for(i=0; i0) { if(strcmp(webdir[wd1].dirname[webdir[wd1].level-1],webdir[wd].dirname[webdir[wd].level])==0) OK = 1; // level below } if(OK==1) { // if(webdir[wd1].level == 0) fprintf(fp,"
",webdir[wd1].level); //else // fprintf(fp,""); sprintf(str1,""); for(i=0; i %s %s",str0,str1,webdir[wd1].fulldir,webdir[wd1].title,strchild); // if(webdir[wd1].level == 0) fprintf(fp,"
\n"); //else //fprintf(fp,"
\n"); } } } fclose(fp); fp = fopen("webpage_nomenulink.txt","w"); fprintf(fp," Show content only (no menu, header)
\n"); // fprintf(fp," Edit source
"); fprintf(fp,"

\n"); fclose(fp); sprintf(fname,"%s/content.html",webdir[wd].fulldir); if (!stat(fname, &filestat_html)) { strftime(updatetime, 200, "%d/%m/%Y %H:%M:%S %Z", localtime( &filestat_html.st_mtime)); printf("File %s last modified date and time = %s\n", fname, updatetime); } sprintf(fname, "%s/content.md", webdir[wd].fulldir); if (!stat(fname, &filestat_txt)) { mdmode = 1; strftime(updatetimetxt, 200, "%d/%m/%Y %H:%M:%S %Z", localtime( &filestat_txt.st_mtime)); printf("File %s last modified date and time = %s\n", fname, updatetimetxt); } else mdmode = 0; if(mdmode==1) { //sprintf(command, "cd %s", webdir[wd].fulldir); //system(command); //sprintf(command, "md2html content.md content_nomenu.html"); //system(command); //system("cd -"); // sprintf(command, "md2html %s/content.md %s/content_nomenu.html", webdir[wd].fulldir, webdir[wd].fulldir); sprintf(command, "( cd %s ; md2html content.md content_nomenu.html )", webdir[wd].fulldir); system(command); //sprintf(command, "sed '1,//d' %s/content_nomenu.html | sed -n '/<\\/body>/q;p' > %s/content.txt", webdir[wd].fulldir, webdir[wd].fulldir); sprintf(command, "cp %s/content_nomenu.html %s/content.txt", webdir[wd].fulldir, webdir[wd].fulldir); system(command); } else { sprintf(fname,"%s/content.txt",webdir[wd].fulldir); if (!stat(fname, &filestat_txt)) { strftime(updatetimetxt, 200, "%d/%m/%Y %H:%M:%S %Z", localtime( &filestat_txt.st_mtime)); printf("File %s last modified date and time = %s\n", fname, updatetimetxt); } fp = fopen("updatetime.txt","w"); fprintf(fp," %s
html file generated %s
",updatetimetxt,updatetime); fclose(fp); // sprintf(command,"./scripts/fileupdatetime.txt %s/%s.txt > updatetime.txt",webdir[wd].fulldir,webdir[wd].dirname[webdir[wd].level]); // system(command); sprintf(command,"cat webpage_sec1_nomenu.txt %s/content.txt webpage_sec2_nomenu.txt > %s/content_nomenu.html",webdir[wd].fulldir,webdir[wd].fulldir); system(command); } sprintf(command,"cat webpage_sec1.0.txt %s/intro.txt webpage_sec1.1.txt webpage_sec2.txt webpage_sec3.txt webpage_nomenulink.txt %s/content.txt webpage_sec5.txt updatetime.txt webpage_sec6.txt> %s/content.html",webdir[wd]. fulldir,webdir[wd].fulldir,webdir[wd].fulldir); // printf("Command : %s\n",command); system(command); // make webpic sprintf(command,"cp %s/webpic.png webpic.png",webdir[wd].fulldir); system(command); system("./scripts/mkwebpicgrad.txt"); sprintf(command,"mv webpic_gd.jpg %s/webpic.jpg",webdir[wd].fulldir); system(command); } system("mv webpic_home.jpg webpic.jpg"); return(0); }