-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave.sh
More file actions
executable file
·59 lines (52 loc) · 1.99 KB
/
wave.sh
File metadata and controls
executable file
·59 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# wave.sh: Add ~ character before specific patterns in LaTeX files
# Copyright (C) 2017-2018 Libor Polčák
#
# 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 3 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/>.
function fix_space_before {
FIND=$1
FILE=$2
# See also https://stackoverflow.com/questions/12129870/how-can-i-remove-a-line-feed-newline-before-a-pattern-using-sed
sed -i ":r;\$!{N;br};s/\n *${FIND}/~${FIND}/g" "$FILE"
sed -i "s/ ${FIND}/~${FIND}/g" "$FILE"
}
function fix_space_after {
FIND=$1
REPLACEMENT=$2
FILE=$3
# See also https://stackoverflow.com/questions/12129870/how-can-i-remove-a-line-feed-newline-before-a-pattern-using-sed
sed -i ":r;\$!{N;br};s/\(${FIND}\)\n/\1${REPLACEMENT}/g" "$FILE"
sed -i "s/\(${FIND}\) \b/\1${REPLACEMENT}/g" "$FILE"
}
function fix_file() {
# Fix \cite and \ref
fix_space_before '\\cite' $1
fix_space_before '\\ref' $1
# Squeeze (number)\nsqueeze on the previous line
fix_space_after '([0-9]\+)' '~' $1
# Do not leave (a), (b), (c) ... or (1), (2), (3) on the end of the line
fix_space_after '([a-z0-9])' '~' $1
# Some of the https://english.stackexchange.com/questions/67089/english-line-breaking-rules
fix_space_after '\ba\b' '~' $1
fix_space_after '\ban\b' '~' $1
fix_space_after '\bthe\b' '~' $1
#fix_space_after '\bwhich\b' '\\nolinebreak[3] ' $1
#fix_space_after '\bthat\b' '\\nolinebreak[3] ' $1
#fix_space_after '\bwho\b' '\\nolinebreak[3] ' $1
}
while [ $# -gt 0 ];
do
fix_file $1
shift
done