nihdoc

WIP markup parser (txt -> html)
git clone git://git.alexkarle.com.com/blag
Log | Files | Refs | README | LICENSE

commit 19d6df987ac2d4edc582b048818ba5872c460b4b (patch)
parent bd43c075db3d89805f5c102e02f2bc854a9d9354
Author: Alex Karle <alex@alexkarle.com>
Date:   Fri, 17 Dec 2021 23:09:14 -0500

Add support for simple no-description links [link]

I was on the fence about this feature, since it means that we need to
store the contents of the link in memory as we parse it (and I was so
proud of the minimal memory used!). I tell myself it's still O(1) if I
use a fixed-length buffer :) AND it makes the source much prettier to
not have to double the link if I want it shown that way.

Diffstat:
Mblag.c | 24+++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/blag.c b/blag.c @@ -22,7 +22,7 @@ * * Escaping via \ * - * [url Links] + * Links: [url desc] or [url] * * TODO: * ----- @@ -130,6 +130,9 @@ int parse() { int in_ital = 0; int in_bold = 0; int escape = 0; + char lnkbuf[2048] = {0}; + int lnkidx = 0; + while ((c = getchar()) != EOF) { /* Handle Escapes before all else */ if (escape) { @@ -141,6 +144,11 @@ int parse() { escape = 0; continue; } + + /* Store links as we go */ + if (in_link == LINK_URL_PARSE && c != ']') { + lnkbuf[lnkidx++] = c; + } /* Handle unique state changes by char */ switch (c) { @@ -247,24 +255,26 @@ int parse() { } break; case '[': - if (in_link == NONE) { + if (in_link == NONE && !in_code) { if (in == NONE) { /* Assume this is a new paragraph */ in = PARAGRAPH; printf("<p>\n"); } - /* XXX: need to print the first link */ in_link = LINK_URL_PARSE; + lnkidx = 0; printf("<a href=\""); - } else if (in_link == LINK_URL_PARSE) { - /* TODO: save to linkbuf to support [url] links? */ - putesc(c); } else { putesc(c); } break; case ']': - if (in_link == LINK_DESC_PARSE) { + if (in_link == LINK_URL_PARSE) { + /* no description */ + in_link = NONE; + lnkbuf[lnkidx] = '\0'; + printf("\">%s</a>", lnkbuf); + } else if (in_link == LINK_DESC_PARSE) { in_link = NONE; printf("</a>"); } else {