From eb4048f4d179f2329d77827831af06da52947de9 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Tue, 14 Dec 2021 00:35:12 -0500 Subject: [PATCH] Add initial version of blag, a simpler markup This is most definitely buggy, but a repo's gotta start somewhere! --- blag.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 blag.c diff --git a/blag.c b/blag.c new file mode 100644 index 0000000..08d968c --- /dev/null +++ b/blag.c @@ -0,0 +1,227 @@ +/* + * blag.c -- generate the blog! + * ~akarle, MIT License + * + * "because markdown isn't in base!" + * + * Support: + * -------- + * #|##|### headers + * + *

tags around paragraphs + * + * * bullets (non-nested) + * + * 1. numerical lists (non-nested) + * + * > quotes + * + * TAB code blocks + * + * Inline `code` + * + * TODO: + * [[url|Links]] + * -> if we see [[ + * -> print out + * -> just putchar + * -> else + * -> printf ">url + * -> once we hit ]] + * -> + * + * --> error reporting for unclosed links?? + * -> errx in closeblock() if in link parser + */ +#include +#include +#include + +enum Block { + NONE, + HEADER_PARSE, + HEADER, + PARAGRAPH, + PARAGRAPH_BREAK, + CODE, + CODE_BREAK, + QUOTE, + QUOTE_BREAK, + ULIST, + ULIST_START, + ULIST_PARSE, + ULIST_BREAK, + OLIST, + OLIST_START, + OLIST_PARSE, + OLIST_BREAK, +}; + +int closeblock(int in, int hlvl) { + if (in == HEADER) { + in = NONE; + printf("", hlvl); + } else if (in == PARAGRAPH) { + in = PARAGRAPH_BREAK; + } else if (in == PARAGRAPH_BREAK) { + in = NONE; + printf("

\n"); + } else if (in == CODE) { + in = CODE_BREAK; + } else if (in == CODE_BREAK) { + in = NONE; + printf("\n"); + } else if (in == QUOTE) { + in = QUOTE_BREAK; + } else if (in == QUOTE_BREAK) { + in = NONE; + printf("\n"); + } else if (in == ULIST) { + in = ULIST_BREAK; + printf(""); + } else if (in == ULIST_BREAK) { + in = NONE; + printf("\n"); + } else if (in == OLIST) { + in = OLIST_BREAK; + printf(""); + } else if (in == OLIST_BREAK) { + in = NONE; + printf("\n"); + } else { + /* keep in as is */ + } + return in; +} + +int parse() { + /* Mini state machine */ + int c; + enum Block in = NONE; + int hlvl = 0; + int in_code; + while ((c = getchar()) != EOF) { + switch (c) { + case '#': + if (in == NONE) { + in = HEADER_PARSE; + hlvl = 1; + } else if (in == HEADER_PARSE) { + hlvl++; + } else { + /* not a special # */ + putchar(c); + } + break; + case ' ': + if (in == HEADER_PARSE) { + printf("", hlvl); + in = HEADER; + } else if (in == ULIST_START) { + printf("