#!/usr/bin/env -S 9 sed -f
# This is free and unencumbered software released into the public domain.
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
#     May you do good and not evil.
#     May you find forgiveness for yourself and forgive others.
#     May you share freely, never taking more than you give.

# inferno sed is not complete enough to execute this script

# preprocess links so they resolve to another proxy page?

#s,=>[ 	]*?gemini://,=>	/gemini/,g

# escape html

s/&/\&amp;/g
s/</\&lt;/g
s/>/\&gt;/g
s/\"/\&#34;/g
s/\'/\&#39;/g

# /A/,/B/ is sed syntax for "match a region marked by delimiters /A/
# and /B/ where A and B are regex expressions". working with delimiters
# is a little funky because sed normally only understands lines. symmetry
# breaking is really important to avoid weird behavior.
#
# {} applies a block of sed expressions to the selected pattern space, in
# this case, the region matched by the delimiters.

/^```/,/^```$/{
	s,^(.*)$,<code aria-hidden=true>\1</code>,g
}

s,^---+$,<hr />,g

# transform heading line types

s,^#[ 	]*?([^#].+)$,<a role=heading aria-level=1><span aria-hidden=true># </span>\1</a>,g
s,^##[ 	]*?([^#].+)$,<a role=heading aria-level=2><span aria-hidden=true>## </span>\1</a>,g
s,^###[ 	]*?([^#].+)$,<a role=heading aria-level=3><span aria-hidden=true>### </span>\1</a>,g

# transform link line types

s,^=&gt;[ 	]*?([^ 	]+)$,<a role=link href="\1"><span aria-hidden=true>=\&gt; </span>\1</a>,g
s,^=&gt;[ 	]*?([^ 	]+?)[ 	]+(.+)$,<a role=link href="\1"><span aria-hidden=true>=\&gt; </span>\2</a>,g

s,^<a (.+)>([0-9]+)-([0-9][0-9])-([0-9][0-9])[ 	]+-[ 	]+([^<]+)</a>$,<a role=link \1><time datetime=\2-\3-\4>\2-\3-\4</time> - \5</a>,g

# transform blockquote line type

s,^&gt;[ 	]*?(.+)$,<q>\1</q>,g

# transform list line type

s,^[*][ 	]*?(.+)$,<span role=listitem><span aria-hidden=true>\&#42; </span>\1</span>,g

# HTML PLUS
# some alt texts create invalid markup :eyes:

s,<a role=link href="([^ ]+?\.)(gif|jpe?g|png|svg|bmp)">.+</span>(.+)</a>,<img src="\1\2" alt="\3" title="\3">,g
s,<a role=link href="([^ ]+?\.)(mp4|webm|ogv)">.+</span>(.+)</a>,<video controls src="\1\2" title="\3"></video>,g
s,<a role=link href="([^ ]+?\.)(mp3|ogg|oga)">.+</span>(.+)</a>,<figure class=c-audio><audio controls src="\1\2"><a href="\1\2">download</a></audio><figcaption>\3</figcaption></figure>,g
s,<a role=link href="https:\/\/youtube\.com\/watch\?v=([^ ]+?)">.+</span>(.+)</a>,<iframe class=c-youtube-frame frameborder=0 src=":invidious-domain/embed/\1" title="\2" allowfullscreen></iframe>,g
s,<a role=link href="https:\/\/spectra\.video\/videos\/embed\/([^ ]+?)">.+</span>(.+)</a>,<iframe class=c-peertube-frame title="\2" src="https://spectra.video/videos/embed/\1" frameborder="0" allowfullscreen sandbox="allow-same-origin allow-scripts allow-popups"></iframe>,g

/^<code aria-hidden=true>```[^`]+<\/code>$/,/^<code aria-hidden=true>```<\/code>$/{
	s,^<code aria-hidden=true>```([a-z0-9]+)</code>$,<code role=figure aria-label="lang: \1" aria-hidden=true class="language-\1">,g
	s,^<code aria-hidden=true>```([^`]+)</code>$,<code role=figure aria-label="\1" aria-hidden=true>,g
	s,^<code aria-hidden=true>(.*)</code>$,\1,g
	s,^```$,</code>,g
}

