lentejanumerica/gemtext2html.awk

343 lines
7.3 KiB
Awk
Raw Normal View History

2021-05-20 00:06:21 +00:00
# gemtext2html
# convierte un archivo en gemtext a html de acuerdo a la spec
# excepción: enlaces a imagen (jpg, png, gif) se vuelven <img>
2021-05-20 03:21:49 +00:00
# TODO actualizar descripción
2021-05-20 22:10:49 +00:00
# TODO h2 en nav?
2021-05-20 03:21:49 +00:00
#
# importante: solo un {wikilink} (con o sin espacios) por línea
2021-05-20 00:06:21 +00:00
#
# modo de uso:
# awk -f gemtext2html.awk archivo.gmo > archivo.html
2021-05-20 00:06:21 +00:00
#
2021-05-20 00:24:20 +00:00
2021-05-20 00:06:21 +00:00
BEGIN{
2021-05-26 23:31:28 +00:00
# sitio = "🥭"
sitio = "ln"
2021-05-20 00:06:21 +00:00
# para poder abrir y cerrar <ul>, <pre>, <p>:
modo_lista = 0
modo_pre = 0
modo_parrafo = 0
2021-05-20 03:21:49 +00:00
modo_galeria = 0
2021-05-20 00:06:21 +00:00
2021-05-20 21:13:28 +00:00
en_section = 1 #empezamos abriendo una <section> sin h1
navcount = 0
2021-05-20 00:55:29 +00:00
2021-05-20 00:06:21 +00:00
bloque = 0 # para no agregar <br/> después de headers y blockquotes
print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
2021-05-20 22:10:49 +00:00
print "<html xmlns='http://www.w3.org/1999/xhtml' lang='es-MX,en'>"
2021-05-20 00:06:21 +00:00
print "<head>"
print "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
print "<meta content='initial-scale=1.0, maximum-scale=1.0, user-scalable=yes' name='viewport'/>"
2021-06-01 04:01:01 +00:00
print "<meta http-equiv='onion-location' content='http://chhhqibec5pvmyxmdobfdx33xhhtbla5lvrs2fgn7knh7kaaz4kgylad.onion.onion' />"
2021-05-20 02:32:26 +00:00
print "<link rel='stylesheet' href='./static/estilo.css'>"
2021-05-26 23:31:28 +00:00
print " <link rel='icon' href='./img/icon_bola.gif' type='image/gif' sizes='16x16'> "
2021-05-20 00:24:20 +00:00
2021-05-20 00:55:29 +00:00
contenido = "<main><section>"
2021-05-20 00:24:20 +00:00
nav = "<nav><ul>"
}
function appendContenido( t ){
2021-05-20 03:21:49 +00:00
contenido = contenido t "\n"
2021-05-20 00:06:21 +00:00
}
2021-05-20 00:24:20 +00:00
function appendNav( t ){
2021-05-20 03:21:49 +00:00
nav = nav t "\n"
2021-05-20 21:13:28 +00:00
navcount++
2021-05-20 00:24:20 +00:00
}
2021-05-20 01:14:08 +00:00
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
2021-05-20 20:48:31 +00:00
nombre = nombre2Link( nombre, "_" )
2021-05-20 02:32:26 +00:00
2021-05-20 01:14:08 +00:00
post = substr(t, ifinal+1) # string posterior
2021-05-20 01:31:57 +00:00
return prev "<a href='./" nombre ".html'>" link "</a>" post
2021-05-20 01:14:08 +00:00
}
else{
return t
}
}
2021-05-20 20:48:31 +00:00
function nombre2Link( t, r ){ # convierte un nombre con espacios, a uno con r (e.g. "_"
gsub(" ",r,t);
return t
}
2021-05-20 00:06:21 +00:00
NR == 1{
titulo = $0
sub("#[[:blank:]]+","",titulo) #prefijo
2021-05-20 03:21:49 +00:00
print "<title>" sitio " &mdash; " titulo "</title>"
2021-05-20 00:06:21 +00:00
print "</head>"
print "<body>"
2021-05-20 00:24:20 +00:00
print "<header>"
2021-05-26 23:31:28 +00:00
# print "<p><a href='./index.html'>{" sitio "}</a></p>"
2021-06-01 03:53:36 +00:00
print "<p><a href='./index.html' title='lenteja numérica'>{<img src='./img/icon_bola.gif' style='margin:0' alt='icono de lenteja numérica'/>}</a></p>"
2021-05-20 00:24:20 +00:00
print "<h1>"titulo"</h1>"
print "</header>"
2021-05-20 00:55:29 +00:00
bloque = 1
2021-05-20 21:02:36 +00:00
next # lee la siguiente línea
2021-05-20 00:06:21 +00:00
}
2021-05-20 21:02:36 +00:00
2021-05-20 21:13:28 +00:00
/^\+ /{ # include literal
2021-05-20 21:06:33 +00:00
sub(/^+ /,"",$0) # elimina el +
2021-05-20 21:02:36 +00:00
appendContenido( $0 )
next
2021-05-20 00:06:21 +00:00
}
2021-05-20 21:06:33 +00:00
/^&/{ # include literal gemtext
next
}
2021-05-20 00:06:21 +00:00
/^[[:blank:]]*$/ { # línea vacía
if( !modo_pre ) {
if( modo_lista ){ # cierra la lista
modo_lista = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</ul>" )
2021-05-20 00:06:21 +00:00
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</p>" )
2021-05-20 00:06:21 +00:00
}
2021-05-20 03:21:49 +00:00
else if( modo_galeria ){
modo_galeria = 0
appendContenido( "</gallery>" )
}
# else
# appendContenido( "<br/>" )
2021-05-20 03:21:49 +00:00
if( bloque ) # si lo previo fue header o blockquote
bloque = 0;
2021-05-20 00:06:21 +00:00
}
else
2021-05-20 00:24:20 +00:00
appendContenido( $0 )
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^=>/{ # link
if(!modo_pre){
if( modo_lista ){ # cierra la lista
modo_lista = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</ul>" )
2021-05-20 00:06:21 +00:00
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</p>" )
2021-05-20 00:06:21 +00:00
}
2021-05-20 01:48:34 +00:00
bloque = 1 #empieza bloque porque es <p>
2021-05-20 01:35:27 +00:00
2021-05-20 00:06:21 +00:00
# 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
}
2021-05-20 01:31:57 +00:00
if( match($1, /^\.\//)) { # si es link local
# si el path es imagen
if( match($1, /(png|jpg|gif)$/) ){
# crea imagen <img>
2021-05-20 03:21:49 +00:00
if( !modo_galeria ){
appendContenido("<gallery>")
modo_galeria = 1
}
appendContenido("<img src='"$1"' alt='"texto"' loading='lazy'/>")
2021-05-20 01:31:57 +00:00
}
2021-05-21 04:07:25 +00:00
# si el path no es imagen y es .gmi
else if( match($1, /gmi$/) ){
2021-05-20 01:31:57 +00:00
# convierte enlace de .gmi a .html !
sub(".gmi$",".html",$1)
2021-05-20 00:06:21 +00:00
2021-05-20 01:31:57 +00:00
# crea link <a>
2021-05-20 03:21:49 +00:00
appendContenido("<p><a href='"$1"'>"texto"</a></p>")
2021-05-20 01:31:57 +00:00
}
2021-05-21 04:07:25 +00:00
else{
appendContenido("<p><a href='"$1"'>"texto"</a></p>")
}
2021-05-20 00:06:21 +00:00
}
2021-05-20 01:48:34 +00:00
else{ # link externo
2021-05-20 03:21:49 +00:00
appendContenido("<p><a href='"$1"' rel=external target=_blank>"texto"</a></p>")
2021-05-20 01:35:27 +00:00
}
2021-05-20 00:06:21 +00:00
}
2021-05-20 03:21:49 +00:00
else{
appendContenido( $0 )
}
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^* /{ # lista
if(!modo_pre){
if(!modo_lista){ # inicia la lista
if(modo_parrafo){
modo_parrafo = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</p>" )
2021-05-20 00:06:21 +00:00
}
modo_lista = 1
2021-05-20 00:24:20 +00:00
appendContenido( "<ul>" )
2021-05-20 00:06:21 +00:00
}
sub("*[[:blank:]]+","<li>",$0)
sub("$","</li>",$0)
2021-06-01 18:09:15 +00:00
appendContenido( wikiLink($0) )
2021-05-20 00:06:21 +00:00
}
2021-06-01 18:09:15 +00:00
else
appendContenido( $0 )
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^```/{ # preformatted
if(modo_pre){
# cierra preformatted
modo_pre = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</pre>" )
2021-05-20 00:06:21 +00:00
}
else{
if( modo_lista ){ # cierra la lista
modo_lista = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</ul>" )
2021-05-20 00:06:21 +00:00
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
2021-05-20 00:24:20 +00:00
appendContenido( "</p>" )
2021-05-20 00:06:21 +00:00
}
# abre preformatted
modo_pre = 1
2021-05-20 00:24:20 +00:00
appendContenido( "<pre>" )
2021-05-20 00:06:21 +00:00
}
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^> /{ # blockquote
if(!modo_pre){
sub(">[[:blank:]]+","<blockquote>",$0)
sub("$","</blockquote>",$0)
bloque = 1
}
2021-05-20 00:24:20 +00:00
appendContenido( $0 )
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^# /{ # h1
if(!modo_pre){
2021-05-20 00:24:20 +00:00
sub("#[[:blank:]]+","",$0) #prefijo
sub("$","",$0) #sufijo
2021-05-20 00:06:21 +00:00
bloque = 1
2021-05-20 00:55:29 +00:00
if( !en_section ){ # si no se ha iniciado una sección antes
appendContenido( "<section>" )
en_section = 1
}
else{
appendContenido( "</section><section>" )
}
2021-05-20 01:48:34 +00:00
# crea header con id
2021-05-20 00:55:29 +00:00
appendContenido( "<h1 id='"$0"'>"$0"</h1>" )
# agrega header a navegación
appendNav( "<li><a href='#"$0"'>"$0"</a></li>" )
2021-05-20 00:06:21 +00:00
}
2021-05-20 00:55:29 +00:00
else{
appendContenido( $0 )
}
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:55:29 +00:00
2021-05-20 00:06:21 +00:00
}
/^## /{ # h2
if(!modo_pre){
2021-05-20 20:50:41 +00:00
sub("##[[:blank:]]+","",$0)
sub("$","",$0)
# crea header con id
appendContenido( "<h2 id='"$0"'>"$0"</h2>" )
2021-05-20 00:06:21 +00:00
bloque = 1
}
2021-05-20 20:50:41 +00:00
else{
appendContenido( $0 )
}
2021-05-20 21:02:36 +00:00
next
2021-05-20 00:06:21 +00:00
}
/^### /{ # h3
if(!modo_pre){
2021-05-20 20:50:41 +00:00
sub("###[[:blank:]]+","",$0)
sub("$","",$0)
appendContenido( "<h3 id='"$0"'>"$0"</h3>" )
2021-05-20 00:06:21 +00:00
bloque = 1
}
2021-05-20 20:50:41 +00:00
else{
appendContenido( $0 )
}
2021-05-20 21:02:36 +00:00
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( "<p>" )
}
else # nueva línea en el mismo párrafo
appendContenido( "<br/>" )
# busca y convierte wikiLink (máx uno por línea)
appendContenido( wikiLink($0) )
}
else{
2021-05-20 22:10:49 +00:00
gsub("<","\\&lt;",$0)
gsub(">","\\&gt;",$0)
2021-05-20 21:02:36 +00:00
appendContenido( $0 )
}
2021-05-20 00:06:21 +00:00
}
END{
2021-05-20 01:48:34 +00:00
# imprime y cierra nav
2021-05-20 21:13:28 +00:00
if(navcount){
print nav
print "</ul></nav>"
}
2021-05-20 01:48:34 +00:00
# imprime contenido
2021-05-20 00:24:20 +00:00
print contenido
2021-05-20 01:31:57 +00:00
# cierra tags que pudieron haber quedado abiertas
2021-05-20 00:06:21 +00:00
if(modo_pre)
print "</pre>"
else if(modo_parrafo)
print "</p>"
else if(modo_lista)
print "</ul>"
2021-05-20 01:48:34 +00:00
# finaliza...
2021-05-20 00:55:29 +00:00
print "</section>"
2021-05-20 00:24:20 +00:00
print "</main>"
print "<footer>"
2021-05-26 23:31:28 +00:00
# print "<p><a href='./index.html'>{" sitio "}</a></p>"
print "<p><a href='./index.html' title='lenteja numérica'>{<img src='./img/icon_bola.gif' alt='icono de lenteja numérica' width=8px height=8px style='margin:0'/>}</a></p>"
2021-05-20 21:29:48 +00:00
print "<p>página actualizada en: "
print "<time datetime='"fecha"'>" fechasjm "</time>"
2021-05-20 22:57:40 +00:00
print " (1"fecha")"
2021-05-20 00:55:29 +00:00
print "</p>"
2021-05-20 21:29:48 +00:00
# fecha = system( "date -r " FILENAME " --rfc-3339=date" )
# print "</p>"
print "<a href='https://endefensadelsl.org/ppl_deed_es.html' rel=external target=_blank>ppl: licencia de producción de pares</a></p>"
2021-05-20 00:24:20 +00:00
print "</footer>"
2021-05-20 00:06:21 +00:00
print "</body>"
print "</html>"
}