From c73f86fa6601593ea756a8ccdf0fbbf391183755 Mon Sep 17 00:00:00 2001
From: sejo
Date: Wed, 3 Nov 2021 16:41:30 -0600
Subject: [PATCH] html clean version
---
gemtext2html-clean.awk | 339 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 339 insertions(+)
create mode 100644 gemtext2html-clean.awk
diff --git a/gemtext2html-clean.awk b/gemtext2html-clean.awk
new file mode 100644
index 00000000..d4da2e53
--- /dev/null
+++ b/gemtext2html-clean.awk
@@ -0,0 +1,339 @@
+# gemtext2html-clean
+# convierte un archivo en gemtext a html de acuerdo a la spec
+# excepción: enlaces a imagen (jpg, png, gif) se vuelven
+#
+# importante: solo un {wikilink} (con o sin espacios) por línea
+#
+# modo de uso:
+# awk -f gemtext2html-clean.awk archivo.gmo > archivo.html
+#
+
+BEGIN{
+ sitio = "compudanzas"
+ descripcion = "explorations of computing at a human scale"
+ # para poder abrir y cerrar , :
+ modo_lista = 0
+ modo_pre = 0
+ modo_parrafo = 0
+ modo_galeria = 0
+
+ en_section = 1 #empezamos abriendo una sin h1
+
+ navcount = 0
+
+ bloque = 0 # para no agregar después de headers y blockquotes
+
+ print ""
+ print ""
+ print ""
+ print " "
+ print " "
+ contenido = ""
+ nav = ""
+}
+
+function appendContenido( t ){
+ contenido = contenido t "\n"
+}
+function appendNav( t ){
+ nav = nav t "\n"
+ navcount++
+}
+
+function wikiLink( t ){
+ i = match( t, /{.+}/)
+ if ( i ){
+ ifinal = index(t, "}") # índice del } final
+
+ prev = substr(t, 1, i-1) # string previa al link
+# link = substr(t, i, ifinal-i+1) # {link}
+ nombre = substr(t, i+1, ifinal-i-1) # link
+ link = nombre
+ nombre = nombre2Link( nombre, "_" )
+
+ post = substr(t, ifinal+1) # string posterior
+
+ return prev "" link " " post
+ }
+ else{
+ return t
+ }
+}
+
+function nombre2Link( t, r ){ # convierte un nombre con espacios, a uno con r (e.g. "_"
+ gsub(" ",r,t);
+ return t
+}
+
+
+NR == 1{
+ titulo = $0
+ sub("#[[:blank:]]+","",titulo) #prefijo
+
+ archivo = FILENAME
+ sub(/^.+\//,"",archivo) # remove path from filename
+ sub(/.gmo$/,".html",archivo) #change extension
+
+ print " "
+ print " "
+ print " "
+
+ print "" sitio " — " titulo " "
+ print ""
+ print ""
+ print ""
+ print ""titulo" "
+ print " "
+
+ bloque = 1
+
+ next # lee la siguiente línea
+}
+
+
+
+/^\+ /{ # include literal
+ sub(/^+ /,"",$0) # elimina el +
+ appendContenido( $0 )
+ next
+}
+
+/^&/{ # include literal gemtext
+ next
+}
+
+/^[[:blank:]]*$/ { # línea vacía
+ if( !modo_pre ) {
+ if( modo_lista ){ # cierra la lista
+ modo_lista = 0
+ appendContenido( " " )
+ }
+ else if( modo_parrafo ){ # cierra el párrafo
+ modo_parrafo = 0
+ appendContenido( "
" )
+ }
+ else if( modo_galeria ){
+ modo_galeria = 0
+ appendContenido( "" )
+ }
+# else
+# appendContenido( " " )
+ if( bloque ) # si lo previo fue header o blockquote
+ bloque = 0;
+
+ }
+ else
+ appendContenido( $0 )
+ next
+}
+
+/^=>/{ # link
+ if(!modo_pre){
+ if( modo_lista ){ # cierra la lista
+ modo_lista = 0
+ appendContenido( "" )
+ }
+ else if( modo_parrafo ){ # cierra el párrafo
+ modo_parrafo = 0
+ appendContenido( "
" )
+ }
+
+ bloque = 1 #empieza bloque porque es
+
+ # borra flecha del inicio
+ sub("^=>","",$0)
+ # ahora $1 es el path, $2 a $NF el texto
+
+ # concatena todo el texto
+ texto = $2
+ for(i=3; i<=NF; i++){
+ texto = texto" "$i
+ }
+
+ if( match($1, /^\.\//)) { # si es link local
+ # si el path es imagen
+ if( match($1, /(png|jpg|gif)$/) ){
+ # crea imagen
+ if( !modo_galeria ){
+ appendContenido("")
+ modo_galeria = 1
+ }
+ appendContenido(" ")
+ }
+ # si el path no es imagen y es .gmi
+ else if( match($1, /gmi$/) ){
+ # convierte enlace de .gmi a .html !
+ sub(".gmi$",".html",$1)
+
+ # quita { }
+ sub("{","",texto)
+ sub("}","",texto)
+
+ # crea link
+ appendContenido(""texto"
")
+ }
+ else{
+ appendContenido(""texto"
")
+ }
+ }
+ else{ # link externo
+ appendContenido(""texto"
")
+ }
+ }
+ else{
+ appendContenido( $0 )
+ }
+ next
+}
+
+/^* /{ # lista
+ if(!modo_pre){
+ if(!modo_lista){ # inicia la lista
+ if(modo_parrafo){
+ modo_parrafo = 0
+ appendContenido( "
" )
+ }
+ modo_lista = 1
+ appendContenido( "" )
+ }
+ sub("*[[:blank:]]+","",$0)
+ sub("$"," ",$0)
+ appendContenido( wikiLink($0) )
+ }
+ else
+ appendContenido( $0 )
+ next
+}
+
+/^```/{ # preformatted
+ if(modo_pre){
+ # cierra preformatted
+ modo_pre = 0
+ appendContenido( "" )
+ }
+ else{
+ if( modo_lista ){ # cierra la lista
+ modo_lista = 0
+ appendContenido( " " )
+ }
+ else if( modo_parrafo ){ # cierra el párrafo
+ modo_parrafo = 0
+ appendContenido( "" )
+ }
+
+ # abre preformatted
+ modo_pre = 1
+ appendContenido( "" )
+ }
+ next
+}
+
+/^> /{ # blockquote
+ if(!modo_pre){
+ sub(">[[:blank:]]+","",$0)
+ sub("$"," ",$0)
+ bloque = 1
+ }
+ appendContenido( $0 )
+ next
+}
+
+/^# /{ # h1
+ if(!modo_pre){
+ sub("#[[:blank:]]+","",$0) #prefijo
+ sub("$","",$0) #sufijo
+ bloque = 1
+
+ if( !en_section ){ # si no se ha iniciado una sección antes
+ appendContenido( "" )
+ en_section = 1
+ }
+ else{
+ appendContenido( " " )
+ }
+
+ # crea header con id
+ appendContenido( ""$0" " )
+
+ # agrega header a navegación
+ appendNav( ""$0" " )
+ }
+ else{
+ appendContenido( $0 )
+ }
+ next
+
+}
+
+/^## /{ # h2
+ if(!modo_pre){
+ sub("##[[:blank:]]+","",$0)
+ sub("$","",$0)
+ # crea header con id
+ appendContenido( ""$0" " )
+ bloque = 1
+ }
+ else{
+ appendContenido( $0 )
+ }
+ next
+}
+
+/^### /{ # h3
+ if(!modo_pre){
+ sub("###[[:blank:]]+","",$0)
+ sub("$","",$0)
+ appendContenido( ""$0" " )
+ bloque = 1
+ }
+ else{
+ appendContenido( $0 )
+ }
+ next
+}
+
+#$0 !~ /^(=>|```|#{1,3} |* |\+|>|[[:blank:]]*$)/{ # líneas de texto (no "especiales")
+{ # cualquier otra línea de texto
+ if(!modo_pre){
+ if(!modo_parrafo){
+ modo_parrafo = 1
+ appendContenido( "" )
+ }
+ else # nueva línea en el mismo párrafo
+ appendContenido( " " )
+
+ # busca y convierte wikiLink (máx uno por línea)
+ appendContenido( wikiLink($0) )
+ }
+ else{
+ gsub("<","\\<",$0)
+ gsub(">","\\>",$0)
+ appendContenido( $0 )
+ }
+}
+
+END{
+ # imprime y cierra nav
+ if(navcount){
+ print nav
+ print ""
+ }
+
+ # imprime contenido
+ print contenido
+ # cierra tags que pudieron haber quedado abiertas
+ if(modo_pre)
+ print "
"
+ else if(modo_parrafo)
+ print ""
+ else if(modo_lista)
+ print ""
+
+ # finaliza...
+ print ""
+ print ""
+ print ""
+ print ""
+ print ""
+}