Skip to content

Instantly share code, notes, and snippets.

@coolreader18
Last active July 26, 2019 18:24
Show Gist options
  • Select an option

  • Save coolreader18/00a2f311a2a9d9784e12b4523641d180 to your computer and use it in GitHub Desktop.

Select an option

Save coolreader18/00a2f311a2a9d9784e12b4523641d180 to your computer and use it in GitHub Desktop.
Convert a LibreOffice ODT file to a Hugo post.
#!/bin/bash
set -e
case $# in
0)
file=$(
zenity --file-selection \
--file-filter='*.odt' --file-filter='*.docx' --file-filter='*.*' \
--title "Select a document to publish:" \
2>/dev/null
)
read -p "What category is this post in? " category
;;
2)
file=$1
category=$2
;;
*)
echo >&2 "Expected exactly 0 or 2 arguments: the path to the odt file and the post category."
exit 1
;;
esac
file=$(realpath "$file")
basename=$(basename "$file")
basename=${basename%.*}
hugofile=$category/$basename.md
realhugofile=content/$hugofile
assets_dirname=assets_$basename
assets_dir=static/$assets_dirname
cd "$(dirname "$0")"
pub_date=
if [[ -f $realhugofile ]]; then
pub_date=$(awk '/^date:/{print $2}' "$realhugofile" | head -n1)
fi
rm -f "$realhugofile"
hugo new "$hugofile"
pandoc -t markdown --extract-media __Media "$file" >>"$realhugofile"
sed -i 's|__Media|/'"$assets_dirname"'|' "$realhugofile"
sed -i ':a;N;$!ba;s/{width=\".\+\"\nheight=\".\+\"}//g' "$realhugofile"
if [[ $pub_date ]]; then
sed -i 's/^date:.\+$/date: '"$pub_date"'/' "$realhugofile"
fi
if [[ -d __Media ]]; then
mkdir -p "$assets_dir"
cp -rlf __Media/* "$assets_dir"
rm -rf __Media
fi
if [[ $GIT_COMMIT ]]; then
git add --all
git commit -m "Updated post '$basename'"
fi
echo "Done! Your post has been generated at $realhugofile"
if [[ $WAIT ]]; then
echo Press any button
read -n1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment