| 1 |
#!/bin/sh |
| 2 |
# Generates `lf.1` and `doc.txt` from the `doc.md` file. |
| 3 |
# |
| 4 |
# This script is used to generate a man page and a plain text conversion of the |
| 5 |
# markdown documentation using pandoc (https://pandoc.org/). GitHub Flavored |
| 6 |
# Markdown (GFM) (https://github.github.com/gfm/) is used for the markdown |
| 7 |
# input format. The markdown file is automatically rendered in the GitHub |
| 8 |
# repository (https://github.com/gokcehan/lf/blob/master/doc.md). The man page |
| 9 |
# file `lf.1` is meant to be used for installations on Unix systems. The plain |
| 10 |
# text file `doc.txt` is embedded in the binary to be displayed on request with |
| 11 |
# the `-doc` command line flag. Thus the same documentation is used for online |
| 12 |
# and terminal display. |
| 13 |
|
| 14 |
set -o errexit -o nounset |
| 15 |
|
| 16 |
get_version() { |
| 17 |
printf "r%s" $(($(git describe --tags --abbrev=0 | tr -d r) + 1)) |
| 18 |
} |
| 19 |
|
| 20 |
[ -z "${version:-}" ] && version=$(get_version) |
| 21 |
[ -z "${date:-}" ] && date=$(date +%F) |
| 22 |
|
| 23 |
PANDOC_IMAGE=pandoc/minimal:3.7 |
| 24 |
|
| 25 |
generate_man_page() { |
| 26 |
"${OCI_PROGRAM?}" run \ |
| 27 |
--rm \ |
| 28 |
--volume "$PWD:/data" \ |
| 29 |
"$@" "$PANDOC_IMAGE" \ |
| 30 |
--standalone \ |
| 31 |
--lua-filter /data/gen/deflist.lua \ |
| 32 |
--from gfm --to man \ |
| 33 |
--metadata=title:"LF" \ |
| 34 |
--metadata=section:"1" \ |
| 35 |
--metadata=date:"$date" \ |
| 36 |
--metadata=footer:"$version" \ |
| 37 |
--metadata=header:"DOCUMENTATION" \ |
| 38 |
doc.md -o lf.1 |
| 39 |
} |
| 40 |
|
| 41 |
generate_plain_text() { |
| 42 |
"${OCI_PROGRAM?}" run \ |
| 43 |
--rm \ |
| 44 |
--volume "$PWD:/data" \ |
| 45 |
"$@" "$PANDOC_IMAGE" \ |
| 46 |
--standalone \ |
| 47 |
--lua-filter /data/gen/deflist.lua \ |
| 48 |
--from gfm --to plain \ |
| 49 |
doc.md -o doc.txt |
| 50 |
} |
| 51 |
|
| 52 |
is_rootless() { |
| 53 |
case "$OCI_PROGRAM" in |
| 54 |
podman) podman info -f '{{.Host.Security.Rootless}}' | grep -q true ;; |
| 55 |
docker) docker info -f '{{.SecurityOptions}}' | grep -q rootless ;; |
| 56 |
*) echo >&2 \ |
| 57 |
"Unknown OCI program \"$OCI_PROGRAM\", assuming rootless mode" ;; |
| 58 |
esac |
| 59 |
} |
| 60 |
|
| 61 |
# You can set your own OCI_PROGRAM, which assumes it runs in rootless mode. |
| 62 |
if [ -z "${OCI_PROGRAM:-}" ]; then |
| 63 |
if command -v podman > /dev/null; then |
| 64 |
OCI_PROGRAM=podman |
| 65 |
elif command -v docker > /dev/null; then |
| 66 |
OCI_PROGRAM=docker |
| 67 |
fi |
| 68 |
fi |
| 69 |
|
| 70 |
if is_rootless; then |
| 71 |
generate_man_page |
| 72 |
else |
| 73 |
generate_man_page --user "$(id -u):$(id -g)" |
| 74 |
fi |
| 75 |
|
| 76 |
if is_rootless; then |
| 77 |
generate_plain_text |
| 78 |
else |
| 79 |
generate_plain_text --user "$(id -u):$(id -g)" |
| 80 |
fi |
| 81 |
|
| 82 |
# vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 |