]> git.plutz.net Git - shellwiki/blob - macros/include
Merge commit 'a7a6d7ad2cba7831544b160d9038cde2773dff4e'
[shellwiki] / macros / include
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/cgilite.sh"
4 . "$_EXEC/acl.sh"
5 . "$_EXEC/tools.sh"
6
7 from='1'; to='$'; rev=''; items='$'
8
9 while [ $# -gt 0 ]; do case $1 in
10   --from) from="$2"; shift 2;;
11   from=*) from="${1#*=}"; shift 1;;
12   --to) to="$2"; shift 2;;
13   to=*) to="${1#*=}"; shift 1;;
14   --items) items="$2"; shift 2;;
15   items=*) items="${1#*=}"; shift 1;;
16   --rev|--reverse) rev="-r"; shift 1;;
17   *) page="$1"; shift 1;;
18 esac; done
19
20 if ! printf %s\\n "$from" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
21   debug 'Include macro invalid argument: "from"'
22   exit 1
23 fi
24 if ! printf %s\\n "$to" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
25   debug 'Include macro Invalid argument: "to"'
26   exit 1
27 fi
28 if ! printf %s\\n "$items" |grep -qEx '\$|[0-9]+'; then
29   debug 'Include macro Invalid argument: "items"'
30   exit 1
31 fi
32
33 page_glob "$page" \
34 | sort $rev \
35 | sed "${items}q" \
36 | while read glob; do
37   page="$(page_abs "$glob")"
38   acl_read "$page" || continue
39   mdfile="$(mdfile "$page")" || continue
40   hglob="$(HTML "$glob")"
41   refpfx="$(printf %s\\n "$hglob" |sed 's;[\;&\;];\\&;g')"
42   printf '<div class="macro include">
43             <a class="include link" href="%s">%s</a>
44             <article class="include" id="include_%s">' \
45          "${hglob}" "${hglob}" "${hglob}"
46   ( # PATH_INFO may be used by macros in the included page
47     export PATH_INFO="$page"
48     cd -- "${mdfile%/*}/"
49     sed -n "${from},${to}p" <"$mdfile" \
50     | md \
51     | grep -vx ''
52   ) | sed -E '
53     s;(<[^>]+ )(href|src)="([^"]+://[^"]*|[mM][aA][iI][lL][tT][oO]:[^"]*)"([^>]*>);\1\2="/#safe/\3"\4;g
54     s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"${refpfx}"'\3"\4;g
55     s;(<[^>]+ )(href|src)="/#safe/([^"]*)"([^>]*>);\1\2="\3"\4;g
56   '
57   printf '</article></div>'
58 done