Last active
September 29, 2015 14:17
-
-
Save Kedrigern/1614022 to your computer and use it in GitHub Desktop.
BASH: Find and replace index entry in LaTeX files with many options.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#============================================================================== | |
# | |
# FILE: frp.sh | |
# | |
# USAGE: | |
# ./frp.sh --list list.txt -f text.tex | |
# ./frp.sh --help | |
# | |
# DESCRIPTION: Finds the patterns from list in LaTeX source code and replaces them by index entry. | |
# | |
# AUTHOR: Ondřej Profant, ondrej.profant <> gmail.com | |
# | |
# LICENCE: GPL | |
# | |
# VERSION: 0.3 | |
# | |
# HOMEPAGE: https://gist.github.com/Kedrigern | |
# | |
# CREATED: 10. 08. 2013 | |
# | |
# TODO: | |
# - recursivity | |
# | |
#============================================================================== | |
#GLOBAL VARS | |
VERSION="0.3"; | |
DEBUG=""; # non zero string for true on debug | |
TEST=""; | |
REC=""; | |
#=== FUNCTION =============================================================== | |
# NAME: help | |
# DESCRIPTION: Show help | |
#============================================================================== | |
function showHelp { | |
echo --------- | |
echo GENERALY: | |
echo --------- | |
echo "Find the patterns in LaTeX source code and replaces them by index entry." | |
echo "Index entry is \index{Pattern@Full Pattern}, for example: \index{Smith@Smith, John jr.}" | |
echo | |
echo -------- | |
echo WARNING: | |
echo -------- | |
echo "Make backup before use the $0! It small (maybe not smart) utility for my personal use. It is not properly testet." | |
echo | |
echo ----------- | |
echo PARAMETERS: | |
echo ----------- | |
echo -e "-f <file>.tex\t\tapply to the <file>.tex" | |
echo -e "-d <dir>/\t\tapply to all *.tex files in dir" | |
echo -e "-r \t\t\tuse with param -d, apply recursievly to subdirectories" | |
echo -e "-l \t\t\tfile with list of patterns" | |
echo -e "-t \t\t\tonly test (no change)" | |
echo -e "-h \t\t\tshow this help and ends." | |
echo -e "-v \t\t\tshow version of script and ends." | |
echo | |
echo ----------------- | |
echo LIST of PATTERNS: | |
echo ----------------- | |
echo "Format of list is tsv:" | |
echo "Smith \t smith \t Smith, John jr." | |
echo "Brown \t brown \t Brown, Martin" | |
echo "# comment line" | |
echo "Wolf \t wolf \t Wolf, Richard" | |
echo "Separator is sequence of white spaces. First column is index term (and searched pattern). Second column is alternative term for search. Third is full definition. First and second column can be same. All columns are required. Line with same term can be repeated with another second column. Comment line start with '# ' (space is required)." | |
echo | |
echo ------- | |
echo RESULT: | |
echo ------- | |
echo "List is from previous example." | |
echo "Text is:" | |
echo "Smith's car is fast." | |
echo "Result is:" | |
echo "Smith's\index{Smith@Smith, John jr.} car is fast." | |
echo | |
echo ------ | |
echo ABOUT: | |
echo ------ | |
echo "Author: Ondrej Profant, 2012-2013" | |
echo "Mail: ondrej.profant <> gmail.com" | |
echo "Github: https://github.com/Kedrigern" | |
echo | |
} | |
#=== FUNCTION =============================================================== | |
# NAME: OneFile | |
# USAGE: OneFile file Pattern pattern fullindexentry | |
# DESCRIPTION: Proccess one file with one line entry from list | |
#============================================================================== | |
function OneFile { | |
if [ -f $1 ]; then | |
echo -e "Hledám: $2,\t resp. $3,\t resp. $4 \t\t\tv souboru $1"; | |
if [ ! $TEST ]; then | |
sed "/$2[a-zA-Z']*"'\\'"index/b; | |
/$3[a-zA-Z']*"'\\'"index/b; | |
s/\($2[a-zA-Z']*\)/\1"'\\'"index{$2@$4}/g | |
t end | |
s/\($3[a-zA-Z']*\)/\1"'\\'"index{$2@$4}/g | |
:end" $1 > ${1}.tmp | |
rm ${1}; | |
mv ${1}.tmp ${1}; | |
fi; | |
fi; | |
} | |
#=== FUNCTION =============================================================== | |
# NAME: OneDirectory | |
# DESCRIPTION: Proccess one directory (all file with .tex sufix) | |
#============================================================================== | |
function OneDirectory { | |
cd "$1" ; | |
Pattern=$2 | |
pattern=$3 | |
full=$4 | |
for x in *.tex ; do | |
OneFile $x "$Pattern" "$pattern" "$full" | |
done; | |
if [ $REC ]; then | |
# TODO | |
echo "Recursion not implemented yet."; | |
fi; | |
if [ $1 != "." ]; then | |
cd .. ; | |
fi; | |
} | |
#=== FUNCTION =============================================================== | |
# NAME: MainLoop | |
# DESCRIPTION: Main loop which read list and call proper function | |
#============================================================================== | |
function MainLoop { | |
while read Pattern pattern fullpatt; do | |
if [ -z $Pattern ]; then | |
continue; | |
fi; | |
if [ "$Pattern" = "#" ]; then | |
continue; | |
fi; | |
if [ $file ]; then | |
OneFile "$file" "$Pattern" "$pattern" "$fullpatt" ; | |
elif [ $dir ]; then | |
OneDirectory "$dir" "$Pattern" "$pattern" "$fullpatt" ; | |
else | |
echo "No input file / files." >&2 ; | |
exit 1; | |
fi; | |
done < "$1" | |
} | |
#============================================================================== | |
# PARAMETER PARSING | |
#============================================================================== | |
while [ $# -gt 0 ]; do | |
case $1 in | |
"-h" | "--help") | |
showHelp; exit 0; ;; | |
"-v" | "--version") | |
echo -e "Version: ${VERSION} \nAuthor: Ondrej Profant, 2012"; exit 0; ;; | |
"-f" | "--file") | |
file="$2"; | |
shift 2; | |
;; | |
"-d" | "--dir") | |
dir="$2" | |
shift 2; | |
;; | |
"-r") | |
REC=true | |
shift 1; | |
;; | |
"-l" | "--list") | |
list="$2"; | |
shift 2; | |
;; | |
"-t" | "--test") | |
echo "TEST MODE no change" | |
TEST="yes"; | |
shift 1; | |
;; | |
*) | |
echo "Unknow par: $1" >&2; | |
shift 1; | |
esac; | |
done; | |
if [ -z "$list" ]; then | |
echo "-l is required" >&2; | |
exit 1; | |
fi; | |
if [ ! -f "$list" ]; then | |
echo "$list is not in proper format (character file)." >&2; | |
exit 1; | |
fi; | |
MainLoop "${list}"; | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment