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