-
Notifications
You must be signed in to change notification settings - Fork 2
Restructure Chapters
The reorder_and_rename_chapters.py script provides a safe and automated way to reorder and rename chapter files in
the manuscript and normalize their H1 headers and anchor IDs.
It is especially useful when chapters have been restructured (e.g. Spanish, English, German editions of The Currency of the Mind) and you need to align file names and anchors consistently.
-
Two-phase renaming
Renames files safely via a temporary.tmpmoveextension to avoid filename collisions. -
H1 header normalization
Updates the top header line, e.g.# ✦ Capítulo 7: Creatividad – La moneda renovable {#-chapter-7}ensuring that the number and anchor ID always match the filename.
-
Anchor ID synchronization
Ensures{#-chapter-N}is consistent with the file’s chapter number. -
Language support
Recognizes and normalizes headers for Spanish (Capítulo), German (Kapitel), and English (Chapter).
You can force a language via--lang, or let the script auto-detect. -
Mapping flexibility
Chapter mappings can be:-
Provided inline via
--map src:tgt -
Loaded from a file (
.json,.csv,.yaml)
-
-
Dry run mode
Preview all operations without changing any files. -
Poetry integration
Configured as a Poetry script for simple CLI usage.
Make sure your project is Poetry-based and manuscripta is installed.
In pyproject.toml add:
the manuscripta library
reorder-chapters = "scripts.reorder_and_rename_chapters:main"Reinstall or update your Poetry environment:
poetry installNow the command poetry run reorder-chapters is available.
poetry run reorder-chapters --dry-run \
--base-dir manuscript/chapters \
--map-file mapping.json --lang espoetry run reorder-chapters --base-dir manuscript/chapters --map-file mapping.json --lang espoetry run reorder-chapters --base-dir manuscript/chapters \
--map 11-chapter.md:07-chapter.md \
--map 14-chapter.md:08-chapter.md \
--map 16-chapter.md:09-chapter.md \
--lang es{
"11-chapter.md": "07-chapter.md",
"14-chapter.md": "08-chapter.md",
"16-chapter.md": "09-chapter.md"
}src,tgt
11-chapter.md,07-chapter.md
14-chapter.md,08-chapter.md
16-chapter.md,09-chapter.md- src: "11-chapter.md"
tgt: "07-chapter.md"
- src: "14-chapter.md"
tgt: "08-chapter.md"
- src: "16-chapter.md"
tgt: "09-chapter.md"YAML requires
pyyaml. Install with:
poetry add pyyaml
| Option | Description |
|---|---|
--base-dir PATH |
Base directory of chapter files (default: manuscript/chapters) |
--map-file FILE |
Mapping file (.json, .csv, .yaml) |
--map src:tgt |
Inline mapping, can be repeated |
--lang {es,de,en} |
Force language for H1 normalization (auto-detect if omitted) |
--dry-run |
Preview changes without modifying files |
-
Two-phase renaming prevents accidental overwriting.
-
Warns if source files are missing.
-
Errors out if mapping contains duplicate targets.
-
Logs every action (
[OK],[WARN],[DRY]).
-
Always run with
--dry-runfirst to verify. -
Commit your repo before running, so you can easily revert if needed.
-
Keep your mapping files (
mapping.json) in version control for traceability. -
Use separate branches per language edition (
develop-es,develop-en, …).
-
Prepare a
mapping.jsonwith the chapter renames. -
Run a dry run:
poetry run reorder-chapters --dry-run --map-file mapping.json --lang es
-
If the preview looks good, apply:
poetry run reorder-chapters --map-file mapping.json --lang es
-
Check git diff and commit the changes:
git add manuscript/chapters git commit -m "chore(es): reorder and rename chapters; normalize headers and anchors"
- Home
- Project Initialization
- Generate Project Structure
- How to Write a Book
- Developer Workflow & Makefile
- Chapter File Generator
- Generate Images
- Convert Markdown Images
- Bulk Change File Extensions
- Restructure Chapters
- Remove Bold from Markdown Headers
- Converting Markdown Bullets to Typographic Bullets
- Translate Markdown with DeepL
- Translate with LM Studio
- Translation CLI Commands
- Shortcuts for Translation
- Automatic Book Export
- Shortcuts for Export
- Export HTML Chapters (Comics)
- Export to EPUB 2
- Pandoc Batch Processor
- Export HTML Books to PDF (KDP Ready)
**Libraries: ** manuscripta | manuscript-tools