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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#!/usr/bin/env bash
set -eou pipefail
function usage()
{
cat << HEREDOC
Usage: $PROGRAM --srcdir <srcdir> --destdir <destdir> --asciidoctor-load-path <directory> --docdir <docdir> --po4acfg <file> --util-linux-version <version> [<asciidoc file>...]
Translate Asciidoc man page source files and generate man pages.
Options:
--help show this help message and exit
--progress report the current progress
--srcdir <srcdir> directory containing the asciidoc files to translate
--destdir <destdir> directory in which to place the translated asciidoc files and man pages
--asciidoctor-load-path <directory> value for the --load-path option passed to the Asciidoctor command
--docdir <docdir> directory where the package documentation will be installed
--util-linux-version <version> version of util-linux to include in the man pages
--po4acfg <file> path to the po4a.cfg file
HEREDOC
}
PROGRAM=$(basename "$0")
PROGRESS=false
while [[ $# -gt 0 ]]; do
case $1 in
--srcdir)
SRCDIR="$2"
shift
shift
;;
--destdir)
DESTDIR="$2"
shift
shift
;;
--asciidoctor-load-path)
ASCIIDOCTOR_LOAD_PATH="$2"
shift
shift
;;
--docdir)
DOCDIR="$2"
shift
shift
;;
--help)
usage
exit 0
;;
--po4acfg)
PO4ACFG="$2"
shift
shift
;;
--progress)
PROGRESS=true
shift
;;
--util-linux-version)
UTIL_LINUX_VERSION="$2"
shift
shift
;;
--*|-*)
echo "Unknown option $1"
usage
exit 1
;;
*)
ADOCS+=("$1")
shift
;;
esac
done
set -- "${ADOCS[@]}"
mapfile -t LOCALES < <( awk '/\[po4a_langs\]/ {for (i=2; i<=NF; i++) print $i}' "$PO4ACFG" )
mapfile -t PO4ACFG_TRANSLATIONS < <( awk '/\[type:asciidoc\]/ {print $2;}' "$PO4ACFG" )
mkdir --parents "$DESTDIR"
DESTDIR=$( OLDPWD=- CDPATH='' cd -P -- "$DESTDIR" && pwd )
MANADOCS=()
PO4A_TRANSLATE_ONLY_FLAGS=()
for LOCALE in "${LOCALES[@]}"; do
for ADOC in "${ADOCS[@]}"; do
if [[ "$ADOC" == *"/man-common/manpage-stub.adoc" ]]; then
continue
fi
ADOC_NAME=$(basename "$ADOC")
if [[ ! " ${PO4ACFG_TRANSLATIONS[*]} " =~ .*${ADOC_NAME}[[:space:]] ]]; then
echo "unconfigured in $PO4ACFG: $ADOC"
continue
fi
PO4A_TRANSLATE_ONLY_FLAGS+=("--translate-only")
if [[ "$ADOC" == *"/man-common/"* ]]; then
PO4A_TRANSLATE_ONLY_FLAGS+=("$LOCALE/man-common/$ADOC_NAME")
else
MANADOCS+=("$LOCALE/$ADOC_NAME")
PO4A_TRANSLATE_ONLY_FLAGS+=("$LOCALE/$ADOC_NAME")
fi
done
done
if [ ${#MANADOCS[@]} -eq 0 ] && [ ${#PO4A_TRANSLATE_ONLY_FLAGS[@]} -gt 0 ]; then
echo "Only man-common Asciidoc files were supplied"
exit 1
fi
# Only version 0.72 and later of po4a properly support the --translate-only flag.
PO4A_VERSION=$(po4a --version | { read -r _ _ v; echo "${v%*.}"; })
if echo "0.72" "$PO4A_VERSION" | sort --check --version-sort; then
PO4A_TRANSLATE_ONLY_FLAGS=("--no-update")
fi
[ "$PROGRESS" = true ] && echo "po4a: generate man-pages translations"
DISCARDED_TRANSLATIONS=()
output=$(po4a --srcdir "$SRCDIR" --destdir "$DESTDIR" "${PO4A_TRANSLATE_ONLY_FLAGS[@]}" "$PO4ACFG")
while IFS= read -r line; do
DISCARDED_TRANSLATION=$(echo "$line" | awk '/Discard/ {print $2;}')
if [ -n "${DISCARDED_TRANSLATION+x}" ]; then
DISCARDED_TRANSLATIONS+=("$DISCARDED_TRANSLATION")
fi
done <<< "$output"
TRANSLATED_MANADOCS=()
if [ ${#MANADOCS[@]} -eq 0 ]; then
for LOCALE in "${LOCALES[@]}"; do
shopt -s nullglob
TRANSLATED_MANADOCS=("$DESTDIR/$LOCALE"/*\.adoc)
done
else
for MANADOC in "${MANADOCS[@]}"; do
if [[ ! " ${DISCARDED_TRANSLATIONS[*]} " =~ [[:space:]]${MANADOC}[[:space:]] ]]; then
TRANSLATED_MANADOCS+=("$DESTDIR/$MANADOC")
fi
done
fi
for ADOC in "${TRANSLATED_MANADOCS[@]}"; do
LOCALE=$(basename "$(dirname "$ADOC")")
PAGE="${ADOC%.*}"
SECTION="${PAGE##*.}"
if [ "$PROGRESS" = true ]; then
PAGENAME=$(basename $PAGE)
echo " GEN " $LOCALE ": " $PAGENAME
fi
asciidoctor \
--backend manpage \
--attribute VERSION="$UTIL_LINUX_VERSION" \
--attribute release-version="$UTIL_LINUX_VERSION" \
--attribute ADJTIME_PATH=/etc/adjtime \
--attribute package-docdir="$DOCDIR" \
--base-dir "$DESTDIR/$LOCALE" \
--destination-dir "$DESTDIR/man/$LOCALE/man$SECTION" \
--load-path "$ASCIIDOCTOR_LOAD_PATH" \
--require asciidoctor-includetracker \
--require asciidoctor-unicodeconverter \
--trace \
"$ADOC"
done
|