From 2175b23500ebeba9a4844344df47d581f4f43897 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 6 May 2024 13:55:03 -0500 Subject: Initial --- jimmy | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100755 jimmy diff --git a/jimmy b/jimmy new file mode 100755 index 0000000..6e7c244 --- /dev/null +++ b/jimmy @@ -0,0 +1,151 @@ +#!/bin/sh +{ dummy=":" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- + +#: # h1 +#: ## h2 +#: ### h3 +#: => link +#: * list +#: > quote +#: ``` ... ``` verbatim + +BEGIN { # configuration + if (!to) to = "html" # default: html + + if (to == "html") { + 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 + 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 + opener["list"] = "\n" + linefmt["list"] = "
  • %s
  • \n" + opener["linklist"] = "\n" + linefmt["linklist"] = "
  • %s
  • \n" + } 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" + opener["paragraph"] = "" + closer["paragraph"] = "" + 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" + closer["list"] = "" + linefmt["list"] = "* %s\n" + opener["linklist"] = "\n" + closer["linklist"] = "" + linefmt["linklist"] = "=> %s %s\n" + } else die("Unknown `to' type: `" to "'") +} + +/^```/ { + bl = BLOCK + close_block() + if (bl != "verbatim") BLOCK = "verbatim" + bl = "" + next +} + +BLOCK == "verbatim" { + for (s in verbatim_esc) gsub(verbatim_esc[s], verbatim_repl[s]) + bufpush($0 "\n") + next +} + +/^#/ { + close_block() + match($0, /^#+/) + bufpush(sprintf(headerfmt[RLENGTH], collect(2))) + BLOCK = "header" + next +} + +/^=>/ { + if (BLOCK == "paragraph" && !link_block) + bufpush(sprintf(linkfmt, $2, collect(3))) + if (BLOCK != "linklist") close_block() + BLOCK = "linklist" + bufpush(sprintf(linefmt[BLOCK], $2, collect(3))) + next +} + +/^\*/ { + if (BLOCK != "list") close_block() + BLOCK = "list" + bufpush(sprintf(linefmt[BLOCK], collect(2))) + next +} + +/^>/ { + if (BLOCK != "quote") close_block() + BLOCK = "quote" + bufpush(sprintf(linefmt[BLOCK], collect(2))) + next +} + +/^$/ { + if (BLOCK == "verbatim") bufpush("\n") + else close_block() + next +} + +{ + if (BLOCK != "paragraph") close_block() + BLOCK = "paragraph" + bufpush(sprintf(linefmt[BLOCK], $0)) + next +} + +END { close_block(); printf "\n" } + +function close_block () { + if (!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]) + BUFFER = BLOCK = "" +} + +function collect (begin, end, out) { + for (f = (begin?begin:1); f <= (end?end:NF); f++) + out = out (out?" ":"") $f + return out +} + +function bufpush (str) { + BUFFER = BUFFER str +} -- cgit 1.4.1-21-gabe81