1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# gemtext2html
# convierte un archivo en gemtext a html de acuerdo a la spec
# excepción: enlaces a imagen (jpg, png, gif) se vuelven <img>
#
# modo de uso:
# awk -f gemtext2html.awk archivo.gmi > archivo.html
#
BEGIN{
# para poder abrir y cerrar <ul>, <pre>, <p>:
modo_lista = 0
modo_pre = 0
modo_parrafo = 0
en_section = 1
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'/>"
contenido = "<main><section>"
nav = "<nav><ul>"
}
function appendContenido( t ){
contenido = contenido t
}
function appendNav( t ){
nav = nav t
}
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
post = substr(t, ifinal+1) # string posterior
return prev "<a href='./" nombre ".html'>" link "</a>" post
}
else{
return t
}
}
NR == 1{
titulo = $0
sub("#[[:blank:]]+","",titulo) #prefijo
print "<title>"titulo"</title>"
print "</head>"
print "<body>"
print "<header>"
print "<h1>"titulo"</h1>"
print "</header>"
bloque = 1
getline # lee la siguiente línea
}
$0 !~ /^(=>|```|#{1,3} |* |>|[[:blank:]]*$)/{ # líneas de texto (no "especiales")
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{
appendContenido( $0 )
}
}
/^[[:blank:]]*$/ { # línea vacía
if( !modo_pre ) {
if( modo_lista ){ # cierra la lista
modo_lista = 0
appendContenido( "</ul>" )
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
appendContenido( "</p>" )
}
else if( bloque ) # si lo previo fue header o blockquote
bloque = 0;
else
appendContenido( "<br/>" )
}
else
appendContenido( $0 )
}
/^=>/{ # link
if(!modo_pre){
if( modo_lista ){ # cierra la lista
modo_lista = 0
appendContenido( "</ul>" )
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
appendContenido( "</p>" )
}
bloque = 1 #empieza bloque porque es <p>
# 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 <img>
$0="<img src='"$1"' alt='"texto"' loading='lazy'/>"
}
# si el path no es imagen
else{
# convierte enlace de .gmi a .html !
sub(".gmi$",".html",$1)
# crea link <a>
$0="<p><a href='"$1"'>"texto"</a></p>"
}
}
else{ # link externo
$0="<p><a href='"$1"' rel=external target=_blank>"texto"</a></p>"
}
}
appendContenido( $0 )
}
/^* /{ # lista
if(!modo_pre){
if(!modo_lista){ # inicia la lista
if(modo_parrafo){
modo_parrafo = 0
appendContenido( "</p>" )
}
modo_lista = 1
appendContenido( "<ul>" )
}
sub("*[[:blank:]]+","<li>",$0)
sub("$","</li>",$0)
}
appendContenido( $0 )
}
/^```/{ # preformatted
if(modo_pre){
# cierra preformatted
modo_pre = 0
appendContenido( "</pre>" )
}
else{
if( modo_lista ){ # cierra la lista
modo_lista = 0
appendContenido( "</ul>" )
}
else if( modo_parrafo ){ # cierra el párrafo
modo_parrafo = 0
appendContenido( "</p>" )
}
# abre preformatted
modo_pre = 1
appendContenido( "<pre>" )
}
}
/^> /{ # blockquote
if(!modo_pre){
sub(">[[:blank:]]+","<blockquote>",$0)
sub("$","</blockquote>",$0)
bloque = 1
}
appendContenido( $0 )
}
/^# /{ # 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( "<section>" )
en_section = 1
}
else{
appendContenido( "</section><section>" )
}
# crea header con id
appendContenido( "<h1 id='"$0"'>"$0"</h1>" )
# agrega header a navegación
appendNav( "<li><a href='#"$0"'>"$0"</a></li>" )
}
else{
appendContenido( $0 )
}
}
/^## /{ # h2
if(!modo_pre){
sub("##[[:blank:]]+","<h2>",$0)
sub("$","</h2>",$0)
bloque = 1
}
appendContenido( $0 )
}
/^### /{ # h3
if(!modo_pre){
sub("###[[:blank:]]+","<h3>",$0)
sub("$","</h3>",$0)
bloque = 1
}
appendContenido( $0 )
}
END{
# imprime y cierra nav
print nav
print "</ul></nav>"
# imprime contenido
print contenido
# cierra tags que pudieron haber quedado abiertas
if(modo_pre)
print "</pre>"
else if(modo_parrafo)
print "</p>"
else if(modo_lista)
print "</ul>"
# finaliza...
print "</section>"
print "</main>"
print "<footer>"
print "<p>página actualizada en: "
fecha = system( "date -r " FILENAME " --rfc-3339=date" )
print "</p>"
print "<p><a href='./index.html'>inicio</a></p>"
print "</footer>"
print "</body>"
print "</html>"
}
|