about summary refs log tree commit diff stats
path: root/typeset_gemini.sh
diff options
context:
space:
mode:
Diffstat (limited to 'typeset_gemini.sh')
-rw-r--r--typeset_gemini.sh168
1 files changed, 168 insertions, 0 deletions
diff --git a/typeset_gemini.sh b/typeset_gemini.sh new file mode 100644 index 0000000..e8aed41 --- /dev/null +++ b/typeset_gemini.sh
@@ -0,0 +1,168 @@
1typeset_gemini() {
2 local pre=false
3
4 while read -r line; do
5 case "$line" in
6 '```')
7 flip pre
8 continue
9 ;;
10 =\>*) gemini_link "$line" $pre ;;
11 \#*) gemini_header "$line" $pre ;;
12 \**) gemini_list "$line" $pre ;;
13 *) gemini_text "$line" $pre ;;
14 esac
15 done
16}
17
18gemini_link() {
19 local re="^(=>)[[:blank:]]*([^[:blank:]]+)[[:blank:]]*(.*)"
20 local s t a # sigil, text, annotation(url)
21 if ! ${2-false} && [[ "$1" =~ $re ]]; then
22 s="${BASH_REMATCH[1]}"
23 t="${BASH_REMATCH[3]}"
24 a="${BASH_REMATCH[2]}"
25
26 printf "$C_SIGIL%-${MARGIN}s" "$s"
27 fold_line "$WIDTH" "$(printf "$C_LINK_TITLE%s $C_LINK_URL%s$C_RESET\n" \
28 "$t" "$a")"
29 else
30 gemini_pre "$1"
31 fi
32}
33
34gemini_header() {
35 local re="^(#+)[[:blank:]]*(.*)"
36 local s t a # sigil, text, annotation(lvl)
37 if ! ${2-false} && [[ "$1" =~ $re ]]; then
38 s="${BASH_REMATCH[1]}"
39 a="${#BASH_REMATCH[1]}"
40 t="${BASH_REMATCH[2]}"
41
42 local hdrfmt
43 hdrfmt="$(eval echo "\$C_HEADER$a")"
44 printf "$C_SIGIL%-${MARGIN}s$hdrfmt%s$C_RESET\n" \
45 "$s" "$(fold_line "$WIDTH" "$t")"
46 else
47 gemini_pre "$1"
48 fi
49}
50
51gemini_list() {
52 local re="^(\*)[[:blank:]]*(.*)"
53 local s t a # sigil, text, annotation(n/a)
54 if ! ${2-false} && [[ "$1" =~ $re ]]; then
55 s="${BASH_REMATCH[1]}"
56 t="${BASH_REMATCH[2]}"
57
58 printf "$C_SIGIL%-${MARGIN}s$C_LIST%s$C_RESET\n" \
59 "$s" "$(fold_line "$WIDTH" "$t")"
60 else
61 gemini_pre "$1"
62 fi
63}
64
65gemini_text() {
66 printf "%${MARGIN}s" ' '
67 if ! ${2-false}; then
68 fold_line "$WIDTH" "$1"
69 else
70 gemini_pre "$1"
71 fi
72}
73
74gemini_pre() {
75 printf "%${MARGIN}s%s" ' ' "$1"
76}
77
78flip() { # flip NAME
79 [[ "${!1}" == true || "${!1}" == false ]] || return 1
80
81 if "${!1}"; then
82 eval "$1=false"
83 else
84 eval "$1=true"
85 fi
86}
87
88fold_line() { # fold_line WIDTH TEXT
89 local width="$1"
90 local ll=0 wl plain
91 # shellcheck disable=2086
92 # TODO: determine if this is the best way to do it
93 set -- $2
94
95 for word; do
96 plain="${word//$'\x1b'\[*([0-9;])m/}"
97 wl=$((${#plain} + 1))
98 if (((ll + wl) >= width)); then
99 printf "\n%${MARGIN}s" ' '
100 ll=$wl
101 else
102 ll=$((ll + wl))
103 fi
104 printf '%s ' "$word"
105 done
106 printf '\n'
107}
108
109# just here for reference
110strip() { # strip control sequences
111 # https://stackoverflow.com/a/55872518
112 shopt -s extglob
113 while IFS='' read -r x; do
114 # remove colors
115 echo "${x//$'\x1b'\[*([0-9;])m/}"
116 done
117}
118
119test() {
120 MARGIN=4
121 WIDTH=60
122 #shopt -s checkwinsize; (:;:)
123 #WIDTH="$((COLUMNS - (MARGIN*2)))"
124 C_LINK_TITLE=$'\e[34m'
125 C_LINK_URL=$'\e[31m'
126 C_RESET=$'\e[0m'
127 typeset_gemini <<-'EOF'
128 # Project Gemini
129
130 ## Overview
131
132 Gemini is a new internet protocol which:
133
134 * Is heavier than gopher
135 * Is lighter than the web
136 * Will not replace either
137 * Strives for maximum power to weight ratio
138 * Takes user privacy very seriously
139
140 ## Resources
141
142 => docs/ Gemini documentation
143 => software/ Gemini software
144 => servers/ Known Gemini servers
145 => https://lists.orbitalfox.eu/listinfo/gemini Gemini mailing list
146 => gemini://gemini.conman.org/test/torture/ Gemini client torture test
147
148 ## Web proxies
149
150 => https://portal.mozz.us/?url=gemini%3A%2F%2Fgemini.circumlunar.space%2F&fmt=fixed Gemini-to-web proxy service
151 => https://proxy.vulpes.one/gemini/gemini.circumlunar.space Another Gemini-to-web proxy service
152
153 ## Search engines
154
155 => gemini://gus.guru/ Gemini Universal Search engine
156 => gemini://houston.coder.town Houston search engine
157
158 ## Geminispace aggregators (experimental!)
159
160 => capcom/ CAPCOM
161 => gemini://rawtext.club:1965/~sloum/spacewalk.gmi Spacewalk
162
163 ## Free Gemini hosting
164
165 => users/ Users with Gemini content on this server
166 EOF
167}
168test