summaryrefslogtreecommitdiff
path: root/macros/eplain/doc/util
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/eplain/doc/util
Initial commit
Diffstat (limited to 'macros/eplain/doc/util')
-rwxr-xr-xmacros/eplain/doc/util/idxuniq39
-rwxr-xr-xmacros/eplain/doc/util/trimsee106
2 files changed, 145 insertions, 0 deletions
diff --git a/macros/eplain/doc/util/idxuniq b/macros/eplain/doc/util/idxuniq
new file mode 100755
index 0000000000..599d9b593c
--- /dev/null
+++ b/macros/eplain/doc/util/idxuniq
@@ -0,0 +1,39 @@
+#!/usr/bin/awk -f
+#
+# (This file is public domain.)
+#
+# This file is part of the Eplain macro package.
+#
+# This script filters input lines (which are expected to be a list of
+# `\indexentry' lines for MakeIndex) eliminating matching lines. The comparison
+# is done without regard to hyperlink label names (in the form `IDX*', where `*'
+# stands for a non-negative integer).
+#
+# This has to be done to avoid terms which differ only in hyperlink label names
+# embedded into them, because MakeIndex will treat these otherwise identical
+# terms as distinct, listing more than once a page number for equivalent terms
+# on the same page.
+#
+# `stripped' array is indexed by `\indexentry' lines stripped off the hyperlink
+# label name. For each term, we use its stripped version as a key into the
+# `stripped' array to increment its element. This records the fact that we have
+# seen the term with such key. Also, we add the (full) `\indexentry' line to
+# the `terms' array, but only if we have not yet seen a term with such key.
+#
+# The idea was borrowed from Edition 3 of `GAWK: Effective AWK Programming: A
+# User's Guide for GNU Awk', which contains the following credit:
+#
+# histsort.awk --- compact a shell history file
+# Thanks to Byron Rakitzis for the general idea
+
+{
+ temp = $0
+ sub (/{IDX[[:digit:]]+}/, "", temp)
+ if (stripped[temp]++ == 0)
+ terms[++count] = $0
+}
+
+END {
+ for (i = 1; i <= count; i++)
+ print terms[i]
+}
diff --git a/macros/eplain/doc/util/trimsee b/macros/eplain/doc/util/trimsee
new file mode 100755
index 0000000000..00459956f4
--- /dev/null
+++ b/macros/eplain/doc/util/trimsee
@@ -0,0 +1,106 @@
+#!/bin/sh
+# (This file is public domain.)
+# This file is part of the Eplain macro package.
+
+help ()
+{
+ cat <<EOF
+Usage: trimsee [-i IS] [-o OS] [-s SEE]
+ trimsee {-h|--help|-v|--version}
+Remove commas (or other page list separators) in front
+of see / see also commands in the output of MakeIndex.
+Input is read from stdin, output is directed to stdout.
+
+-i IS use IS as a regular expression matching separator
+ in front of see / see also commands in the input
+ (default: '$DEFAULT_IS')
+-o OS use OS as a separator to replace IS in front of
+ see / see also commands (default: '$DEFAULT_OS')
+-s SEE use SEE as a regular expression matching see /
+ see also commands (default: '$DEFAULT_SEE')
+-h, --help show this help message
+-v, --version show version
+EOF
+}
+
+check_missing_arg ()
+{
+ if test "$1" -lt 2; then
+ echo "Missing argument to option '$2'" 1>&2
+ exit 1
+ fi
+}
+
+VERSION='0.1'
+
+DEFAULT_SEE='\\indexsee'
+DEFAULT_IS=', \+'
+DEFAULT_OS=' '
+
+SEE=$DEFAULT_SEE
+IS=$DEFAULT_IS
+OS=$DEFAULT_OS
+
+while [ $# != 0 ]
+do
+ case "$1" in
+ -i)
+ check_missing_arg $# "$1"
+ IS=$2
+ shift
+ ;;
+ -o)
+ check_missing_arg $# "$1"
+ OS=$2
+ shift
+ ;;
+ -s)
+ check_missing_arg $# "$1"
+ SEE=$2
+ shift
+ ;;
+ -h | --help)
+ help
+ exit 0
+ ;;
+ -v | --version)
+ echo "trimsee version $VERSION"
+ exit 0
+ ;;
+ *)
+ echo "Unknown option '$1'" 1>&2
+ exit 1
+ ;;
+ esac
+ shift
+done
+
+
+# The idea is to implement line output delayed by one line. When we
+# read next line, we check if it starts with a see/see also entry, and
+# if it does, we remove the comma at the end of the previous line
+# before printing it. We keep previous lines in the hold buffer.
+
+sed -n '
+
+# Remove all commas in front of see/see also inside each line.
+s/'"$IS$SEE/$OS$SEE"'/g
+
+# If this line does not start with see/see also, we will print the
+# previous line intact. NOTE: There are two characters inside the
+# brackets: a space and a tab.
+/^[ ]*'"$SEE"'/! { x; b PRINT; }
+
+# This line starts with see/see also, so remove comma at the end of
+# the previous line before we print it.
+x
+s/'"$IS"'$/'"$OS"'/
+
+# Print the previous line.
+:PRINT
+1! p
+
+# At the end of the input, print the last line.
+$ { x; p; }
+
+'