-
-
Save nekketsuuu/8497462d228138bd8b4453c05ec9b18c to your computer and use it in GitHub Desktop.
Sotsuron word counter
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 | |
CHAP= | |
ALLFLAG=0 | |
while getopts ac: OPT | |
do | |
case $OPT in | |
a) ALLFLAG=1;; | |
c) CHAP=$OPTARG;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
THESIS="${1:-./thesis.tex}" | |
# download latexcount.pl | |
if [ ! -e ./latexcount.pl ]; then | |
echo "Downloading latexcount.pl" | |
curl -o ./latexcount.pl http://ftp.jaist.ac.jp/pub/CTAN/support/latexcount/latexcount.pl 2> /dev/null | |
fi | |
# apply patch | |
if ! echo "e7a62d514ef12a326109c685cad975a0 ./latexcount.pl" | md5sum -c /dev/stdin --status; then | |
echo "Applying patch to latexcount.pl" | |
patch -u <<'EOS' | |
--- latexcount.pl 2016-12-29 20:45:35.381827297 +0900 | |
+++ latexcount_fix.pl 2016-12-31 19:46:23.000000000 +0900 | |
@@ -57,8 +57,8 @@ | |
$line =~ s/(?<!\\)%.*?\n//g; | |
# Count curly braces | |
- while($line =~ /\{/g){$depth++} | |
- while($line =~ /\}/g){$depth--} | |
+ while($line =~ /(?<!\\)\{/g){$depth++} | |
+ while($line =~ /(?<!\\)\}/g){$depth--} | |
# Concatenate the new hunk of input to any | |
# left over from previous cycles. | |
EOS | |
fi | |
mainpart () { | |
cat - | awk -F '' ' | |
BEGIN { | |
flg = 0 | |
} | |
/\\bibliographystyle/ { | |
exit | |
} | |
flg == 1 { | |
print $0 | |
} | |
/\\mainmatter/ { | |
flg = 1 | |
} | |
' | |
} | |
getchapter () { | |
cat - | awk -v chap="$1" ' | |
BEGIN { | |
flg = 0 | |
pat = "\\chapter{" chap "}" | |
} | |
/\\bibliographystyle/ { | |
exit | |
} | |
/\\chapter{.+}/ && flg == 1{ | |
exit | |
} | |
flg == 1 { | |
print $0 | |
} | |
$0 ~ pat { | |
flg = 1 | |
} | |
' | |
} | |
chapters () { | |
cat - | awk -F '' ' | |
/\\chapter{.+}/ { | |
line = $0 | |
gsub(/\\/, "\\\\", line) | |
I1 = index(line, "{")+1 | |
print substr(line, I1, index(line, "}")-I1) | |
} | |
' | |
} | |
allchapters () { | |
TOTAL=0 | |
while read CH | |
do | |
CH2=$(echo $CH | sed -e 's/\\/\\\\\\\\/g' | sed -e 's/\$/\\\\$/g') | |
WORDS=$(cat ${THESIS} | getchapter "${CH2}" | perl latexcount.pl | awk ' | |
/^[0-9]+ total/ { | |
print substr($0, 0, index($0, " ")); | |
} | |
') | |
TOTAL=`expr ${TOTAL} + ${WORDS}` | |
echo -n "${CH}: " | |
echo $WORDS | |
done | |
echo | |
echo "TOTAL: ${TOTAL}" | |
} | |
if [ -n "$CHAP" ]; then | |
cat ${THESIS} | getchapter "$CHAP" | perl ./latexcount.pl | |
elif [ ${ALLFLAG} -eq 1 ]; then | |
cat ${THESIS} | mainpart | perl ./latexcount.pl | |
else | |
cat ${THESIS} | chapters | allchapters | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment