commit 800c45422ea9c0ea8b9287c98f86b075c1487adb
parent d11584ca59a9ca8ebf7a00064c7d694b1b1fdc85
Author: gearsix <gearsix@tuta.io>
Date: Sat, 6 Nov 2021 15:23:12 +0000
created cmark();
Diffstat:
M | mdoc.c | | | 27 | +++++++++++++++------------ |
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/mdoc.c b/mdoc.c
@@ -1,8 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+
#include <cmark.h>
+
void usage()
{
puts("usage: ./mdoc TITLE AUTHOR < file.md > file.html\n");
@@ -14,9 +16,9 @@ void printhead(const char *title, const char *author)
puts("<html lang=\"en\">");
puts("<head>");
puts(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
- if (title)
+ if (title && strlen(title) > 0)
printf(" <title>%s</title>\n", title);
- if (author)
+ if (title && strlen(author) > 0)
printf(" <meta name=\"author\">%s</meta>\n", title);
puts(" <style>");
puts(" body {");
@@ -38,6 +40,16 @@ void printfoot()
puts("</html>");
}
+size_t cmark(const size_t bufsiz, 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);
+ fprintf(out, "%s", html);
+ free(html);
+ return n;
+}
+
int main(int argc, char *argv[])
{
if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--usage") == 0)) {
@@ -62,15 +74,6 @@ int main(int argc, char *argv[])
}
printhead(title, author);
-
- int n = 0;
- char buf[BUFSIZ];
- do {
- n = fread(buf, sizeof(char), BUFSIZ, stdin);
- char *html = cmark_markdown_to_html(buf, strlen(buf), 0);
- printf("%s", html);
- free(html);
- } while (n == BUFSIZ);
-
+ while (cmark(BUFSIZ, stdin, stdout) == BUFSIZ);
printfoot();
}