]> git.plutz.net Git - clickslide/blob - clickslide.sh
portability: prevent crashes in bash
[clickslide] / clickslide.sh
1 #!/bin/sh
2
3 prev='' next='' idoff=0
4 depth=0 ucdepth=-1
5
6 base64() {
7   uuencode -m - <"$1" \
8   | sed '1d; :X;$!{N;bX;}; s;\n;;g; s;=\+;;g;'
9 }
10
11 { "${0%/*}"/cgilite/html-sh.sed || cat; } \
12 | {
13   line="$(line)"
14   while :; do 
15     tag="${tag}${line%%>*}"
16   
17     if [ "$line" = "${line%%>*}" ]; then
18       # $line did not contain ">" and thus was added to $tag entirely
19       if ! line="$(line)"; then
20         printf %s\\n "$tag"
21         break
22       fi
23       tag="${tag}
24   "
25     else
26       # $line is shortened by segment added to $tag
27       line="${line#*>}"
28       tag="${tag}>"
29     fi
30
31     ### Image embedding for Inline styles
32     while expr "$tag" : '.*<[^>]*style="[^"]*url("\?[^)]\+\.\(png\|jpg\|jpeg\|gif\|svg\)"\?)'; do
33       pre="${tag%%url(*)*}"
34       post="${tag#*url(*)}"
35       file="${tag#${pre}url(}" file="${file%)$post}"
36       file="${file#\"}" file="${file%\"}"
37       echo Inlining Background Image "$file" >&2
38       if [ -r "$file" ]; then
39         tag="${pre}url('data:image/${file##*.};base64,$(base64 "$file")')${post}"
40       fi
41     done >/dev/null
42
43     ### Image embedding for Image tags
44     while expr "$tag" : '.*<img [^>]*src="[^"]\+\.\(png\|jpg\|jpeg\|gif\|svg\)"'; do
45       pre="${tag%%src=\"*\"*}"
46       post="${tag#*src=\"*\"}"
47       file="${tag#${pre}src=\"}" file="${file%\"$post}"
48       echo Inlining Image "$file" >&2
49       if [ -r "$file" ]; then
50         tag="${pre}src=\"data:image/${file##*.};base64,$(base64 "${file}")\"${post}"
51       fi
52     done >/dev/null
53
54     ### Tag Processing
55     case $tag in
56       *\<head\>*|\*\<head\ *\>)  # Inline styles into head
57         printf '%s<meta name="viewport" content="width=device-width">
58                 <meta charset="UTF-8">
59                 <style type="text/css"><!--\n' "${tag%${tag#*<head*>}}"
60         cat "${0%/*}/clickslide.css"
61         printf '\n--></style>%s' "${tag#*<head*>}"
62         tag='' depth=$((depth + 1))
63         ;;
64       *\<slide\ *id=\"?*\"*\>*)  # Count slide id, insert "next" button
65         prev="$next"
66         next="${tag#*<slide }" next="${next#*id=\"}" next="${next%\"*}"
67         next="autoslide${idoff}"
68         idoff="$((idoff + 1))"
69         printf '%s<a class="nextslide" href="#%s">next</a><div class="slide" count="%i" id="%s" %s' \
70           "${tag%<slide *}" "$next" "$idoff" "$next" "${tag#*<slide }"
71         tag='' depth=$((depth + 1))
72         ;;
73       *\<slide\ *\>*|*\<slide\>*)
74         prev="$next"
75         next="autoslide${idoff}"
76         idoff="$((idoff + 1))"
77         printf '%s<a class="nextslide" href="#%s">next</a><div class="slide" count="%i" id="%s" %s' \
78           "${tag%<slide*}" "$next" "$idoff" "$next" "${tag#*<slide}"
79         tag='' depth=$((depth + 1))
80         ;;
81       *\</slide\>*)  # Insert "previous" button
82         printf '%s</div><a class="prevslide" href="#%s">previous</a>%s' \
83           "${tag%</slide>*}" "$prev" "${tag#*</slide>}"
84         tag='' depth=$((depth - 1))
85         ;;
86       *\<body*\>*)  # Insert toplevel link
87         printf '%s<a href="#" class="toplevel">overview</a>' "$tag"
88         tag='' depth=$((depth + 1))
89         ;;
90       *\</body*\>*)  # Insert total slide count
91         printf '<span class="count">%i</span>%s' "$idoff" "$tag"
92         tag='' depth=$((depth - 1))
93         ;;
94       *\<*class=\"uncover\"*\>*)  # Insert checkboxes in "uncover" lists
95         #printf '%s<li></li>' "$tag"
96         printf '%s' "$tag"
97         tag='' depth=$((depth + 1))
98         ucdepth=$depth
99         ;;
100       *\</*\>*)
101         printf %s "$tag"
102         tag='' depth=$((depth - 1))
103         [ $depth -lt $ucdepth ] && ucdepth=-1
104         ;;
105       *\<*\>*)
106         if [ $ucdepth = $depth ]; then
107           printf '%s<input type="checkbox" class="uncover"/><%s' "${tag%<*}" "${tag#*<}"
108         else
109           printf %s "$tag"
110         fi
111         [ "${tag}" = "${tag%/>}" ] && depth=$((depth + 1))
112         tag=''
113         ;;
114       *) :
115         ;;
116     esac
117   done
118 }