aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xsetenv.sh81
1 files changed, 50 insertions, 31 deletions
diff --git a/setenv.sh b/setenv.sh
index b0ba11d..7be94e8 100755
--- a/setenv.sh
+++ b/setenv.sh
@@ -25,7 +25,7 @@
# I consider a PGN file to be "pretty" if it
#
-# 1. has Windows-style newlines (\r\n),
+# 1. has Unix-style newlines (\n),
# 2. places the main line moves on separates lines,
# 3. doesn't have [%clk] tags after the moves, as those are redundant for
# storage and analysis (at least that's my opinion currently).
@@ -42,71 +42,90 @@
# by passing the paths to the PGN files you want to "prettify".
# For example,
#
-# > normalize_pgns Kasparov_vs_Karpov.pgn MyCrushingWin.pgn
+# > pgn_normalize Kasparov_vs_Karpov.pgn MyCrushingWin.pgn
#
# You can also compress a bunch of PGNs into a single PGN file:
#
-# > join_pgns Kasparov_vs_Karpov.pgn MyCrushingWin.pgn > MemorableGames.pgn
+# > pgn_join Kasparov_vs_Karpov.pgn MyCrushingWin.pgn > MemorableGames.pgn
#
# Or you might want to add a game from a PGN file to a database of games:
#
-# > append_pgn MemorableGames.pgn MyCrushingDefeat.pgn
+# > pgn_append MemorableGames.pgn MyCrushingDefeat.pgn
+
+alias pgn_dos2eol='sed --binary --in-place -- '"'"'s/\(\r\?\)$//'"'"
+alias pgn_eol2dos='sed --binary --in-place -- '"'"'s/\r\?$/\r/'"'"
+
+alias pgn_trim='sed --binary --in-place -- '"'"'s/[[:blank:]]*\(\r\?\)$/\1/'"'"
+
+alias pgn_trimeol='sed --binary --in-place -e :a -e '"'"'/^\n*$/{$d;N;ba}'"'"' --'
+alias pgn_trimdoseol='sed --binary --in-place -e :a -e '"'"'/^\(\r\n\)*\r$/{$d;N;ba}'"'"' --'
+
+alias pgn_eol='sed --binary --in-place -- '"'"'$a\'"'"
+alias pgn_doseol='sed --binary --in-place -- '"'"'$s/\r\?$/\r/;a\'"'"
# "Lints" PGN files.
# Each PGN
#
-# * has its Unix-style newlines (\n) replaced by Windows-style newlines (\r\n),
+# * has Windows-style newlines (\r\n) replaced by Unix-style newlines (\n),
# * gets whitespace characters trimmed from the end of each line,
# * gets trailing newlines trimmed from the end of the file,
-# * gets a single newline (\r\n) appended at the end of the file.
-lint_pgn() {
-
- sed --binary --in-place -- 's/\r\?$/\r/' "$@" \
- && sed --binary --in-place -- 's/[[:blank:]]*\(\r\?\)$/\1/' "$@" \
- && sed --binary --in-place -e :a -e '/^\(\r\n\)*\r$/{$d;N;ba}' -- "$@" \
- && sed --binary --in-place -- '$s/\r\?$/\r/;a\' "$@"
+# * is ensured to have a newline at the end.
+pgn_lint() {
+ pgn_dos2eol "$@" \
+ && pgn_trim "$@" \
+ && pgn_trimeol "$@" \
+ && pgn_eol "$@"
}
# Strips [%clk] tags from PGN files.
-alias strip_pgn_clk='sed --binary --in-place -- '"'"'s/ {\[%clk [[:digit:]]\+:[[:digit:]]\+\(:[[:digit:]]\+\)*\]}//g'"'"
+alias pgn_strip_clk='sed --binary --in-place -- '"'"'s/ {\[%clk [[:digit:]]\+:[[:digit:]]\+\(:[[:digit:]]\+\)*\]}//g'"'"
# Places main line moves on separate lines.
-alias slice_pgn_moves='sed --binary --in-place -- '"'"'s/ \([[:digit:]]\+\.\)/\r\n\1/g'"'"
+alias pgn_slice_moves='sed --binary --in-place -- '"'"'s/ \([[:digit:]]\+\.\)/\n\1/g'"'"
# "Prettifies" PGN files by (see above)
#
# * "linting" them,
# * stripping [%clk] tags,
# * placing main line moves on separate lines.
-normalize_pgn() {
- lint_pgn "$@" \
- && strip_pgn_clk "$@" \
- && slice_pgn_moves "$@"
+pgn_normalize() {
+ pgn_lint "$@" \
+ && pgn_strip_clk "$@" \
+ && pgn_slice_moves "$@"
}
-append_pgn() {
- if [ "$#" -ne 2 ]; then
- echo "usage: ${FUNCNAME[0]} DEST_PGN SRC_PGN" >&2
+pgn_append() (
+ set -o errexit -o nounset -o pipefail
+
+ if [ "$#" -lt 2 ]; then
+ echo "usage: ${FUNCNAME[0]} DEST_PGN SRC_PGN..." >&2
return 1
fi
- printf -- '\r\n' >> "$1" \
- && cat -- "$2" >> "$1"
-}
+ local dest="$1"
+ shift
+
+ local src
+ for src; do
+ echo >> "$dest"
+ cat -- "$src" >> "$dest"
+ done
+)
+
+pgn_join() (
+ set -o errexit -o nounset -o pipefail
-join_pgns() (
if [ "$#" -eq 0 ]; then
echo "usage: ${FUNCNAME[0]} PGN_FILE..."
return 0
fi
- set -o errexit
-
cat -- "$1"
+ shift
- local i
- for i in "${@:2}"; do
- printf -- '\r\n'
- cat "$i"
+ local pgn
+ for pgn; do
+ echo
+ cat -- "$pgn"
done
)