Repository navigation
Expand file tree
/
Copy pathtoc.bash
More file actions
executable file
·94 lines (88 loc) · 3.32 KB
/
Copy pathtoc.bash
File metadata and controls
executable file
·94 lines (88 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
#
# toc.bash writes the site's table of contents, /.well-known/toc.opml, for a
# BonBon browser or cmarkbrowser, from the Markdown pages named on the command
# line.
#
# toc.bash BASE_URL SITE_TITLE PAGE.md [PAGE.md ...] > .well-known/toc.opml
#
# BASE_URL is where the site is published, ending in a slash. Pages are listed
# in the order given, in one "Pages" group, each with its title (the title: of
# its YAML front matter, else its first "# " heading) and the address of its
# Markdown file (a manual page as name(section), about.md as "About SITE_TITLE").
# README.md is listed as
# index.md, the page a BonBon browser asks for at the site's address. The output
# is the same for the same input (the date is the last commit's, not today's),
# so a rebuild changes nothing in git.
#
set -eu
if [ "$#" -lt 3 ]; then
echo "usage: toc.bash BASE_URL SITE_TITLE PAGE.md [PAGE.md ...]" >&2
exit 2
fi
BASE_URL="$1"
SITE_TITLE="$2"
shift 2
case "${BASE_URL}" in
http://*/ | https://*/) ;;
*)
echo "toc.bash: BASE_URL must be an http or https address ending in /: ${BASE_URL}" >&2
exit 2
;;
esac
# xml_escape quotes the characters that matter in an XML attribute or text. It
# uses sed: in bash 5.2 an "&" in the replacement of ${s//x/y} means "the
# matched text", which turned "<" into "<lt;".
xml_escape() {
printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/"/\"/g'
}
# page_title is the title a page gives itself in the table of contents, which
# a reader shows as a list of pages, so each must differ from the others: a
# manual page is "name(section)" from its YAML front matter; about.md is
# "About" and the project's name; any other page is the title: of its front
# matter, else the text of its first "# " heading.
page_title() {
local t sec
t=$(awk 'NR==1 && $0!="---" {exit} NR>1 && $0=="---" {exit} NR>1 && /^title:/ {sub(/^title: */, ""); gsub(/^["'\'']|["'\'']$/, ""); print; exit}' "$1")
sec=$(awk 'NR==1 && $0!="---" {exit} NR>1 && $0=="---" {exit} NR>1 && /^section:/ {sub(/^section: */, ""); print; exit}' "$1")
if [ -n "${t}" ] && [ -n "${sec}" ]; then
t="${t}(${sec})"
elif [ "$1" = "about.md" ]; then
t="About ${SITE_TITLE}"
elif [ -z "${t}" ]; then
t=$(sed -n 's/^# \{1,\}\(.*[^ ]\) *$/\1/p' "$1" | head -1)
fi
printf '%s' "${t}"
}
# The date of the last commit that touched a page, or the newest file time
# when there is no git history; never the current time.
modified() {
local d
d=$(git log -1 --format=%cD -- "$@" 2>/dev/null || true)
if [ -z "${d}" ]; then
d=$(LC_ALL=C date -u -r "$(ls -t "$@" | head -1)" '+%a, %d %b %Y %H:%M:%S GMT')
fi
printf '%s' "${d}"
}
printf '<?xml version="1.0" encoding="UTF-8"?>\n'
printf '<opml version="2.0">\n'
printf '\t<head>\n\t\t<title>%s</title>\n\t\t<dateModified>%s</dateModified>\n\t</head>\n' \
"$(xml_escape "${SITE_TITLE}")" "$(modified "$@")"
printf '\t<body>\n\t\t<outline text="Pages">\n'
for page in "$@"; do
if [ ! -f "${page}" ]; then
echo "toc.bash: ${page}: no such page" >&2
exit 66
fi
title=$(page_title "${page}")
if [ -z "${title}" ]; then
title="${page%.md}"
fi
published="${page}"
if [ "${page}" = "README.md" ]; then
published="index.md"
fi
printf '\t\t\t<outline text="%s" url="%s%s"></outline>\n' \
"$(xml_escape "${title}")" "${BASE_URL}" "${published}"
done
printf '\t\t</outline>\n\t</body>\n</opml>\n'