]> git.plutz.net Git - shellwiki/blob - macros/include
Merge commit '6bc502434737d7f08379e79b94fc6fda424ef779'
[shellwiki] / macros / include
1 #!/bin/sh
2
3 # Copyright 2022 - 2023 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='$'; hl=0; link='true'; depth=0; tags=''; ntags=''; page='';
22
23 set -- "$@" --
24 while [ $# -gt 0 ]; do case $1 in
25   --from) from="$2"; shift 2;;
26   from=*) from="${1#*=}"; shift 1;;
27   --to) to="$2"; shift 2;;
28   to=*) to="${1#*=}"; shift 1;;
29   --items) items="$2"; shift 2;;
30   items=*) items="${1#*=}"; shift 1;;
31   --rev|--reverse) rev="-r"; shift 1;;
32   --nolink) link=""; shift 1;;
33   --hl|-hl) hl=$2; shift 2;;
34   --depth) depth=$2; shift 2;;
35   \#*) tags="${tags}${tags:+ }${1}"; shift 1;;
36   \!*) ntags="${ntags}${ntags:+ }${1}"; shift 1;;
37   --) shift 1; break;;
38   *) set -- "$@" "$1"; shift 1;;
39 esac; done
40
41 if ! printf %s\\n "$from" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
42   debug 'Include macro invalid argument: "from"'
43   exit 1
44 fi
45 if ! printf %s\\n "$to" |grep -qEx '\$|[0-9]+|/([^/\\]|\\/|\\.)*/'; then
46   debug 'Include macro Invalid argument: "to"'
47   exit 1
48 fi
49 if ! printf %s\\n "$items" |grep -qEx '\$|[0-9]+'; then
50   debug 'Include macro Invalid argument: "items"'
51   exit 1
52 fi
53
54 for page in "$@"; do
55   page_glob "$page" "$depth"
56 done \
57 | sort $rev \
58 | sed "${items}q" \
59 | while read glob; do
60   page="$(page_abs "$glob")"
61   mdfile="$(mdfile "$page")" || continue
62   acl_read "$page" || continue
63   has_tags "$page" $tags || continue
64   has_tag "$page" $ntags && continue
65   printf %s\\n "$INCLUDE_LIST" |grep -qxF "$page" && continue
66   export INCLUDE_LIST="${INCLUDE_LIST}${INCLUDE_LIST:+${BR}}$page"
67   hglob="$(HTML "$glob")"
68   refpfx="$(printf %s\\n "$hglob" |sed 's;[\;&\;];\\&;g')"
69   [ "$link" ] \
70   && printf '<div class="macro include">
71                <a class="include link" href="%s">%s</a>
72                <article class="include" id="include_%s">' \
73             "${hglob}" "${hglob}" "${hglob}" \
74   || printf '<div class="macro include">
75                <article class="include" id="include_%s">' \
76             "${hglob}"
77   ( # PATH_INFO may be used by macros in the included page
78     export PATH_INFO="$page"
79     cd -- "${mdfile%/*}/"
80     sed -n "${from},${to}p" <"$mdfile" \
81     | md \
82     | grep -vx ''
83   ) | sed -E '
84     s;(<[^>]+ )(href|src)="([^"]+://[^"]*|[mM][aA][iI][lL][tT][oO]:[^"]*)"([^>]*>);\1\2="/#safe/\3"\4;g
85     s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"${refpfx}"'\3"\4;g
86     s;(<[^>]+ )(href|src)="/#safe/([^"]*)"([^>]*>);\1\2="\3"\4;g
87   ' | case $hl in
88       1) sed -E 's;(<h|</h)5( |>);\16\2;g;
89                  s;(<h|</h)4( |>);\15\2;g;
90                  s;(<h|</h)3( |>);\14\2;g;
91                  s;(<h|</h)2( |>);\13\2;g;
92                  s;(<h|</h)1( |>);\12\2;g;
93          ';;
94       2) sed -E 's;(<h|</h)5( |>);\16\2;g;
95                  s;(<h|</h)4( |>);\16\2;g;
96                  s;(<h|</h)3( |>);\15\2;g;
97                  s;(<h|</h)2( |>);\14\2;g;
98                  s;(<h|</h)1( |>);\13\2;g;
99          ';;
100       3) sed -E 's;(<h|</h)5( |>);\16\2;g;
101                  s;(<h|</h)4( |>);\16\2;g;
102                  s;(<h|</h)3( |>);\16\2;g;
103                  s;(<h|</h)2( |>);\15\2;g;
104                  s;(<h|</h)1( |>);\14\2;g;
105          ';;
106       4) sed -E 's;(<h|</h)5( |>);\16\2;g;
107                  s;(<h|</h)4( |>);\16\2;g;
108                  s;(<h|</h)3( |>);\16\2;g;
109                  s;(<h|</h)2( |>);\16\2;g;
110                  s;(<h|</h)1( |>);\15\2;g;
111          ';;
112       5|[6789])
113          sed -E 's;(<h|</h)5( |>);\16\2;g;
114                  s;(<h|</h)4( |>);\16\2;g;
115                  s;(<h|</h)3( |>);\16\2;g;
116                  s;(<h|</h)2( |>);\16\2;g;
117                  s;(<h|</h)1( |>);\16\2;g;
118          ';;
119       *) cat;;
120     esac
121   printf '</article></div>'
122 done