Created
April 24, 2019 19:35
-
-
Save co60ca/1dec52ff4122aeb7f5227d7c62513812 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Copyright Maeve Kennedy 2019 | |
# Under MIT License | |
set -eu | |
if ! [ "${1:-""}" ] ; then | |
echo "Usage: $0 filename..." >&2 | |
echo " converts filename... to <pdf title>.pdf" >&2 | |
exit 1 | |
fi | |
if ! which pdftotext > /dev/null ; | |
echo "pdftotext is required, please install or add to path" >&2 | |
exit 1 | |
fi | |
mkdir -p backups | |
for file in "$@"; do | |
# File exist Guard | |
if ! [ -f "$file" ] ; then echo "$file" not found >&2 ; continue ; fi | |
txt=`pdftotext -htmlmeta "${file}" /dev/stdout | head -n 3` | |
[[ "$txt" =~ \<title\>(.*)\</title\> ]] | |
newname="${BASH_REMATCH[1]:-""}" | |
# Guard | |
if ! [ "${newname}" ] ; then echo "$file" has no pdf title >&2 ; continue ; fi | |
newname="${newname}.pdf" | |
# Replace non alpha words, html encoding | |
if which recode > /dev/null & which sed > /dev/null ; then | |
newname=`echo "${newname}" | recode html` | |
newname=`echo "${newname}" | sed 's/<.*>//g'` | |
fi | |
# Replace / 's | |
newname="${newname//\//-}" | |
# Replace spaces with dashes | |
newname="${newname//[[:space:]]/-}" | |
echo "$file -> $newname" | |
cp "${file}" backups | |
mv "${file}" "$newname" | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment