about summary refs log tree commit diff stats
path: root/typeset_gemini.awk
diff options
context:
space:
mode:
Diffstat (limited to 'typeset_gemini.awk')
-rw-r--r--typeset_gemini.awk122
1 files changed, 122 insertions, 0 deletions
diff --git a/typeset_gemini.awk b/typeset_gemini.awk new file mode 100644 index 0000000..eb5b7e1 --- /dev/null +++ b/typeset_gemini.awk
@@ -0,0 +1,122 @@
1BEGIN {
2 pre = 0
3 margin = margin ? margin : 4
4 # Core lines
5 txs = "" # text style
6 lns = "\033[1m" # link number style
7 lus = "\033[36m" # link url style
8 lts = "\033[4m" # link text style
9 pfs = "" # preformatted style
10 # Advanced lines
11 h1s = "\033[1;4m" # h1 style
12 h2s = "\033[1m" # h2 style
13 h3s = "\033[3m" # h3 style
14 lis = "" # list item style
15 # Reset
16 res = "\033[0m" # reset style
17}
18/```/ {
19 pre = ! pre
20 next
21}
22pre {
23 mark = "```"
24 fmt = pfs "%s" res
25 text = $0
26}
27/^#/ {
28 match($0, /#+/)
29 mark = substr($0, RSTART, RLENGTH)
30 sub(/#+[[:space:]]*/, "", $0)
31 level = length(mark)
32 if (level == 1) {
33 fmt = h1s "%s" res
34 } else if (level == 2) {
35 fmt = h2s "%s" res
36 } else {
37 fmt = h3s "%s" res
38 }
39}
40/^=>/ {
41 mark = "=>"
42 sub(/=>[[:space:]]*/, "", $0)
43 desc = $1
44 text = ""
45 for (w = 2; w <= NF; w++) {
46 text = text (text ? " " : "") $w
47 }
48 fmt = lns "[" (++ln) "]" res " " lts "%s" res "\t" lus "%s" res
49}
50/^\*[[:space:]]/ {
51 mark = "*"
52 sub(/\*[[:space:]]*/, "", $0)
53 fmt = lis "%s" res
54}
55{
56 mark = mark ? mark : mark
57 fmt = fmt ? fmt : "%s"
58 text = text ? text : fold($0, " ")
59 desc = desc ? desc : ""
60 printf "%-" margin "s" fmt "\n", mark, text, desc
61 mark = fmt = text = desc = ""
62}
63function fold(str, sep, cols, out, cmd, i, j, len, chars, c, last, f, first)
64{
65 if (! cols) {
66 # checks if stdout is a tty
67 if (system("test -t 1")) {
68 cols = 80
69 } else {
70 cmd = "tput cols"
71 cmd | getline cols
72 close(cmd)
73 }
74 }
75 # squeeze tabs and newlines to spaces
76 gsub(/[\t\n]/, " ", str)
77 # if "sep" is empty, just fold on cols with substr
78 if (! length(sep)) {
79 len = length(str)
80 out = substr(str, 1, cols)
81 for (i = cols + 1; i <= len; i += cols) {
82 out = out "\n"
83 for (j = 1; j < margin; j++) {
84 out = out " "
85 }
86 out = out substr(str, i, cols)
87 }
88 return out
89 # otherwise, we have to loop over every character (can't split() on sep, it
90 # would destroy the existing separators)
91 } else {
92 # split string into char array
93 len = split(str, chars, "")
94 # set boolean, used to assign the first line differently
95 first = 1
96 for (i = 1; i <= len; i += last) {
97 f = 0
98 for (c = i + cols - 1; c >= i; c--) {
99 if (index(sep, chars[c])) {
100 last = c - i + 1
101 f = 1
102 break
103 }
104 }
105 if (! f) {
106 last = cols
107 }
108 if (first) {
109 out = substr(str, i, last)
110 first = 0
111 } else {
112 out = out "\n"
113 for (j = 0; j < margin; j++) {
114 out = out " "
115 }
116 out = out substr(str, i, cols)
117 }
118 }
119 }
120 # return the output
121 return out
122}