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