summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2014-06-27 19:33:06 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2014-06-27 19:33:06 -0400
commitd7763f0a6cb902e969305d02a18ec13d84227d9d (patch)
treecc343ee3a753bf60aaedc9298c215703c30756e7
parentfb94599dcea71cdf2cb448e1545cfbdc526288a1 (diff)
Add librexgettext, to make generating .pot file easy for librelib users.
I don't like how much longer this is than the Makefile, but most of it is option parsing and usage text.
-rw-r--r--common.mk15
-rw-r--r--src/chroot-tools/Makefile2
-rw-r--r--src/fullpkg/Makefile2
-rw-r--r--src/lib/Makefile7
-rwxr-xr-xsrc/lib/librexgettext110
5 files changed, 118 insertions, 18 deletions
diff --git a/common.mk b/common.mk
index f8421d4..44896e2 100644
--- a/common.mk
+++ b/common.mk
@@ -22,17 +22,6 @@ indent = emacs --batch $1 \
# Normalize a .po(t) file
pofmt = msguniq -Fi
-xgettext-keywords-sh += --keyword={_,print}
-xgettext-keywords-sh += --keyword={msg,msg2,warning,error,stat_busy,die}
-xgettext-keywords-sh += --keyword={lock,slock}:3
-
-xgettext-sh = xgettext --omit-header --from-code=UTF-8 -L shell -o -
-xgettext-sh-std = $(xgettext-sh) $(xgettext-keywords-sh) $^
-
-pot-remove-wrap = tr '\n' '\r' | sed 's/"\r\s*"//g' | tr '\r' '\n'
-html-whitespace-collapse = sed -r -e 's/(\\n|\\t|\t)/ /g' -e 's/ +/ /g'
-xgettext-sh-prose = $(xgettext-sh) -k --keyword={prose,bullet,flag:2} $^ | $(pot-remove-wrap) | $(html-whitespace-collapse) | sed '/^\#, sh-format/d'
-
# Usage: as a variable
# relative path to `/` from $(bindir)
# used for symlinking libexec files
@@ -112,8 +101,8 @@ pot: everything.pot
everything.pot: $(pot_files)
cat $^ | $(pofmt) > '$@'
-%.pot: %
- { $(xgettext-sh-std); $(xgettext-sh-prose); } | $(pofmt) > '$@'
+%.pot: % $(topdir)/src/lib/librexgettext
+ $(topdir)/src/lib/librexgettext $(LIBREXGETTEXT_FLAGS) $< > '$@'
# Install ######################################################################
diff --git a/src/chroot-tools/Makefile b/src/chroot-tools/Makefile
index e44ad5f..106aa3a 100644
--- a/src/chroot-tools/Makefile
+++ b/src/chroot-tools/Makefile
@@ -15,4 +15,4 @@ include ../../common.mk
makechrootpkg.sh.in: %.sh.in: $(devtoolsdir)/%.in
cp $< $@
-distcc-tool.pot: xgettext-keywords-sh+=--keyword=errusage
+distcc-tool.pot: LIBREXGETTEXT_FLAGS+=--simple=errusage
diff --git a/src/fullpkg/Makefile b/src/fullpkg/Makefile
index ee9c395..8860e93 100644
--- a/src/fullpkg/Makefile
+++ b/src/fullpkg/Makefile
@@ -1,3 +1,3 @@
include ../../common.mk
-fullpkg-build.pot: xgettext-keywords-sh+=--keyword=list_pkgs:2
+fullpkg-build.pot: LIBREXGETTEXT_FLAGS+=--simple=list_pkgs:2
diff --git a/src/lib/Makefile b/src/lib/Makefile
index 495abb1..3d2d8f9 100644
--- a/src/lib/Makefile
+++ b/src/lib/Makefile
@@ -27,11 +27,12 @@ common.sh: %: %.in %.top Makefile
# Translate ##########################################################
-libreblacklist.pot: libreblacklist
+libreblacklist.pot: libreblacklist librexgettext
{ \
sed -n '/^# Usage:/,/()/{ /^#/ { =; p; } }' $< | \
sed -r -e 's/^# (.*)/msgid "\1"\nmsgstr ""\n/' \
-e 's/^[0-9]*$$/#. embedded usage text\n#: $<:&/'; \
- $(xgettext-sh-prose); \
- $(xgettext-sh-std); \
+ ./librexgettext $<; \
} | $(pofmt) > $@
+
+librexgettext.pot: LIBREXGETTEXT_FLAGS+=--simple=errusage
diff --git a/src/lib/librexgettext b/src/lib/librexgettext
new file mode 100755
index 0000000..f680784
--- /dev/null
+++ b/src/lib/librexgettext
@@ -0,0 +1,110 @@
+#!/usr/bin/env bash
+# Copyright (C) 2013-2014 Luke Shumaker <lukeshu@sbcglobal.net>
+#
+# License: GNU GPLv2+
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+export TEXTDOMAIN='librelib'
+export TEXTDOMAINDIR='/usr/share/locale'
+
+default_simple=(
+ --keyword={eval_,}{gettext,'ngettext:1,2'}
+ --keyword={_,print}
+ --keyword={msg,msg2,warning,error,stat_busy,die}
+ --keyword={lock,slock}:3
+)
+default_prose=(--keyword={prose,bullet,flag:2})
+
+readonly default_simple default_prose
+
+if ! type gettext &>/dev/null; then
+ gettext() { echo "$@"; }
+fi
+
+errusage() {
+ if [[ $# -gt 0 ]]; then
+ fmt="$(gettext "$1")"; shift
+ printf "${0##*/}: $fmt\n" "$@"
+ fi
+ usage >&2
+}
+
+usage() {
+ . libremessages
+ print 'Usage: %s [OPTIONS] FILES...' "${0##*/}"
+ print 'Generates .pot files for programs using libremessages'
+ echo
+ prose 'The keyword format is the same as in GNU xgettext.'
+ echo
+ prose 'The default simple keywords are: %s' "${default_simple[*]#--keyword=}"
+ echo
+ prose 'The default prose keywords are: %s' "${default_prose[*]#--keyword=}"
+ echo
+ print 'Options:'
+ flag '--simple=KEYWORD' 'Look for KEYWORD as an additional simple keyword'
+ flag '--prose=KEYWORD' 'Look for KEYWORD as an additional prose keyword'
+ flag '-k' 'Disable using the default keywords'
+ flag '-h, --help' 'Show this text'
+}
+
+xgettext-sh() {
+ xgettext --omit-header --from-code=UTF-8 -L shell -k -o - "$@"
+}
+
+main() {
+ local simple=()
+ local prose=()
+ local files=()
+ local use_defaults=true
+ local error=false
+
+ declare -i i
+ for (( i=1; i <= $#; i++ )); do
+ case "${!i}" in
+ --simple) i+=1; simple+=(--keyword="${!i}");;
+ --simple=*) simple+=(--keyword="${!i#*=}");;
+ --prose) i+=1; prose+=(--keyword="${!i}");;
+ --prose=*) prose+=(--keyword="${!i#*=}");;
+ -k) use_defaults=false;;
+ --help|-h) usage; return 0;;
+ --) i+=1; break;;
+ -*) errusage "unrecognized option: %s" "${!i}"; error=true;;
+ *) files+=("${!i}");;
+ esac
+ done
+ files+=("${@:$i}")
+ if [[ ${#files[@]} -lt 1 ]]; then
+ errusage "no input file given"
+ error=true
+ fi
+ if "$error"; then
+ return 1
+ fi
+ if "$use_defaults"; then
+ simple+=("${default_simple[@]}")
+ prose+=("${default_prose[@]}")
+ fi
+
+ # Main code
+ {
+ xgettext-sh "${simple[@]}" -- "${files[@]}"
+ xgettext-sh "${prose[@]}" -- "${files[@]}" | # These are the raw strings given to prose functions
+ tr '\n' '\r' | sed 's/"\r\s*"//g' | tr '\r' '\n' | # This removes the awkward word-wrapping done by xgettext
+ sed -r -e 's/(\\n|\\t|\t)/ /g' -e 's/ +/ /g' | # This collapses whitespace, HTML-style
+ sed '/^\#, sh-format/d'
+ } | msguniq -Fi
+}
+
+main "$@"