commit 4d7ec462e83363bbb02350148f79af4d7393c64b
parent bad2b5a6db4a42f90944730f58d729d12526b2b4
Author: gearsix <gearsix@tuta.io>
Date: Thu, 25 Aug 2022 17:35:44 +0100
updated to use cmark stream interface
This avoids documents getting cut-up with end tags in the middle of
blocks when the input is longer than the bufsiz.
It should also reduce the number of calls made to `cmark` to 1 per
document.
Diffstat:
M | mdoc.c | | | 19 | ++++++++++--------- |
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/mdoc.c b/mdoc.c
@@ -38,17 +38,17 @@ void printhead(const char *title, const char *author)
void printfoot()
{
puts("</body>");
- puts("</html>");
+ puts("</html>\0");
}
-size_t cmark(const size_t bufsiz, FILE *in, FILE *out)
+void cmark(FILE *in, FILE *out)
{
- char buf[bufsiz];
- size_t n = fread(buf, sizeof(char), BUFSIZ, in);
- char *html = cmark_markdown_to_html(buf, strlen(buf), 0);
+ const int opts = (CMARK_OPT_UNSAFE|CMARK_OPT_VALIDATE_UTF8);
+ cmark_node *n = cmark_parse_file(in, opts);
+ char *html = cmark_render_html(n, opts);
fprintf(out, "%s", html);
+ cmark_node_free(n);
free(html);
- return n;
}
void cleanexit() {
@@ -79,10 +79,11 @@ int main(int argc, char *argv[])
}
}
- void (*sig)(int) = 0;
- sig = signal(SIGINT, cleanexit);
+ signal(SIGINT, cleanexit);
+
+ setvbuf(stdout, NULL, _IONBF, 0);
printhead(title, author);
- while (!sig && cmark(BUFSIZ, stdin, stdout) == BUFSIZ);
+ cmark(stdin, stdout);
printfoot();
}