author | Olivier Brunel
<jjk@jjacky.com> 2022-12-28 08:55:52 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2022-12-28 08:55:52 UTC |
parent | b2c06ef4c1384ded9f7638bc97e4185e570752f9 |
main.c | +17 | -34 |
diff --git a/main.c b/main.c index 8a32d45..f2c7736 100644 --- a/main.c +++ b/main.c @@ -57,12 +57,7 @@ struct page { enum { DOC_HAS_TITLE = (1 << 0), -}; - -enum { - BUF_OFF = 0, - BUF_WAITING, - BUF_ON + DOC_BUFFERING = (1 << 1), }; struct ctx { @@ -82,7 +77,6 @@ struct ctx { struct { stralloc sa; size_t salen; - int state; } buf; union { struct { @@ -108,18 +102,7 @@ enum { static int raw_text(struct ctx *ctx, const char *text, size_t size) { - stralloc *sa = &ctx->sa_out; - - switch (ctx->buf.state) { - case BUF_WAITING: - ctx->buf.salen = ctx->buf.sa.len; - ctx->buf.state = BUF_ON; - /* fall through */ - case BUF_ON: - sa = &ctx->buf.sa; - break; - } - + stralloc *sa = (ctx->doc.flags & DOC_BUFFERING) ? &ctx->buf.sa : &ctx->sa_out; return stralloc_catb(sa, text, size); } @@ -311,13 +294,13 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) ctx->otoc = ctx->sa_out.len; /* open it */ - ctx->buf.state = BUF_ON; + ctx->doc.flags |= DOC_BUFFERING; if (!raw_str(ctx, "<ul>")) { - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; return ERR_PARSER_TOC; } ctx->toc_lvl = 1; - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; } } #undef str_title @@ -360,9 +343,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) if ((ctx->doc.flags & DOC_HAS_TITLE) && !raw_str(ctx, "</section>")) return ERR_PARSER_ENTER_BLOCK; - ctx->doc.flags |= DOC_HAS_TITLE; - - ctx->buf.state = BUF_ON; + ctx->doc.flags |= DOC_HAS_TITLE | DOC_BUFFERING; /* TOC */ for ( ; ctx->toc_lvl < d->level; ++ctx->toc_lvl) { @@ -427,10 +408,12 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) } } - if (ctx->code.flags & CODE_BUFFERED) - ctx->buf.state = BUF_WAITING; - else + if (ctx->code.flags & CODE_BUFFERED) { + ctx->doc.flags |= DOC_BUFFERING; + ctx->buf.salen = ctx->buf.sa.len; + } else { return (raw_str(ctx, "<pre>")) ? 0 : ERR_PARSER_ENTER_BLOCK; + } } } break; @@ -499,7 +482,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) const char *s = ctx->buf.sa.s + ctx->buf.salen; size_t l = ctx->buf.sa.len - ctx->buf.salen; ctx->buf.sa.len = ctx->buf.salen; - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; char buf[UINT32_FMT]; buf[uint32_fmt(buf, (uint32) ctx->title.level)] = '\0'; @@ -521,14 +504,14 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) /* TOC */ char toc[l]; memcpy(toc, s, l); - ctx->buf.state = BUF_ON; + ctx->doc.flags |= DOC_BUFFERING; if (!raw_str(ctx, "<li><a href=\"#") || !anchor(ctx, toc, l) || !raw_str(ctx, "\">") || !strip_tags(ctx, toc, l) || !raw_str(ctx, "</a></li>")) return ERR_PARSER_TOC; - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; } break; @@ -542,7 +525,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) const char *buf = ctx->buf.sa.s + ctx->buf.salen; size_t blen = ctx->buf.sa.len - ctx->buf.salen; ctx->buf.sa.len = ctx->buf.salen; - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; if (ctx->code.flags & CODE_LINES) { const char *s = buf; @@ -826,7 +809,7 @@ convert_page(struct ctx *ctx, int fddest) } /* close TOC */ - ctx->buf.state = BUF_ON; + ctx->doc.flags |= DOC_BUFFERING; for ( ; ctx->toc_lvl > 0; --ctx->toc_lvl) { if (!raw_str(ctx, "</ul>")) { char buf[UINT32_FMT]; @@ -834,7 +817,7 @@ convert_page(struct ctx *ctx, int fddest) ret_strerr_warnwu2x(ERR_PARSER, "parser internal error ", buf); } } - ctx->buf.state = BUF_OFF; + ctx->doc.flags &= ~DOC_BUFFERING; /* write output : */ if ( /* up to TOC position */