Blob


1 # gmi2txt
3 gmi2txt is a small script to format a text/gemini page. needs par(1)
5 #!/usr/bin/awk -f
7 BEGIN {
8 in_pre = 0
10 title1 = "sed s/./=/g"
11 title2 = "sed s/./-/g"
12 parl = "par 72p2"
13 pari = "par 72p3 | sed '2,$s/^ \\*/ /'"
14 parq = "par 72p2"
15 par = "par 72"
16 }
18 !in_pre && /^```/ {
19 in_pre = 1
20 print $0
21 next
22 }
23 in_pre && /^```/ {
24 in_pre = 0
25 print $0
26 next
27 }
28 in_pre { print $0; next }
30 // {
31 # print a blank line between links and other line
32 # types, unless there's already a blank line.
34 if (last_was_link && !match($0, "^[ \t]*$")) {
35 print "";
36 }
37 }
39 /^=>/ {
40 last_was_link = 1
42 $0 = gensub("=> *", "", 1)
43 link = $1
44 $1 = ""
45 text = gensub("^ *", "", 1)
46 if (text == "")
47 text = link
49 printf("~ %s\n", text) | parl
50 printf("%s\n", link)
51 close(parl)
52 next
53 }
55 // { last_was_link = 0 }
57 /^###/ {
58 t = gensub("### *", "", "1")
59 printf("-%s-\n", t)
60 next
61 }
63 /^##/ {
64 t = gensub("## *", "", 1)
65 print t
66 print t | title2
67 close(title2)
68 next
69 }
71 /^#/ {
72 t = gensub("# *", "", 1)
73 print t
74 print t | title1
75 close(title1)
76 next
77 }
79 /^>/ {
80 print $0 | parq
81 close(parq)
82 next
83 }
85 /^\*/ {
86 printf(" %s", $0) | pari
87 close(pari)
88 next
89 }
91 // {
92 print $0 | par
93 close(par)
94 }
96 END {
97 if (in_pre)
98 print "```"
99 }