Another `text/gemini` to html translator, but this time it's AWK!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/gawk -f | |
BEGIN{ | |
# Printing header! | |
print "<!DOCTYPE html>\n\ | |
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"\" xml:lang=\"\">\n\ | |
<head>\n\ | |
<meta charset=\"utf-8\" />\n\ | |
<style type=\"text/css\">\n\ | |
body{\n\ | |
margin:auto;\n\ | |
max-width:40em;\n\ | |
font-size: 150%;\n\ | |
}\n\ | |
code{font-family: monospace;}\n\ | |
</style>\n\ | |
</head>\n\ | |
<body>" | |
# Control variables | |
pre = 0 | |
list = 0 | |
} | |
# First we change all &, < and > into &, < and >. This will cause conflicts | |
# with links (=>) and quotes (>) but it will be easier to check only then than | |
# keep converting at each rule. | |
{ | |
gsub(/&/, "\\&") | |
gsub(/</, "\\<") | |
gsub(/>/, "\\>") | |
} | |
/^```/&&(pre == 0){ | |
# We must close the list! | |
if(list == 1){ | |
list = 0 | |
print "</ul>" | |
} | |
pre = 1 | |
printf "<pre><code>" | |
next | |
} | |
/^```/&&(pre == 1){ | |
pre = 0 | |
print "</code></pre>" | |
next | |
} | |
(pre == 1){ | |
print $0 | |
next | |
} | |
/\* /{ | |
if(list == 0){ | |
list = 1 | |
print "<ul>" | |
} | |
sub(/\* [ \t]*/, "") | |
print "<li>"$0"</li>" | |
next | |
} | |
# If the list has ended | |
(list == 1){ | |
list = 0 | |
print "</ul>" | |
} | |
/^---[-]*[ \t]*$/{ | |
print "<hr/>" | |
next | |
} | |
/^[ \t]*$/{ | |
print "<br/>" | |
next | |
} | |
/^###/{ | |
sub(/^#[#]*[ \t]*/, "") | |
print "<h3>"$0"</h3>" | |
next | |
} | |
/^##/{ | |
sub(/^#[#]*[ \t]*/, "") | |
print "<h2>"$0"</h2>" | |
next | |
} | |
/^#/{ | |
sub(/^#[#]*[ \t]*/, "") | |
print "<h1>"$0"</h1>" | |
next | |
} | |
/^>/{ | |
sub(/^>[ \t]*/, "") | |
print "<blockquote><p>> "$0"</p></blockquote>" | |
next | |
} | |
/^=>/{ | |
sub(/^=>[ \t]*/, "") | |
url=$0 | |
sub(/[ \t].*$/, "", url) | |
text=$0 | |
sub(/^[^ \t]*/, "", text) | |
sub(/^[ \t]*/, "", text) | |
sub(/[ \t]*$/, "", text) | |
# If it's a local gemini file, link to the html: | |
if((url !~ /^[a-zA-Z]*:\/\//) && ((url ~ /\.gmi$/) || (url ~ /\.gemini$/))){ | |
sub(/\.gmi$/, ".html", url) | |
sub(/\.gemini$/, ".html", url) | |
} | |
if(text == ""){ | |
text = url | |
} | |
print "<p><a href=\""url"\">"text"</a></p>" | |
next | |
} | |
{ | |
print "<p>"$0"</p>" | |
} | |
END{ | |
# Closes open list | |
if(list == 1){ | |
print "</ul>" | |
} | |
print "</body>\n</html>" | |
} |
Thanks!!! Fell free to use any license you want on it, I'm just happy that some people find any use for it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Excellent work!
I just wanted to let you know that I've been working on a fork of this code: https://codeberg.org/jhpotter/gmi2html-ng
I've licensed it under the MIT license, let me know if that works for you. And of course you are free to contribute to my repo and I can give you admin access as well.
Regards,
Jeremy