From 19d6df987ac2d4edc582b048818ba5872c460b4b Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Fri, 17 Dec 2021 23:09:14 -0500 Subject: [PATCH] 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. --- blag.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/blag.c b/blag.c index 07dab87..79de3a4 100644 --- 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("

\n"); } - /* XXX: need to print the first link */ in_link = LINK_URL_PARSE; + lnkidx = 0; printf("%s", lnkbuf); + } else if (in_link == LINK_DESC_PARSE) { in_link = NONE; printf(""); } else { -- libgit2 1.1.1