Skip to content

Instantly share code, notes, and snippets.

@amineHY
Created February 4, 2024 18:40
Show Gist options
  • Save amineHY/86519dfc646ae343b66eac10f64f04d0 to your computer and use it in GitHub Desktop.
Save amineHY/86519dfc646ae343b66eac10f64f04d0 to your computer and use it in GitHub Desktop.
This Bash script converts an AsciiDoc file to plain text using Asciidoctor and Pandoc. It takes the AsciiDoc file as a command-line argument, generates an intermediate XML file, converts it to plain text, opens the text file for analysis, and then deletes the intermediate XML file. The script can be executed from any directory by providing the f…
#!/bin/bash
# This Bash script converts an AsciiDoc file to plain text using Asciidoctor and Pandoc. It takes the AsciiDoc file as a command-line argument, generates an intermediate XML file, converts it to plain text, opens the text file for analysis, and then deletes the intermediate XML file. The script can be executed from any directory by providing the full or relative path to the AsciiDoc file.
# usage: ./convert_asciidoc_to_txt.sh /path/to/your/input_file.adoc
# Install Asciidoctor and Pandoc if not already installed
if ! command -v asciidoctor &> /dev/null; then
gem install asciidoctor
fi
if ! command -v pandoc &> /dev/null; then
echo "Pandoc is not installed. Please install Pandoc and rerun the script."
exit 1
fi
# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <absolute_or_relative_path/input_file.adoc>"
exit 1
fi
# Get the absolute path of the AsciiDoc file
input_file="$(realpath "$1")"
# Check if the file exists
if [ ! -f "$input_file" ]; then
echo "File not found: $input_file"
exit 1
fi
# Change to the directory containing the script
cd "$(dirname "$0")" || exit 1
# Render AsciiDoc file to text
asciidoctor -b docbook -o output.xml "$input_file"
pandoc -f docbook -t plain output.xml -o output.txt
# Open the generated text file for analysis
open output.txt # For macOS
# Delete the intermediate XML file
rm output.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment