From: Jordan Williams Date: Tue, 26 Nov 2024 15:58:06 +0000 (-0600) Subject: meson: generate man page translations X-Git-Tag: v2.42-start~47^2~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=44e4e2c0a6cbfd77b97286f31b0fb02ba4b496ac;p=thirdparty%2Futil-linux.git meson: generate man page translations Signed-off-by: Jordan Williams --- diff --git a/meson.build b/meson.build index 74d1f542a..141540f91 100644 --- a/meson.build +++ b/meson.build @@ -4070,6 +4070,8 @@ if asciidoctor.found() 'man' + target_section / target, mandir / 'man' + link_section / link_name) endforeach + + subdir('po-man') endif if bash_completion.found() diff --git a/po-man/install-translations.sh b/po-man/install-translations.sh new file mode 100755 index 000000000..9969d0e00 --- /dev/null +++ b/po-man/install-translations.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +set -eou pipefail + +function usage() +{ + cat << HEREDOC + + Usage: + $PROGRAM --mandir --mansrcdir [...] + +Install translated man pages. + + Options: + --help show this help message and exit + --mandir directory in which to install the man pages, usually share/man + --mansrcdir directory containing the man pages to install + + Environment Variables: + MESON_INSTALL_PREFIX install destination prefix directory + DESTDIR install destination directory + +HEREDOC +} + +DESTDIR="${DESTDIR:-''}" +MANPAGES=() +PROGRAM=$(basename "$0") + +while [[ $# -gt 0 ]]; do + case $1 in + --help) + usage + exit 0 + ;; + --mandir) + MANDIR="$2" + shift + shift + ;; + --mansrcdir) + MANSRCDIR="$2" + shift + shift + ;; + --*|-*) + echo "Unknown option $1" + usage + exit 1 + ;; + *) + MANPAGES+=("$1") + shift + ;; + esac +done + +set -- "${MANPAGES[@]}" + +if [ ${#MANPAGES[@]} -eq 0 ]; then + shopt -s nullglob + MANPAGES=("$MANSRCDIR"/*/man[0-9]/*\.[0-9]) +fi + +for LOCALEDIR in "$MANSRCDIR"/*/; do + LOCALE=$(basename "$LOCALEDIR") + for MANPAGE in "${MANPAGES[@]}"; do + MANPAGE=$(basename "$MANPAGE") + SECTION="${MANPAGE##*.}" + PAGE="$LOCALEDIR/man$SECTION/$MANPAGE" + if [ -f "$PAGE" ]; then + if [ -z ${MESON_INSTALL_QUIET+x} ]; then + echo "Installing $PAGE to $DESTDIR/$MESON_INSTALL_PREFIX/$MANDIR/$LOCALE/man$SECTION" + fi + install -D --mode=0644 --target-directory="$DESTDIR/$MESON_INSTALL_PREFIX/$MANDIR/$LOCALE/man$SECTION" "$PAGE" + fi + done +done diff --git a/po-man/meson.build b/po-man/meson.build new file mode 100644 index 000000000..e7298a5a6 --- /dev/null +++ b/po-man/meson.build @@ -0,0 +1,38 @@ +po4a = find_program('po4a', required : false) + +if not (asciidoctor.found() and po4a.found()) + subdir_done() +endif + +awk = find_program('awk', required : true) +locales_output = run_command(awk, '/\[po4a_langs\]/ {for (i=2; i<=NF; i++) print $i}', 'po4a.cfg', check : true) +locales = locales_output.stdout().strip().split('\n') +po_files = [] +foreach locale : locales + po_files += locale + '.po' +endforeach + +custom_target('translate', + build_by_default : true, + command : ['translate.sh', + '--srcdir', meson.current_source_dir(), + '--destdir', meson.current_build_dir() / 'translations', + '--asciidoctor-load-path', meson.project_source_root() / 'tools', + '--docdir', docdir, + '--po4acfg', meson.current_source_dir() / 'po4a.cfg', + '--util-linux-version', meson.project_version(), + '@INPUT@'], + console : true, + depend_files : ['po4a.cfg', 'util-linux-man.pot'] + po_files, + input : man_common_adocs + manadocs, + output : ['translations']) + +manpages = [] +foreach adoc : manadocs + manpages += fs.stem(adoc) +endforeach + +meson.add_install_script('install-translations.sh', + '--mandir', get_option('mandir'), + '--mansrcdir', meson.current_build_dir() / 'translations/man', + manpages) diff --git a/po-man/translate.sh b/po-man/translate.sh new file mode 100755 index 000000000..16cece5fb --- /dev/null +++ b/po-man/translate.sh @@ -0,0 +1,150 @@ +#!/usr/bin/env bash +set -eou pipefail + +function usage() +{ + cat << HEREDOC + + Usage: $PROGRAM --srcdir --destdir --asciidoctor-load-path --docdir --po4acfg --util-linux-version [...] + +Translate Asciidoc man page source files and generate man pages. + + Options: + --help show this help message and exit + --srcdir directory containing the asciidoc files to translate + --destdir directory in which to place the translated asciidoc files and man pages + --asciidoctor-load-path value for the --load-path option passed to the Asciidoctor command + --docdir directory where the package documentation will be installed + --util-linux-version version of util-linux to include in the man pages + --po4acfg path to the po4a.cfg file + +HEREDOC +} + +PROGRAM=$(basename "$0") + +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 + ;; + --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 [[ ! " ${PO4ACFG_TRANSLATIONS[*]} " =~ [[:space:]]${ADOC}[[:space:]] ]]; then + continue + fi + PO4A_TRANSLATE_ONLY_FLAGS+=("--translate-only") + ADOC_NAME=$(basename "$ADOC") + 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 + +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##*.}" + 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 \ + "$ADOC" +done