Skip to content

Instantly share code, notes, and snippets.

@amineHY
Last active February 4, 2024 18:41
Show Gist options
  • Save amineHY/949f704aaee125469b1dbefa3daa230b to your computer and use it in GitHub Desktop.
Save amineHY/949f704aaee125469b1dbefa3daa230b to your computer and use it in GitHub Desktop.
Convert an AsciiDoc file to HTML and then to DOCX.
#!/bin/bash
usage() {
echo "Usage: $0 [-h] FILENAME" >&2
echo "Convert an AsciiDoc file to DOCX" >&2
echo "" >&2
echo "positional arguments:" >&2
echo " FILENAME path to the AsciiDoc file" >&2
echo "" >&2
echo "optional arguments:" >&2
echo " -h, --help show this help message and exit" >&2
exit 1
}
# Parse command-line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
usage
shift
;;
*)
FILENAME="$1"
shift
;;
esac
done
# Check if Asciidoctor is installed
if ! command -v asciidoctor &> /dev/null
then
echo "Asciidoctor is not installed. Installing it now."
sudo apt-get install -y asciidoctor
fi
# Check if Pandoc is installed
if ! command -v pandoc &> /dev/null
then
echo "Pandoc is not installed. Installing it now."
sudo apt-get install -y pandoc
fi
# Check if a filename was provided as an argument
if [ -z "${FILENAME}" ]
then
echo "Error: Please provide a filename as an argument."
usage
fi
# Extract the directory, filename and extension from the argument
directory=$(dirname "$FILENAME")
filename=$(basename "$FILENAME")
extension="${filename##*.}"
filename="${filename%.*}"
# Change directory to the directory of the file
cd "$directory"
# Convert AsciiDoc file to HTML
asciidoctor -b html5 "$filename.$extension"
# Convert HTML file to DOCX
pandoc -s "$filename.html" -o "$filename.docx"
# Clean up intermediate files
rm "$filename.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment