use hashable rather than data-hash (hashes Text more efficiently)
[diohsc.git] / tools / gmi2html.sed
blob6c1c31fdd3288eb9668f566d3f0a3b0074cd55ea
1 #!/bin/sed -Ef
2 # gmi2html: Sed script to convert text/gemini to html.
3 # Based on v0.14.2 of the gemini spec.
5 # This script is dedicated to the public domain according to the terms of CC0:
6 # https://creativecommons.org/publicdomain/zero/1.0/
8 # TODO: add meta charset header, I suppose, to ensure utf8?
10 s/\&/\&/g
11 s/"/\"/g
12 s/</\&lt;/g
13 /^=?>/! s/>/\&gt;/g
14 /^=?>/ {
15     :loop
16     s/^((=?>).*)>/\1\&gt;/
17     t loop
20 1 {
21     i\
22 <!DOCTYPE html>\
23 <html>
24     h
25     s/^#\s*//
26     t title_set
27     s/.*/Untitled/
28     :title_set
29     s/.*/<head><title>&<\/title><\/head>/
30     p
31     i\
32 <body>
33     s/.*//
34     x
38 /^```/ {
39     x
40     /^```/ {
41         s/.*/<\/pre>/
42         h
43     }
44     b end
46 /^(\*|=>) / {
47     x
48     /^(\*|=>) / {
49         s/^\* (.*)/<li>\1<\/li>/
50         s/^=>\s*([^[:space:]]+)\s*$/<li><a href="\1">\1<\/a><\/li>/
51         s/^=>\s*([^[:space:]]+)\s+(.+)/<li><a href="\1">\2<\/a><\/li>/
52         b end
53     }
54     i\
55 </ul>
56     h
60 t linetypes
61 :linetypes
63 s/^```.*/<pre>/
65 s/^\* (.*)/<ul><li>\1<\/li>/
67 s/^=>\s*([^[:space:]]+)\s*$/<ul><li><a href="\1">\1<\/a><\/li>/
68 s/^=>\s*([^[:space:]]+)\s+(.+)/<ul><li><a href="\1">\2<\/a><\/li>/
70 s/^###\s*(.*)/<h3>\1<\/h3>/
71 s/^##\s*(.*)/<h2>\1<\/h2>/
72 s/^#\s*(.*)/<h1>\1<\/h1>/
74 s/^>\s*(.*)/<blockquote>\1<\/blockquote>/
76 t end
77 s/.+/<p>&<\/p>/
79 :end
80 $ {
81     x
82     /^```/ a\
83 </pre>
84     /^(\*|=>) / a\
85 </ul>
86     x
87     a\
88 </body>\
89 </html>