diff --git a/cstring.h b/cstring.h index b4f68fe..af9a8ac 100644 --- a/cstring.h +++ b/cstring.h @@ -8,6 +8,7 @@ #define _CSTRING_D #include +#include /* expandable Pascal-style string. */ diff --git a/docheader.c b/docheader.c index 1c71c2f..aa8ff21 100644 --- a/docheader.c +++ b/docheader.c @@ -13,12 +13,13 @@ #include "cstring.h" #include "markdown.h" +#define afterdle(t) (T((t)->text) + (t)->dle) char * mkd_doc_title(Document *doc) { if ( doc && doc->headers ) - return T(doc->headers->text); + return afterdle(doc->headers); return 0; } @@ -27,7 +28,7 @@ char * mkd_doc_author(Document *doc) { if ( doc && doc->headers && doc->headers->next ) - return T(doc->headers->next->text); + return afterdle(doc->headers->next); return 0; } @@ -36,6 +37,6 @@ char * mkd_doc_date(Document *doc) { if ( doc && doc->headers && doc->headers->next && doc->headers->next->next ) - return T(doc->headers->next->next->text); + return afterdle(doc->headers->next->next); return 0; } diff --git a/mkd2html.c b/mkd2html.c index 43adb42..2ff022c 100644 --- a/mkd2html.c +++ b/mkd2html.c @@ -15,6 +15,10 @@ * be distributed with this source code. */ #include +#include +#include +#include +#include #include "config.h" @@ -23,10 +27,18 @@ extern char version[]; +char *pgm = "mkd2html"; + void -fail(char *why) +fail(char *why, ...) { - fprintf(stderr, "md2html: %s\n", why); + va_list ptr; + + va_start(ptr,why); + fprintf(stderr, "%s: ", pgm); + vfprintf(stderr, why, ptr); + fputc('\n', stderr); + va_end(ptr); exit(1); } @@ -36,47 +48,113 @@ main(argc, argv) char **argv; { char *h; + char *source = 0, *dest = 0; + void *mmiot; int i; - void *input; + FILE *input, *output; + STRING(char*) css, headers, footers; - if ( (input = mkd_in(stdin, 0)) == 0 ) - fail("can't read input"); - if ( !mkd_compile(input, 0) ) + CREATE(css); + CREATE(headers); + CREATE(footers); + pgm = basename(argv[0]); + + while ( argc > 2 ) { + if ( strcmp(argv[1], "-css") == 0 ) { + EXPAND(css) = argv[2]; + argc -= 2; + argv += 2; + } + else if ( strcmp(argv[1], "-header") == 0 ) { + EXPAND(headers) = argv[2]; + argc -= 2; + argv += 2; + } + else if ( strcmp(argv[1], "-footer") == 0 ) { + EXPAND(footers) = argv[2]; + argc -= 2; + argv += 2; + } + } + + + if ( argc > 1 ) { + char *p, *dot; + + source = alloca(strlen(argv[1]) + 6); + dest = alloca(strlen(argv[1]) + 6); + + strcpy(source, argv[1]); + if (( p = strrchr(source, '/') )) + p = source; + else + ++p; + + if ( (input = fopen(source, "r")) == 0 ) { + strcat(source, ".text"); + if ( (input = fopen(source, "r")) == 0 ) + fail("can't open either %s or %s", argv[1], source); + } + strcpy(dest, source); + + if (( dot = strrchr(dest, '.') )) + *dot = 0; + strcat(dest, ".html"); + + if ( (output = fopen(dest, "w")) == 0 ) + fail("can't write to %s", dest); + } + else { + input = stdin; + output = stdout; + } + + if ( (mmiot = mkd_in(input, 0)) == 0 ) + fail("can't read %s", source ? source : "stdin"); + + if ( !mkd_compile(mmiot, 0) ) fail("couldn't compile input"); - h = mkd_doc_title(input); + h = mkd_doc_title(mmiot); /* print a header */ - puts(""); - puts(""); - puts(""); - printf(" \n", version); - puts(" "); + fprintf(output, + "\n" + "\n" + "\n" + " \n", version); + + fprintf(output," "); + + for ( i=0; i < S(css); i++ ) + fprintf(output, " \n", T(css)[i]); + if ( h ) { - printf(" "); - mkd_text(h, strlen(h), stdout, 0); - puts(""); + fprintf(output," "); + mkd_text(h, strlen(h), output, 0); + fprintf(output, "\n"); } - for (i=1; (i < argc) && (strcmp(argv[i], "/") != 0); i++) - printf(" %s\n", argv[i]); - puts(""); - puts(""); + for ( i=0; i < S(headers); i++ ) + fprintf(output, " %s\n", T(headers)[i]); + fprintf(output, "\n" + "\n"); /* print the compiled body */ - mkd_generatehtml(input, stdout); - mkd_cleanup(input); + mkd_generatehtml(mmiot, output); - i++; - - while ( i < argc ) - puts(argv[i++]); - - puts(""); - puts(""); + for ( i=0; i < S(footers); i++ ) + fprintf(output, "%s\n", T(footers)[i]); + + fprintf(output, "\n" + "\n"); + + mkd_cleanup(mmiot); exit(0); } diff --git a/mkdio.c b/mkdio.c index 4c18bcc..25ceefa 100644 --- a/mkdio.c +++ b/mkdio.c @@ -68,6 +68,16 @@ queue(Document* a, Cstring *line) } +/* trim leading blanks from a header line + */ +static void +snip(Line *p) +{ + CLIP(p->text, 0, 1); + p->dle = mkd_firstnonblank(p); +} + + /* build a Document from any old input. */ typedef unsigned int (*getc_func)(void*); @@ -117,6 +127,9 @@ populate(getc_func getc, void* ctx, int flags) a->headers = T(a->content); T(a->content) = a->headers->next->next->next; a->headers->next->next->next = 0; + snip(a->headers); + snip(a->headers->next); + snip(a->headers->next->next); } #endif