#define PROGRAM "mkhtml" #define VERSION "0.0.1-4" #include #include #include #include #include FILE *document; char filename[256] = "mkhtml.html"; DIR *dir; struct dirent *de; /* HTML */ char *HTML_OPEN = "\n"; char *HTML_CLOSE = "\n"; /* Header */ char *HEAD_OPEN = "\t\n"; char *TITLE = "\t\t\n"; char *META = "\t\t\n"; char *HEAD_CLOSE = "\t\n"; /* JS */ char *SCRIPT_OPEN = "\t\n"; /* CSS */ char *CSS_OPEN = "\t\n"; /* Body */ char *BODY_OPEN = "\t\n"; char *BODY_CONTENT_A = "\n"; char *BODY_CONTENT_B = "\t\t
zurück
\n"; char *BODY_CLOSE = "\t\n"; /* Table */ char *TABLE_OPEN = "\t\t\n"; char *TABLE_CLOSE = "\t\t
\n"; char *ROW_OPEN = "\t\t\t\n"; char *ROW_CLOSE = "\t\t\t\n"; char *CELL_OPEN = "\t\t\t\t"; char *TABLE_CELL = "\t\t\t\t\n"; /* remove tabs and newlines if you want */ char *CELL_CLOSE = "\n"; /* List */ char *OL_OPEN = "\t\t
    \n"; char *LI_OPEN = "\t\t\t
  1. "; char *LIST_ENTRY = "\t\t\t
  2. \n"; char *LI_CLOSE = "\n"; char *OL_CLOSE = "\t\t
\n"; /* forward declaration */ int write_document_top(); int write_document(); int write_document_bottom(); /* getopt Section **********************************************************************************************************************************************************************************************/ struct option mkhtml_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'v' }, { "nocss", 0, NULL, 0 }, { "noscr", 0, NULL, 0 }, { "emptytags", 0, NULL, 'e' }, { "notitle", 0, NULL, 0 }, { "nometa", 0, NULL, 0 }, { "output", 1, NULL, 'o' }, { "ofile", 1, NULL, 'o' }, { "table", 0, NULL, 't' }, { "diridx", 0, NULL, 'd' }, { "gallery", 0, NULL, 'g' }, { "list", 0, NULL, 'l' }, { "frameset", 0, NULL, 'f'} }; int o_help=0, o_version=0, o_nocss=0, o_noscr=0, o_emptytags=0, o_nometa=0, o_notitle=0, o_table=0, o_diridx=0, o_gallery=0, o_list=0, o_frameset=0; int option, option_idx; int parse_options(int argc, char *argv[]) { option_idx = 0; opterr = 0; /* getopt.h */ while (1) { option = getopt_long(argc, argv, "hveotd", mkhtml_options, &option_idx); if (option == EOF) break; switch(option) { case 'e' : o_emptytags=1; break; case 'v' : o_version=1; break; case 't' : o_table=1; break; case 'd' : o_diridx=1; break; case 'g' : o_gallery=1; break; case 'l' : o_list=1; break; case 'f' : o_frameset=1; break; case 'h' : o_help=1; break; case 'o' : if (optarg) strcpy(filename, optarg); break; case 0 : if (!strcmp(mkhtml_options[option_idx].name, "nocss")) o_nocss=1; /* strcmp returns 0 bei Erfolg, !strcmp bedeutet in dem Fall Erfolg */ else if (strcmp(mkhtml_options[option_idx].name, "noscr")==0) o_noscr=1; else if (strcmp(mkhtml_options[option_idx].name, "nometa")==0) o_nometa=1; else if (!strcmp(mkhtml_options[option_idx].name, "notitle")) o_notitle=1; break; case '?': fprintf(stdout, "unknown option"); break; case ':': fprintf(stdout, "invalid option"); break; default: break; } } return 1; } int version() { fprintf(stdout, "%s\n", VERSION); return 0; } int help(char *arg0) { fprintf(stdout, "\n" PROGRAM " " VERSION "\n" "usage: %s [options]\n" "where options are some of the following\n" "\n" "--help -h show help\n" "--version -v show version number\n" "--nocss - no css\n" "--noscr - no javascript\n" "--emptytags -e with all tags, but without their contents...\n" "--nometa - to turn of meta tag explicit\n" "--notitle - to leave out title tag\n" "--table -t generate empty table\n" "--diridx - index all files in this directory\n" "--gallery - index dir and create table with img tags\n" "\n" "--output filename - output to filename (default %s)\n" "--ofile filename - s.a.\n" "\n" "--frameset - creates frameset.html frame_nav.html frame_home.html\n" " with directory names + files index anchored in frame_nav.html\n" "\n", arg0, filename ); return 0; } /* mkhtml Section **********************************************************************************************************************************************************************************************/ int write_string_to_disk(char *str) { fputs(str,document); } int write_newline() { write_string_to_disk("\n"); } int write_newline_br() { write_string_to_disk("
\n"); } int write_newline_hr() { write_string_to_disk("
\n"); } int generate_directory_index(char *path=".") { char str[2048]; if (dir=opendir(path)) { rewinddir(dir); while (de=readdir(dir)) { if (strcmp(de->d_name, ".")) if (strcmp(de->d_name, "..")) { sprintf(str, "\t\t%s\n", de->d_name, de->d_name); write_string_to_disk(str); } } closedir(dir); } else { return 0; } return 1; } int generate_table(int columns=3, int rows=3) { char str[2048]; write_string_to_disk(TABLE_OPEN); for (int i = 1; i<=rows; i++) { memset(str, 0, sizeof(str)); strcat(str, ROW_OPEN); for (int j = 1; j<=columns; j++) { strcat(str, TABLE_CELL); } strcat(str, ROW_CLOSE); write_string_to_disk(str); } write_string_to_disk(TABLE_CLOSE); return 0; } int generate_foto_gallery( char *p = ".", int max_per_page=20 ) { int entries=0; int processed=0; int row=1; int column=1; int page=1; char str[256]; if (dir=opendir(p)) { rewinddir(dir); while (de=readdir(dir)) { if (strcmp(de->d_name, ".")) if (strcmp(de->d_name, "..")) if (strcmp(de->d_name, filename)) { entries++; } } if (entries>max_per_page) { /* Hier document - dateiname schliessen */ /* Ausgabedateien -gallery1..n.html generieren */ fclose(document); sprintf(str,"gallerie%i.html", page); document=fopen(str,"w"); write_document_top(); } rewinddir(dir); if (entries>0) { write_string_to_disk("\t\t\n"); while (de=readdir(dir)) { processed++; if (strcmp(de->d_name, ".")) if (strcmp(de->d_name, "..")) if (strcmp(de->d_name, filename)) { if (column==1) { write_string_to_disk("\t\t\t\n"); } write_string_to_disk("\t\t\t\t\n"); column++; if (column>5) { write_string_to_disk("\t\t\t\n"); column=1; row++; } } if (processed>max_per_page) { if (column!=1) write_string_to_disk("\t\t\t\n"); write_string_to_disk("\t\t
d_name); write_string_to_disk("\");>
\n"); write_newline(); write_document_bottom(); fclose(document); page++; sprintf(str,"gallerie%i.html", page); document=fopen(str,"w"); write_document_top(); write_string_to_disk("\t\t\n"); column=1;row=1;processed=0; } } if (column!=1) write_string_to_disk("\t\t\t\n"); write_string_to_disk("\t\t
\n"); write_newline(); if (entries>max_per_page) { write_document_bottom(); fclose(document); document=fopen(filename, "a"); write_string_to_disk("\t\t\t

Foto Gallerie

\n"); write_string_to_disk("\t\t\tGeneriert mit mkhtml 0.0.1-4
\n"); for (int p = 1; p<=page; p++) { sprintf(str, "\t\t\tGallerie %i
\n", p, p); write_string_to_disk(str); } } } closedir(dir); } else { return 0; } } int generate_list(char list_type='u') { if (list_type=='o') write_string_to_disk("\t\t
    \n"); else { write_string_to_disk("\t\t
      \n"); } for (int i=1; i<11; i++) { write_string_to_disk("\t\t\t
    • \n"); } if (list_type=='o') write_string_to_disk("\t\t
\n"); else { write_string_to_disk("\t\t\n"); } } /* mkhtml is now split in three parts, used for multipagemode (e.g. now in the gallery) */ int write_document_top() { write_string_to_disk(HTML_OPEN); write_string_to_disk(HEAD_OPEN); if (!o_notitle) write_string_to_disk(TITLE); if (!o_nometa) write_string_to_disk(META); write_string_to_disk(HEAD_CLOSE); if (!o_nocss) { write_string_to_disk(CSS_OPEN); if (!o_emptytags) write_string_to_disk(CSS_CONTENT); write_string_to_disk(CSS_CLOSE); } if (!o_noscr) { write_string_to_disk(SCRIPT_OPEN); if (!o_emptytags) write_string_to_disk(SCRIPT_CONTENT); write_string_to_disk(SCRIPT_CLOSE); } write_string_to_disk(BODY_OPEN); if (!o_emptytags) write_string_to_disk(BODY_CONTENT_A); } int write_document_bottom() { if (!o_emptytags) write_string_to_disk(BODY_CONTENT_B); write_string_to_disk(BODY_CLOSE); write_string_to_disk(HTML_CLOSE); } int write_document() { fprintf(stdout, "writing: %s...", filename); document=fopen(filename, "w"); write_document_top(); if (o_diridx) { generate_directory_index(); write_newline(); } if (o_table) { generate_table(); write_newline(); } if (o_gallery) { generate_foto_gallery(); write_newline(); } if (o_list) { generate_list(); write_newline(); } write_document_bottom(); fclose(document); fprintf(stdout, "written!\n"); } /* frameset Section ********************************************************************************************************************************************************************************************/ int write_frameset() { /* Schreibe frameset-datei - schreibe char * FRAMESET_OPEN, _CLOSE "defines" wie fuers document */ /* Schreibe Navigations-Datei - directory index to anchor - target setzen - klick oeffnet verzeichnis im target fenster */ /* Schreibe Default-Angezeigte-Datei - dazu reicht die write document function */ } /* Main ********************************************************************************************************************************************************************************************************/ int main(int argc, char *argv[]) { int i; parse_options(argc,argv); if (o_help) { return help(argv[0]); } else if (o_version) { return version(); } else if (o_frameset) { write_frameset(); } else write_document(); return 1; } /* History: v0.0.1-1: html_definitions, write_document(), all (head, title, meta, script, style, body) v0.0.1-2: getopt.h to use command line parameters to turn certain tags on and of v0.0.1-3: directory index, table generator, simple foto gallery generation, list generator v0.0.1-4: mkhtml generiert automatisch mehrere gallerie dateien bei erreichen der maximalen bilderzahl Open: Mehr Dynamik durch Breiten, Groessen, als Kommandozeilenparameter uebergeben im Programm verwertet */