mirror of
https://github.com/Orc/discount.git
synced 2026-09-25 04:10:00 +03:00
1. Add the new flag MKD_BROKET, which tells markdown to expand all text <>s.
2. define the flag `MKD_EMBED`, which turns on `<>` and `"`expansion. 3. modify `theme` so it keeps track of whether it's inside html tags and whether it's in the header or body of a page.
This commit is contained in:
+10
-2
@@ -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
|
||||
|
||||
+3
-1
@@ -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;
|
||||
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ extern char version[];
|
||||
#define MKD_NOIMAGE 0x0002 /* don't do image processing, block <img> */
|
||||
#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()
|
||||
*/
|
||||
|
||||
@@ -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 <?theme expansions
|
||||
*
|
||||
* theme expansions we love:
|
||||
@@ -183,16 +196,18 @@ spin(FILE *template, MMIOT doc, FILE *output)
|
||||
int c;
|
||||
int *p;
|
||||
char *h;
|
||||
int flags;
|
||||
int intag=0, inhead=0;
|
||||
|
||||
prepare(template);
|
||||
|
||||
while ( (c = pull()) != EOF ) {
|
||||
if ( c == '<' ) {
|
||||
if ( peek(1) == '!' && peek(2) == '-' && peek(3) == '-' ) {
|
||||
fputs("<!--", stdout);
|
||||
fputs("<!--", output);
|
||||
shift(3);
|
||||
do {
|
||||
putchar(c);
|
||||
putc(c, output);
|
||||
} while ( ! (c == '-' && peek(1) == '-' && peek(2) == '>') );
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user