]> git.plutz.net Git - shellwiki/blob - macros/include
f6b3a34ced7f4c22b700b9b4886e1070c8c2f421
[shellwiki] / macros / include
1 #!/bin/sh
2
3 # Copyright 2022 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . "$_EXEC/cgilite/cgilite.sh"
18 . "$_EXEC/acl.sh"
19 . "$_EXEC/tools.sh"
20
21 from='1'; to='$'; rev=''; items='$'; link='true'
22
23 while [ $# -gt 0 ]; do case $1 in
24   --from) from="$2"; shift 2;;
25   from=*) from="${1#*=}"; shift 1;;
26   --to) to="$2"; shift 2;;
27   to=*) to="${1#*=}"; shift 1;;
28   --items) items="$2"; shift 2;;
29   items=*) items="${1#*=}"; shift 1;;
30   --rev|--reverse) rev="-r"; shift 1;;
31   --nolink) link=""; shift 1;;
32   *) page="$1"; shift 1;;
33 esac; done
34
35 if ! printf %s\\n "$from" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
36   debug 'Include macro invalid argument: "from"'
37   exit 1
38 fi
39 if ! printf %s\\n "$to" |grep -qEx '\$|[0-9]+|/([^/\\]|\\/|\\.)*/'; then
40   debug 'Include macro Invalid argument: "to"'
41   exit 1
42 fi
43 if ! printf %s\\n "$items" |grep -qEx '\$|[0-9]+'; then
44   debug 'Include macro Invalid argument: "items"'
45   exit 1
46 fi
47
48 page_glob "$page" \
49 | sort $rev \
50 | sed "${items}q" \
51 | while read glob; do
52   page="$(page_abs "$glob")"
53   acl_read "$page" || continue
54   mdfile="$(mdfile "$page")" || continue
55   hglob="$(HTML "$glob")"
56   refpfx="$(printf %s\\n "$hglob" |sed 's;[\;&\;];\\&;g')"
57   [ "$link" ] \
58   && printf '<div class="macro include">
59                <a class="include link" href="%s">%s</a>
60                <article class="include" id="include_%s">' \
61             "${hglob}" "${hglob}" "${hglob}" \
62   || printf '<div class="macro include">
63                <article class="include" id="include_%s">' \
64             "${hglob}"
65   ( # PATH_INFO may be used by macros in the included page
66     export PATH_INFO="$page"
67     cd -- "${mdfile%/*}/"
68     sed -n "${from},${to}p" <"$mdfile" \
69     | md \
70     | grep -vx ''
71   ) | sed -E '
72     s;(<[^>]+ )(href|src)="([^"]+://[^"]*|[mM][aA][iI][lL][tT][oO]:[^"]*)"([^>]*>);\1\2="/#safe/\3"\4;g
73     s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"${refpfx}"'\3"\4;g
74     s;(<[^>]+ )(href|src)="/#safe/([^"]*)"([^>]*>);\1\2="\3"\4;g
75   '
76   printf '</article></div>'
77 done