Storage for XML files used to represent Dante's *Divine Comedy*.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

33 lines
1.2 KiB

#!/bin/bash
# Add terzina tags to each canto.xml file in a directory
# Also add canto number attribute.
echo -e "\nAdding terzina tags and numbers."
for file in *;
do
if [[ "$file" != 'terzina-tags.sh' ]]; then # exclude this script
if [[ "$file" != sed* ]]; then # exclude sed file made later
echo -e "Modifying $file"
# Every third line (mod 3) add a closing tag, newline, and opening tag.
awk -i inplace '{print}; NR % 3 == 0 {print "\t\t</terzina>\n\t\t<terzina num=~NUM~>"}' "$file"
# Add an opening tag at the beginning and a closing tag at the end because the above awk doesn't.
sed -i '1s/^/\t\t<terzina num="1">\n/' "$file"
echo -e "\t\t</terzina>" >> "$file"
# Add the canto number attribute (at most 160/3 ~ 54 terzine)
for i in $(seq 1 54);
do
# Substitute in the actual number with sed. I couldn't figure out
# how to include the $i variable with the surrounding attribute
# quotes in the awk statement, so I just used sed here separately.
# We start at i=2 because the awk command above
# doesn't add a terzina tag to the first terzina.
TERZ_NUM=$((i + 1))
# Replace only first occurence of ~NUM~.
sed -i -e "0,/~NUM~/ s/~NUM~/\"$TERZ_NUM\"/" "$file"
done
fi
fi
done