From 71d543bedde36b3a05d41cb5613f37710832adc2 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 6 May 2024 17:28:56 -0500 Subject: Rejigger variables --- jimmy | 134 ++++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 40 deletions(-) diff --git a/jimmy b/jimmy index 6e7c244..74cef29 100755 --- a/jimmy +++ b/jimmy @@ -13,59 +13,81 @@ BEGIN { # configuration if (!to) to = "html" # default: html if (to == "html") { + # blocks + isblock["paragraph"] = 1 + opener["paragraph"] = "

" + closer["paragraph"] = "

\n" + linefmt["paragraph"] = "%s\n" + isblock["verbatim"] = 1 opener["verbatim"] = "
"
 		closer["verbatim"] = "
\n" linefmt["verbatim"] = "%s\n" - verbatim_esc[1] = "&" - verbatim_repl[1] = "&" - verbatim_esc[2] = "<" - verbatim_repl[2] = "<" - verbatim_esc[3] = ">" - verbatim_repl[3] = ">" - headerfmt[1] = "

%s

\n" - headerfmt[2] = "

%s

\n" - headerfmt[3] = "

%s

\n" - linkfmt = "%s\n" - link_block = 0 + isblock["quote"] = 1 opener["quote"] = "
" closer["quote"] = "
\n" linefmt["quote"] = "%s\n" - opener["paragraph"] = "

" - closer["paragraph"] = "

\n" - linefmt["paragraph"] = "%s\n" - paragraph_collapse = 0 # turn \n to ' ' in paragraphs - # consider: paragraph_fold-- , 0 (do nothing), -1 (collapse) - collapse_blanks = 1 + isblock["list"] = 1 opener["list"] = "\n" linefmt["list"] = "
  • %s
  • \n" + isblock["linklist"] = 1 opener["linklist"] = "\n" linefmt["linklist"] = "
  • %s
  • \n" + # lines + isblock["header"] = 1 + linefmt["header", 1] = "

    %s

    \n" + linefmt["header", 2] = "

    %s

    \n" + linefmt["header", 3] = "

    %s

    \n" + isblock["link"] = 0 + linefmt["link"] = "%s\n" + # escapes + esc["verbatim", 0, "&"] = "\\&" + esc["verbatim", 8, "<"] = "\\<" + esc["verbatim", 9, ">"] = "\\>" + # fill -- # fill[BLOCK] can be a width, 0, or -1. + ## 0 => do nothing . -1 => all one line + fill["paragraph"] = 0 + fill["quote"] = 0 + # collapse whitespace + collapse_blanks = 1 + # collapse_blanks is a boolean: collapse >1 blank lines? } else if (to == "gemini") { - opener["verbatim"] = "```\n" - closer["verbatim"] = "```" - linefmt["verbatim"] = "%s\n" - headerfmt[1] = "# %s\n" - headerfmt[2] = "## %s\n" - headerfmt[3] = "### %s\n" - linkfmt = "=> %s %s\n" - link_block = 1 - opener["quote"] = "" - closer["quote"] = "" - linefmt["quote"] = "> %s\n" + # blocks + isblock["paragraph"] = 1 opener["paragraph"] = "" - closer["paragraph"] = "" + closer["paragraph"] = "\n" linefmt["paragraph"] = "%s\n" - paragraph_collapse = 1 # turn \n to ' ' in paragraphs - # consider: paragraph_fold-- , 0 (do nothing), -1 (collapse) - collapse_blanks = 0 - opener["list"] = "\n" + isblock["verbatim"] = 1 + opener["verbatim"] = "```\n" + closer["verbatim"] = "```\n" + linefmt["verbatim"] = "%s\n" + isblock["quote"] = 1 + opener["quote"] = "> " + closer["quote"] = "\n" + linefmt["quote"] = "%s" + isblock["list"] = 1 + opener["list"] = "" closer["list"] = "" linefmt["list"] = "* %s\n" - opener["linklist"] = "\n" + isblock["linklist"] = 1 + opener["linklist"] = "" closer["linklist"] = "" linefmt["linklist"] = "=> %s %s\n" + # lines + isblock["header"] = 1 + linefmt["header", 1] = "# %s\n" + linefmt["header", 2] = "## %s\n" + linefmt["header", 3] = "### %s\n" + isblock["link"] = 0 + linefmt["link"] = "@NL@=> %s %s@NL@" + # escapes + # fill -- # fill[BLOCK] can be a width, 0, or -1. + ## 0 => do nothing . -1 => all one line + fill["paragraph"] = -1 + fill["quote"] = -1 + # collapse whitespace + collapse_blanks = 1 } else die("Unknown `to' type: `" to "'") } @@ -86,14 +108,16 @@ BLOCK == "verbatim" { /^#/ { close_block() match($0, /^#+/) - bufpush(sprintf(headerfmt[RLENGTH], collect(2))) + bufpush(sprintf(linefmt["header", RLENGTH], collect(2))) BLOCK = "header" next } /^=>/ { - if (BLOCK == "paragraph" && !link_block) - bufpush(sprintf(linkfmt, $2, collect(3))) + if (BLOCK == "paragraph" && !isblock["link"]) { + bufpush(sprintf(linefmt["link"], $2, collect(3))) + next + } if (BLOCK != "linklist") close_block() BLOCK = "linklist" bufpush(sprintf(linefmt[BLOCK], $2, collect(3))) @@ -134,9 +158,17 @@ function close_block () { if (collapse_blanks) return else printf "\n" } - if (BLOCK == "paragraph" && paragraph_collapse) - gsub(/\n/, " ", BUFFER) - printf("%s%s%s", opener[BLOCK], BUFFER, closer[BLOCK]) + + if (!isblock[BLOCK]) return + if (fill[BLOCK]) BUFFER = buffill(BUFFER, fill[BLOCK]) + + for (e in esc) { + if (BLOCK && match(e, "^" BLOCK ".[0-9]")) { + gsub(substr(e, RLENGTH+2), esc[e], BUFFER) + } + } + + printf("%s%s%s\n", opener[BLOCK], BUFFER, closer[BLOCK]) BUFFER = BLOCK = "" } @@ -149,3 +181,25 @@ function collect (begin, end, out) { function bufpush (str) { BUFFER = BUFFER str } + +function buffill(buf, width, out) { + if (width < 0) { + gsub("\n", " ", buf) + out = buf + } + else { + split(buf, arr) + nline = 0 + for (w=1;w<=length(arr);w++) { + if (nline + length(arr[w]) >= width) { + out = out (out?"\n":"") arr[w] + nline = length(arr[w]) + } else { + out = out (out?" ":"") arr[w] + nline += length(arr[w]) + 1 + } + } + } + gsub("@NL@", "\n", out) + return out +} -- cgit 1.4.1-21-gabe81