]> git.plutz.net Git - shellwiki/blob - macros/include
Merge commit '3e23ea79fad3824fcf71532ff605bd753f83c0b3'
[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 printf '<div class="macro include wrapper">'
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   hglob="$(HTML "$glob")"
55   refpfx="$(printf %s\\n "$hglob" |sed 's;[\;&\;];\\&;g')"
56   printf '<div class="macro include page"><article class="include" id="include_%s">' "${hglob}"
57   ( cd -- "${mdfile%/*}"
58     sed -n "${from},${to}p" <"$mdfile" \
59     | md \
60     | grep -vx ''
61   ) | sed -E '
62     s;(<[^>]+ )(href|src)="([^"]+://[^"]*|[mM][aA][iI][lL][tT][oO]:[^"]*)"([^>]*>);\1\2="/#safe/\3"\4;g
63     s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"${refpfx}"'\3"\4;g
64     s;(<[^>]+ )(href|src)="/#safe/([^"]*)"([^>]*>);\1\2="\3"\4;g
65   '
66   printf '</article>'
67   printf '<a class="include link" href="%s">%s</a>' "${hglob}" "${hglob}"
68   printf '</div>'
69 done
70 printf '</div>'