about summary refs log tree commit diff stats
path: root/jimmy.sh
diff options
context:
space:
mode:
authorCase Duckworth2024-05-09 22:28:47 -0500
committerCase Duckworth2024-05-09 22:28:47 -0500
commit6b63dad736d44ef0e61f4ca1aa95e07430b89af3 (patch)
tree37889195ba326025273a26501fee18d63b39e32f /jimmy.sh
parentBegin an sh impl (diff)
downloadjimmy-6b63dad736d44ef0e61f4ca1aa95e07430b89af3.tar.gz
jimmy-6b63dad736d44ef0e61f4ca1aa95e07430b89af3.zip
Finish shell impl
This version includes
- customizable format specifiers for all elements
- line and buffer filtering, which makes all sorts of things possible
- and more!
Diffstat (limited to 'jimmy.sh')
-rwxr-xr-xjimmy.sh137
1 files changed, 0 insertions, 137 deletions
diff --git a/jimmy.sh b/jimmy.sh deleted file mode 100755 index f039f4f..0000000 --- a/jimmy.sh +++ /dev/null
@@ -1,137 +0,0 @@
1#!/bin/sh
2
3BLOCK=
4BUFFER=
5
6process() {
7 while read -r LINE
8 do
9 if test "$BLOCK" = verbatim
10 then
11 printf '%s\n' "$LINE"
12 continue
13 fi
14
15 set -- $LINE
16
17 case "$LINE" in
18 ('```')
19 if test "$BLOCK" = verbatim
20 then BLOCK=
21 else BLOCK=verbatim
22 fi
23 ;;
24 ('')
25 if test "$BLOCK" = verbatim
26 then bufpush $'\n'
27 else bufclose
28 fi
29 ;;
30 ('=>'*) link "$@" ;;
31 ('#'*) header "$@" ;;
32 ('*'*) shift; blknew list "$*" ;;
33 ('>'*) shift; blknew quote "$*" ;;
34 (*) shift; blknew paragraph "$*" ;;
35 esac
36 done
37 bufclose
38}
39
40blknew() {
41 test "$BLOCK" = "$1" || bufclose
42 bufpush "$(printf "$(eval echo "\$format_$BLOCK")" "$@")"
43 BLOCK="$1"
44}
45
46bufclose() {
47 if test -z "$BLOCK"
48 then "$COLLAPSE_BLANKS" && return
49 else newline; return
50 fi
51
52 # blockp "$BLOCK" || return
53 # fillp $BLOCK && buffill "$(fillp $BLOCK)"
54
55 # TODO: escape shit
56
57 printf '%s%s%s\n' \
58 "$(echo eval "\$opener_$BLOCK")" \
59 "$BUFFER" \
60 "$(echo eval "\$closer_$BLOCK")"
61
62 BLOCK=
63}
64
65buffill() { # buffill WIDTH
66 if test $1 -lt 0
67 then BUFFER="$(printf '%s\n' "$BUFFER" | tr '\n' ' ')"
68 else
69 out=
70 nline=0
71 printf '%s\n' "$BUFFER" | sed 's/[ \t]\+/\n/g' |
72 while read -r word
73 do
74 if test $((nline + ${#word})) -ge "$1"
75 then
76 out="${out}"${out:+$'\n'}"${word}"
77 nline=${#word}
78 else
79 out="${out}${out:+ }${word}"
80 nline=$((nline + ${#word} + 1))
81 fi
82 done
83 BUFFER="$out"
84 fi
85}
86
87bufpush() { BUFFER="${BUFFER}$@"; }
88
89fillp() {
90 :
91}
92
93header() {
94 bufclose
95 lvl=${#1}; shift
96 bufpush "$(printf "$(eval echo "\$format_h$lvl")" "$*")"
97 BLOCK=header
98}
99
100link() {
101 url="$2"; shift 2
102 if test "$BLOCK" = paragraph #&& ! blockp link
103 then bufpush "$(printf "$format_link" "$url" "$*")"
104 else blknew linklist "$url" "$*"
105 fi
106}
107
108newline() { printf '\n'; }
109
110html() {
111 format_link='<a href="%s">%s</a>\n'
112 format_h1='<h1>%s</h1>\n'
113 format_h2='<h2>%s</h2>\n'
114 format_h3='<h3>%s</h3>\n'
115 opener_verbatim='<pre><code>'
116 closer_verbatim='</code></pre>\n'
117 format_verbatim='%s\n'
118 opener_paragraph='<p>'
119 closer_paragraph='</p>\n'
120 format_paragraph='%s\n'
121 opener_quote='<blockquote>'
122 closer_quote='</blockquote>\n'
123 format_quote='%s\n'
124 opener_list='<ul>\n'
125 closer_list='</ul>\n'
126 format_list='<li>%s</li>'
127 opener_linklist='<ul class="linklist">'
128 closer_linklist='</ul>'
129 format_linklist="$(printf "$format_list" "$format_link")"
130}
131
132main() {
133 html
134 process "$@"
135}
136
137main "$@"