diff --git a/generate.c b/generate.c index 7a294ae..ef73553 100644 --- a/generate.c +++ b/generate.c @@ -344,12 +344,12 @@ linkylinky(int image, MMIOT *f) if ( S(link.title) ) { fprintf(f->out, " title=\""); - reparse(T(link.title), S(link.title), DENY_A|DENY_IMG|EXPAND_QUOTE, f); + reparse(T(link.title), S(link.title), EMBEDDED, f); fputc('"', f->out); } if (image) { fprintf(f->out, " alt=\""); - reparse(T(link.tag), S(link.tag), DENY_A|DENY_IMG|EXPAND_QUOTE, f); + reparse(T(link.tag), S(link.tag), EMBEDDED, f); fputs("\" />", f->out); } else { @@ -404,6 +404,9 @@ maybe_tag_or_link(MMIOT *f) int c, size, i; int maybetag=1, maybeaddress=0; + if ( f->flags & EXPAND_BROKET ) + return 0; + for ( size=0; ((c = peek(f,size+1)) != '>') && !isspace(c); size++ ) { if ( ! (c == '/' || isalnum(c) || c == '~') ) maybetag=0; @@ -616,6 +619,11 @@ text(MMIOT *f) switch (c) { case 0: break; + case '>': if ( f->flags & EXPAND_BROKET ) + fprintf(f->out, ">"); + else + fputc(c, f->out); + break; case '"': if ( f->flags & EXPAND_QUOTE ) fprintf(f->out, """); else diff --git a/markdown.h b/markdown.h index 55e6413..f6ab4a3 100644 --- a/markdown.h +++ b/markdown.h @@ -54,7 +54,9 @@ typedef struct mmiot { #define DENY_IMG 0x0002 #define DENY_SMARTY 0x0004 #define EXPAND_QUOTE 0x0010 -#define DENY_MASK (DENY_A|DENY_IMG|DENY_SMARTY|EXPAND_QUOTE) +#define EXPAND_BROKET 0x0020 +#define DENY_MASK (DENY_A|DENY_IMG|DENY_SMARTY|EXPAND_QUOTE|EXPAND_BROKET) +#define EMBEDDED DENY_A|DENY_IMG|EXPAND_QUOTE|EXPAND_BROKET } MMIOT; diff --git a/mkdio.h b/mkdio.h index ab656e2..bbdbefb 100644 --- a/mkdio.h +++ b/mkdio.h @@ -38,6 +38,8 @@ extern char version[]; #define MKD_NOIMAGE 0x0002 /* don't do image processing, block */ #define MKD_NOPANTS 0x0004 /* don't run smartypants() */ #define MKD_QUOT 0x0010 /* expand " to " */ +#define MKD_BROKET 0x0020 /* expand < & > */ +#define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_QUOT|MKD_BROKET /* special flags for mkd_in() and mkd_string() */ diff --git a/theme.c b/theme.c index ba923fe..9cc3d2d 100644 --- a/theme.c +++ b/theme.c @@ -168,6 +168,19 @@ thesame(int *p, char *pat) } +static int +istag(int *p, char *pat) +{ + int c; + + if ( thesame(p, pat) ) { + c = peek(strlen(pat)+1); + return (c == '>' || isspace(c)); + } + return 0; +} + + /* spin() - run through the theme template, looking for ') ); } else if ( (peek(1) == '?') && thesame(cursor(), "?theme ") ) { @@ -204,27 +219,37 @@ spin(FILE *template, MMIOT doc, FILE *output) shift(-1); p = cursor(); + if ( intag ) + flags = MKD_EMBED; + else if ( inhead ) + flags = MKD_NOIMAGE|MKD_NOLINKS; + else + flags = 0; + if ( thesame(p, "title?>") ) { - if (( h = mkd_doc_title(doc) )) - mkd_text(h, strlen(h), stdout, 0); - else if ( source ) - mkd_text(source, strlen(source), stdout, 0); + if ( (h = mkd_doc_title(doc)) == 0 && source ) + h = source; + + if ( h ) + mkd_text(h, strlen(h), output, flags); } else if ( thesame(p, "date?>") ) { if (( h = mkd_doc_date(doc) )) - mkd_text(h, strlen(h), stdout, 0); + mkd_text(h, strlen(h), output, flags); } else if ( thesame(p, "author?>") ) { - if (( h = mkd_doc_author(doc) )) - mkd_text(h, strlen(h), stdout, 0); - else if ( me ) - mkd_text(me->pw_gecos, strlen(me->pw_gecos), stdout, 0); + if ( (h = mkd_doc_author(doc)) == 0 && me ) + h = me->pw_gecos; + + if ( h ) + mkd_text(h, strlen(h), output, flags); } else if ( thesame(p, "version?>") ) { - fwrite(version, strlen(version), 1, stdout); + fwrite(version, strlen(version), 1, output); } else if ( thesame(p, "body?>") ) { - mkd_generatehtml(doc,stdout); + if ( !(inhead||intag) ) + mkd_generatehtml(doc,output); } while ( (c = pull()) != EOF && (c != '?' && peek(1) != '>') ) @@ -232,10 +257,19 @@ spin(FILE *template, MMIOT doc, FILE *output) shift(1); } else - putchar(c); + putc(c, output); + + if ( istag(cursor(), "head") ) + inhead=1; + else if ( istag(cursor(), "body") ) + inhead=0; + intag=1; + continue; } - else - putchar(c); + else if ( c == '>' ) + intag=0; + + putc(c, output); } } /* spin */ @@ -249,7 +283,6 @@ char **argv; int opt; MMIOT doc; - if (( me = getpwuid(getuid()) )) { root = strdup(me->pw_dir); if ( !root ) @@ -278,11 +311,14 @@ char **argv; if ( argc > 0 ) { + int added_text=0; + source = alloca(strlen(argv[0]) + strlen(".text") + 1); strcpy(source,argv[0]); if ( !freopen(source, "r", stdin) ) { strcat(source, ".text"); + added_text = 1; if ( !freopen(source, "r", stdin) ) fail("can't open either %s or %s", argv[0], source); } @@ -290,20 +326,23 @@ char **argv; if ( !output ) { char *p, *q; output = alloca(strlen(source) + strlen(".html") + 1); - strcpy(output,source); - if ( strcmp(source, argv[0]) == 0 ) { + if ( added_text ) { + strcpy(output, argv[0]); + strcat(output, ".html"); + } + else { + strcpy(output, source); + if (( p = strchr(output, '/') )) q = strrchr(p+1, '.'); else q = strrchr(output, '.'); - } - else q = 0; - if ( q ) - strcpy(q, ".html"); - else - strcat(output, ".html"); + if ( q ) + *q = 0; + strcat(q, ".html"); + } } if ( !freopen(output, "w", stdout) ) fail("can't write to %s", output);