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