fsfe-website/tools/encoding-convert.sh

30 lines
1.1 KiB
Bash
Raw Normal View History

2019-05-22 10:59:00 +00:00
#!/bin/bash
2019-05-22 13:32:25 +00:00
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Select files
2019-05-22 10:59:00 +00:00
#FILES=$(git grep -i -l "<\?xml.*encoding=\"iso" .)
FILES=$(find . -type f)
while read -r file; do
enc=$(file --mime-encoding ${file} | cut -d" " -f2)
2019-05-22 13:32:25 +00:00
# Only run if not correct encoding, and if XML, XHTML, or XSL file
2019-05-22 10:59:00 +00:00
if [ "${enc}" != "utf-8" ] && [ "${enc}" != "us-ascii" ] && [ $(echo $file | grep -qoE "(\.xml$|\.xhtml$|\.xsl$)"; echo $?) -eq 0 ]; then
2019-05-22 13:32:25 +00:00
# Only run if file is not outdated
trstatus=$(tools/check-translation-status.sh -f "$file" -q; echo $?)
if [ $trstatus = 0 ]; then
# Convert to UTF-8
iconv -f ${enc} -t UTF-8 ${file} -o ${file}.utf8
# Replace XML encoding in first line
sed -Ei "s@^<\?xml.*($enc|iso-8859-1|iso-8859-2|iso8859-1).*@<?xml version=\"1.0\" encoding=\"UTF-8\" ?>@I" ${file}.utf8
# Move edited file to original location
mv ${file}.utf8 ${file}
# Status report
enc_new=$(file --mime-encoding ${file} | cut -d" " -f2)
echo "Converted $file from $enc to $enc_new"
elif [ $trstatus = 1 ]; then
echo "WARNING: $file is outdated"
fi
2019-05-22 10:59:00 +00:00
fi
done <<< "${FILES}"