about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xjimmy5
-rwxr-xr-xjimmy.sh137
2 files changed, 141 insertions, 1 deletions
diff --git a/jimmy b/jimmy index 74cef29..aa79183 100755 --- a/jimmy +++ b/jimmy
@@ -41,7 +41,10 @@ BEGIN { # configuration
41 linefmt["header", 3] = "<h3>%s</h3>\n" 41 linefmt["header", 3] = "<h3>%s</h3>\n"
42 isblock["link"] = 0 42 isblock["link"] = 0
43 linefmt["link"] = "<a href=\"%s\">%s</a>\n" 43 linefmt["link"] = "<a href=\"%s\">%s</a>\n"
44 # escapes 44 # escapes -- TODO: rethink these.
45 ## I think the best solution is to have a pair of arrays --
46 ## esc_orig and esc_repl. These will have keys in the
47 ## [n, block] format to keep them straight.
45 esc["verbatim", 0, "&"] = "\\&amp;" 48 esc["verbatim", 0, "&"] = "\\&amp;"
46 esc["verbatim", 8, "<"] = "\\&lt;" 49 esc["verbatim", 8, "<"] = "\\&lt;"
47 esc["verbatim", 9, ">"] = "\\&gt;" 50 esc["verbatim", 9, ">"] = "\\&gt;"
diff --git a/jimmy.sh b/jimmy.sh new file mode 100755 index 0000000..f039f4f --- /dev/null +++ b/jimmy.sh
@@ -0,0 +1,137 @@
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 "$@"