aedocw / epub2tts

Turn an epub or text file into an audiobook
Apache License 2.0
426 stars 43 forks source link

Part cleanup #246

Open Lastofthefirst opened 1 month ago

Lastofthefirst commented 1 month ago

When we run --export txt on a .epub the parts are automatically numbered, this is fantastic and very helpful. I usually delete the first 3 or something parts because they include the table of contents, the copyright page etc. This results in Part 4 being the first part.

I have a script that I use to cleanup the parts which is obviously very simple. It identifies all the lines that start with '# Part' and replaces them we a recount starting with 1. (Though the developer in me toyed with 0 index :laughing: ).

Maybe it would be useful to include a script like this for a common usecase.

#!/bin/bash

# Check if a file path is provided as an argument
if [ -z "$1" ]; then
    echo "Usage: $0 <file_path>"
    exit 1
fi

# Assign the first command-line argument to file_path
file_path="$1"

# Check if the file exists
if [ ! -f "$file_path" ]; then
    echo "File not found: $file_path"
    exit 1
fi

# Initialize chapter counter
chapter_counter=1

# Read the file and process each line
while IFS= read -r line; do
    # Check if the line starts with '# Part'
    if [[ "$line" =~ ^[[:space:]]*#\ Part ]]; then
        # Replace the line with "## Chapter X"
        echo "# Part $chapter_counter"
        chapter_counter=$((chapter_counter + 1))
    else
        # Output the original line
        echo "$line"
    fi
done < "$file_path" > "${file_path}.tmp"

# Replace the original file with the modified file
mv "${file_path}.tmp" "$file_path"
Lastofthefirst commented 1 month ago

Also, thank you for this great project, its very helpful.

aedocw commented 1 month ago

I'd be happy to add this to the utils directory if you want to submit a PR with that, thanks for sharing!