mdoc

Generates a fully-formed HTML document from Markdown using cmark.
git clone git://src.gearsix.net/mdoc
Log | Files | Refs | Atom | README

commit d11584ca59a9ca8ebf7a00064c7d694b1b1fdc85
Author: gearsix <gearsix@tuta.io>
Date:   Sat,  6 Nov 2021 13:40:28 +0000

init commit

Diffstat:
AMakefile | 2++
AREADME | 14++++++++++++++
Alibcmark.so.0.30.2 | 0
Amdoc.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest.md | 4++++
5 files changed, 96 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,2 @@ +all: + ${CC} -Wall -lcmark -o mdoc mdoc.c diff --git a/README b/README @@ -0,0 +1,14 @@ + +mdoc + +A very basic wrapper around *cmark* that adds text need to build +a fully-formed HTML file. It also adds some minimal CSS styling. + + ./mdoc [-t TITLE] [-a AUTHOR] < file.md > file.html + +It uses `stdin` to read input data, so you'll need to pipe the +Markdown text to it (e.g. shown above). Did it like thie because +this is a quick & dirty tool. + +- gearsix + diff --git a/libcmark.so.0.30.2 b/libcmark.so.0.30.2 Binary files differ. diff --git a/mdoc.c b/mdoc.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <cmark.h> + +void usage() +{ + puts("usage: ./mdoc TITLE AUTHOR < file.md > file.html\n"); +} + +void printhead(const char *title, const char *author) +{ + puts("<!DOCTYPE html>"); + puts("<html lang=\"en\">"); + puts("<head>"); + puts(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"); + if (title) + printf(" <title>%s</title>\n", title); + if (author) + printf(" <meta name=\"author\">%s</meta>\n", title); + puts(" <style>"); + puts(" body {"); + puts(" max-width: 37em;"); + puts(" font-family: arial, sans-serif;"); + puts(" text-align: justify;"); + puts(" }"); + puts(" h1 {"); + puts(" text-align: center;"); + puts(" }"); + puts(" </style>"); + puts("</head>"); + puts("<body>"); +} + +void printfoot() +{ + puts("</body>"); + puts("</html>"); +} + +int main(int argc, char *argv[]) +{ + if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--usage") == 0)) { + usage(); + exit(1); + } + + char *title = "", *author = ""; + for (char **arg = argv; !*arg; ++arg) { + if (*arg[0] == '-') continue; + switch (*arg[1]) { + case 'a': + author = *arg; + break; + case 't': + title = *arg; + break; + default: + usage(); + break; + } + } + + 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); + + printfoot(); +} diff --git a/test.md b/test.md @@ -0,0 +1,4 @@ +# heading + +Paragraph **bold**. +