lentejanumerica/gemtext2html.awk

289 lines
5.9 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
#
# 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.gmi > archivo.html
#
2021-05-20 00:24:20 +00:00
2021-05-20 00:06:21 +00:00
BEGIN{
2021-05-20 03:21:49 +00:00
sitio = "HOLO"
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 00:55:29 +00:00
en_section = 1
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'>"
print "<html xmlns='http://www.w3.org/1999/xhtml' lang='es-MX'>"
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-05-20 02:32:26 +00:00
print "<link rel='stylesheet' href='./static/estilo.css'>"
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 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 02:32:26 +00:00
gsub(" ","_",nombre) # reemplaza espacios por _
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 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-20 03:21:49 +00:00
print "<p><a href='./index.html'>{" sitio "}</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
getline # lee la siguiente línea
2021-05-20 00:06:21 +00:00
}
$0 !~ /^(=>|```|#{1,3} |* |>|[[:blank:]]*$)/{ # líneas de texto (no "especiales")
if(!modo_pre){
if(!modo_parrafo){
modo_parrafo = 1
2021-05-20 00:24:20 +00:00
appendContenido( "<p>" )
2021-05-20 00:06:21 +00:00
}
else # nueva línea en el mismo párrafo
2021-05-20 00:24:20 +00:00
appendContenido( "<br/>" )
2021-05-20 00:06:21 +00:00
2021-05-20 01:48:34 +00:00
# busca y convierte wikiLink (máx uno por línea)
2021-05-20 01:14:08 +00:00
appendContenido( wikiLink($0) )
}
else{
appendContenido( $0 )
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 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
}
# si el path no es imagen
else{
# 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-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 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-05-20 01:48:34 +00:00
appendContenido( $0 )
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
}
}
/^> /{ # 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 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 00:06:21 +00:00
}
/^## /{ # h2
if(!modo_pre){
sub("##[[:blank:]]+","<h2>",$0)
sub("$","</h2>",$0)
bloque = 1
}
2021-05-20 00:24:20 +00:00
appendContenido( $0 )
2021-05-20 00:06:21 +00:00
}
/^### /{ # h3
if(!modo_pre){
sub("###[[:blank:]]+","<h3>",$0)
sub("$","</h3>",$0)
bloque = 1
}
2021-05-20 00:24:20 +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 00:24:20 +00:00
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-20 03:45:35 +00:00
print "<p><a href='./index.html'>{" sitio "}</a></p>"
2021-05-20 00:55:29 +00:00
print "<p>página actualizada en: "
fecha = system( "date -r " FILENAME " --rfc-3339=date" )
print "</p>"
2021-05-20 03:21:49 +00:00
print "<a href='https://endefensadelsl.org/ppl_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>"
}