💾 Archived View for celehner.com › gemini-utils › gmi2html captured on 2023-04-19 at 22:33:29.

View Raw

More Information

⬅️ Previous capture (2020-10-31)

-=-=-=-=-=-=-

#!/usr/bin/awk -f
# gmi2html
# Copyright 2020 Charles E. lehner
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

function escape_html(str) {
	gsub(/&/, "\\&", str)
	gsub(/</, "\\&lt;", str)
	gsub(/>/, "\\&gt;", str)
	return str
}

BEGIN {
	print "<!doctype html><body><style>"\
		"h1, h2, h3 { margin: 0; }\n"\
		"</style>"
}

{
	sub(/\r$/, "")
}

/^```/ {
	if (!in_literal) {
		in_literal = 1
		print "<pre>"
	} else {
		in_literal = 0
		print "</pre>"
	}
	next
}

in_literal {
	print escape_html($0)
	next
}

in_list && !/^\*/ {
	in_list = 0
	print "</ul>"
}

/^\*/ {
	if (!in_list) {
		in_list = 1
		printf "<ul>"
	}
	match($0, /^\*+\s*/)
	text = substr($0, RLENGTH+1)
	printf "<li>%s</li>\n", escape_html(text)
	next
}

/^#+/ {
	match($0, /^#+/)
	tag = "h" RLENGTH
	match($0, /^(#+\s*)/)
	text = substr($0, RLENGTH+1)
	html = escape_html(text)
	printf "<%s style=\"font:sans-serif\">%s</%s>\n", tag, html, tag
	next
}

/^>/ {
	match($0, /^>+\s*/)
	text = substr($0, RLENGTH+1)
	printf "<blockquote>%s</blockquote>\n", escape_html(text)
	next
}

/^=>/ {
	match($0, /^=>\s*/)
	href = substr($0, RLENGTH+1)
	if (match(href, /^\S+/)) {
		text = substr(href, RLENGTH+2)
		href = substr(href, 0, RLENGTH)
	}
	match($0, /^=>\s+/)
	prefix = substr($0, 3, RLENGTH-3)
	match(text, /^\s+/)
	text = substr(text, RLENGTH+1)
	sub(/:1965/, "", href)
	if (!text) {
		text = href
	}
	if (match(href, /^gemini:\/\/[^/]+$/)) {
		href = href "/"
	}
	sub(/^\t+/, "", prefix)
	html = escape_html(text)
	printf "<div>%s<a href=\"%s\">%s</a></div>\n", prefix, href, html
	next
}

/^$/ {
	print "<br>"
	next
}

{
	printf "<div>%s</div>\n", escape_html($0)
}

END {
	print "</body>"
}