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