Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part cleanup #246

Open
Lastofthefirst opened this issue Jun 1, 2024 · 2 comments
Open

Part cleanup #246

Lastofthefirst opened this issue Jun 1, 2024 · 2 comments

Comments

@Lastofthefirst
Copy link

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 😆 ).

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
Copy link
Author

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

@aedocw
Copy link
Owner

aedocw commented Jun 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants