gmi2html.awk §
2021-12-02T21:49:43Z
Définitivement, j'adore awk.
J'avais écrit un convertisseur de gemtext vers html en C : 173 lignes.
Je viens de recommencer avec awk : 30 lignes.
C'est hyper satisfaisant !
Je suis sûr qu'on peut faire mieux, mais je suis ravi du résultat tout de même.
#!/usr/bin/awk -f # Read gemtext and output html function htmlesc(s) { gsub("&","\\&",s) gsub("<","\\<",s) gsub(">","\\>",s) gsub("\"","\\"",s) gsub("'","\\'",s) return s } /^```/ { if (pre) { print "</pre>"} else { print "<pre>" } pre = (pre + 1) % 2 # toggle pre next } /^$/ { if (ul == 1) { ul = 0; print "</ul>" } next } !pre && /^\*/ { if (ul == 0) { ul = 1; print "<ul>" } $1="" printf "<li>%s</li>\n", htmlesc($0) !pre && /^=>/ { href = htmlesc($2) if ( NF > 2 ) { $1 = ""; $2 = ""; title = htmlesc($0) } \ else { title = htmlesc($2) } printf "<a href=\"%s\">%s</a><br>\n", href, title next } !pre && /^###/ { $1=""; printf "<h3>%s</h3>\n", htmlesc($0); next } !pre && /^##/ { $1=""; printf "<h2>%s</h2>\n", htmlesc($0); next } !pre && /^#/ { $1=""; printf "<h1>%s</h1>\n", htmlesc($0); next } !pre && /^>/ { $1=""; printf "<blockquote>%s</blockquote>\n", htmlesc($0); next } pre { print htmlesc($0); next} 1 { printf "<p>%s</p>\n", htmlesc($0) }
Le compte est obtenu avec ce script :
#!/bin/sh # count lines of code # https://text.causal.agency/004-uloc.txt sed -E '/\/\//d; /^[[:space:]]+\*[[:space:]]/d; /\/\*/d; /^#/d; /\*\/$/d' *h *c *.awk *.sh |\ sort -u |\ wc -l
Une réaction? §
Envoyez votre commentaire par mail.
Mode d'emploi de la liste de diffusion pour recevoir les réponses.