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