]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
meson: generate man page translations
authorJordan Williams <jordan@jwillikers.com>
Tue, 26 Nov 2024 15:58:06 +0000 (09:58 -0600)
committerKarel Zak <kzak@redhat.com>
Wed, 12 Feb 2025 09:52:47 +0000 (10:52 +0100)
Signed-off-by: Jordan Williams <jordan@jwillikers.com>
meson.build
po-man/install-translations.sh [new file with mode: 0755]
po-man/meson.build [new file with mode: 0644]
po-man/translate.sh [new file with mode: 0755]

index 74d1f542a6ec1ba8f1bf4d9c988cdb703748b928..141540f91f71bc41a36ffc1042d919ef8fc074c1 100644 (file)
@@ -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 (executable)
index 0000000..9969d0e
--- /dev/null
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+set -eou pipefail
+
+function usage()
+{
+    cat << HEREDOC
+
+ Usage:
+  $PROGRAM --mandir <directory> --mansrcdir <directory> [<man page name>...]
+
+Install translated man pages.
+
+ Options:
+  --help                             show this help message and exit
+  --mandir <mandir>                  directory in which to install the man pages, usually share/man
+  --mansrcdir <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 (file)
index 0000000..e7298a5
--- /dev/null
@@ -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 (executable)
index 0000000..16cece5
--- /dev/null
@@ -0,0 +1,150 @@
+#!/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
+  --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")
+
+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