]> git.plutz.net Git - shellwiki/blob - macros/include
omit inclusion of user functions (unnecessary since variables are now exported)
[shellwiki] / macros / include
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/cgilite.sh"
4 . "$_EXEC/acl.sh"
5 . "$_EXEC/tools.sh"
6
7 from=1
8 to=\$
9 rev=''
10 items=\$
11 path_info="$PATH_INFO"
12
13 while [ $# -gt 0 ]; do case $1 in
14   --from) from="$2"; shift 2;;
15   from=*) from="${1#*=}"; shift 1;;
16   --to) to="$2"; shift 2;;
17   to=*) to="${1#*=}"; shift 1;;
18   --items) items="$2"; shift 2;;
19   items=*) items="${1#*=}"; shift 1;;
20   --rev|--reverse) rev="-r"; shift 1;;
21   *) page="$1"; shift 1;;
22 esac; done
23
24 if ! printf %s\\n "$from" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
25   debug 'Include macro invalid argument: "from"'
26   exit 1
27 fi
28 if ! printf %s\\n "$to" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
29   debug 'Include macro Invalid argument: "to"'
30   exit 1
31 fi
32 if ! printf %s\\n "$items" |grep -qEx '\$|[0-9]+'; then
33   debug 'Include macro Invalid argument: "items"'
34   exit 1
35 fi
36
37 page_glob "$page" \
38 | sort $rev \
39 | sed "${items}q" \
40 | while read glob; do
41   case $glob in
42     /*) export PATH_INFO="$(PATH "$glob")";;
43      *) export PATH_INFO="$(PATH "$path_info/$glob")";;
44   esac
45   acl_read || continue
46   if   [ -f "$_DATA/pages/$PATH_INFO/#page.md" ]; then
47      mdfile="$_DATA/pages/$PATH_INFO/#page.md"
48   elif [ -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then
49      mdfile="$_EXEC/pages/$PATH_INFO/#page.md"
50   else
51     continue
52   fi
53   printf '<div class="macro include" page="%s">' "$(HTML "$glob")"
54   ( cd -- "${mdfile%/*}"
55     sed -n "${from},${to}p" <"$mdfile" \
56     | md \
57     | grep -vx ''
58   ) | sed -E '
59     s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"$(HTML "$glob")"'\3"\4;g
60   '
61   printf '</div>'
62 done