#define PROGRAM "mkhtml" #define VERSION "0.0.1-3" #include #include #include #include #include char filename[256] = "mkhtml.html"; char *filenames[256] = { "mkhtml.html" }; int files = 1; /* 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' } }; 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; 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 '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 mkhtml.html)\n" "--ofile filename - s.a.\n" "\n", arg0 ); return 0; } /* mkhtml Section */ FILE *document; char *HTML_OPEN = "\n"; char *HTML_CLOSE = "\n"; char *HEAD_OPEN = "\t\n"; char *HEAD_CLOSE = "\t\n"; char *SCRIPT_OPEN = "\t\n"; char *CSS_OPEN = "\t\n"; char *BODY_OPEN = "\t\n"; char *BODY_CLOSE = "\t\n"; char *TITLE = "\t\t\n"; char *META = "\t\t\n"; char *CSS_CONTENT = "\t\tbody {\n" "\t\t\tbackground:\n" "\t\t\tcolor:\n" "\t\t\tfont-size:\n" "\t\t\tfont-family:\n" "\t\t\tmargin:\n" "\t\t\tpadding:\n" "\t\t}\n" "\t\ta:hover {\n" "\t\t\tbackground:\n" "\t\t\tcolor:\n" "\t\t\t\n" "\t\t}\n" "\t\ta:active, a:link, a:visited {\n" "\t\t\tbackground:\n" "\t\t\tcolor:\n" "\t\t\t\n" "\t\t}\n" "\t\t/* temp {\n" "\t\t\tbackground:\n" "\t\t\tcolor:\n" "\t\t\tfont-size:\n" "\t\t\tfont-family:\n" "\t\t\tmargin:\n" "\t\t\tpadding:\n" "\t\t\tborder-style:\n" "\t\t\tborder-width:\n" "\t\t} */\n"; char *CSS_CONTENT_B = "\n"; char *BODY_CONTENT = "\n"; char *BODY_CONTENT_B = "\t\t
zurück
\n"; char *SCRIPT_CONTENT = "\t\twindow.status=\"\";\n"; char *SCRIPT_CONTENT_B = "\n"; 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=0, char *exclude=0) { char *p="."; char *ex=""; char str[2048]; DIR *dir; struct dirent *de; if ((path) || (exclude)) { if (path) strcpy(p,path); if (exclude) strcpy(ex, exclude); } if (dir=opendir(p)) { 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("\t\t\n"); for (int i = 1; i<=rows; i++) { memset(str, 0, sizeof(str)); strcat(str, "\t\t\t"); for (int j = 1; j<=columns; j++) { strcat(str, ""); } strcat(str, "\n"); write_string_to_disk(str); } write_string_to_disk("\t\t
\n"); return 0; } int generate_foto_gallery( char *p = "." ) { /* 50 Bilder die Seite */ DIR *dir; struct dirent *de; int entries=0; int row=1; int column=1; 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++; } } rewinddir(dir); if (entries>0) { write_string_to_disk("\t\t\n"); while (de=readdir(dir)) { 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 (column!=1) write_string_to_disk("\t\t\t\n"); write_string_to_disk("\t\t
\n"); } 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"); } } int write_document(char *doc) { /* generate the html document */ fprintf(stdout, "writing: %s...", doc); document=fopen(doc, "w"); 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); 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(); } if (!o_emptytags) write_string_to_disk(BODY_CONTENT_B); write_string_to_disk(BODY_CLOSE); write_string_to_disk(HTML_CLOSE); fclose(document); fprintf(stdout, "written!\n"); } /* 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 write_document(filename); 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 Open: real dynamic values for all tables and galleries. multipage-mode for the gallery (more than one file) */