Skip to content

Instantly share code, notes, and snippets.

@ziz
Created November 25, 2022 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziz/8ffdc320709ed13028e0fee3b1009724 to your computer and use it in GitHub Desktop.
Save ziz/8ffdc320709ed13028e0fee3b1009724 to your computer and use it in GitHub Desktop.
AO3 Chapter Splitter by Lara
/*
AO3 Chapter Splitter
Written by Lara in November 2020 for Adobe InDesign CC 2019
Version 1.0
Splits AO3 stories by chapter and bookmarks the beginning of each chapter.
Based in part on StorySplitter by FourAces, Naomi Kennedy and Chris Jennings
From https://docs.google.com/document/d/1uLVfvlzMzUyAyslvlPEZWc4Kzg3DgA39HB6y1af6zhE/edit
Tested successfully on InDesign 2023
# Automated AO3 Chapter Splitting in InDesign
1. Download AO3 fic as HTML and name it “input.html”
2. Follow steps 1-3 on the [Pandoc website](https://pandoc.org/getting-started.html) in order to install Pandoc and navigate to the directory you saved your HTML file.
- If you’re familiar with Homebrew it’s just `brew install pandoc`
3. Run the command `pandoc --standalone -t ICML -o output.icml input.html` in your Terminal (Mac) or Command Prompt (Windows)
4. Open InDesign and create a new 2-3 page file. Create a text box with the dimensions and margins you want for your body text and thread it to the next page. (You will need to have text frames on at least two pages for the script to work.).
5. Drag output.icml into the InDesign file and into the text frame you just created.
6. Go to the link pane and unlink the output.icml file. (The script will not work if the file is still linked.)
7. Edit the default paragraph styles to your preferences. Window → Styles → Paragraph Styles.
- I change “Paragraph” (body text) to my preferred font, size and indent and I edit “Blockquote > Paragraph” (chapter notes and summary) to have a yellow highlight so I don’t miss it.
8. Install my script. [Here](https://indesignsecrets.com/how-to-install-scripts-in-indesign.php) are instructions on how to add scripts to InDesign.
- I give a few options for how many pages are added between chapters. If you open and edit the document the “chapter_spacing” variable is at the very top, right underneath the credits.
9. Run the script. Window → Utilities → Scripts
10. Wait (up to 5 minutes or so for large files)
11. Open the bookmarks panel. You can use bookmarks to navigate between the chapters. Window → Interactive → Bookmarks
12. Manually finish your typesetting.
Let me know if you have any problems, or suggestions!
You can reach me at larahechu@gmail.com or hechulara#8840 on Discord
A warning: If you’re using this script on a document you’ve already put time into formatting, make sure to save a copy before starting. It is very hard to undo scripts.
The script (this script!) can be found here: https://cdn.discordapp.com/attachments/726108789161590916/778849084379234314/AO3-split-chapters.jsx
------------------------------------------------------------------------------------------------------------------
StorySplitter
------------------------------------------------------------------------------------------------------------------
An InDesign CS/CS2/CS3 JavaScript by FourAces
© The Final Touch 2006
Version 3.0.0
Splits the selected Story to separate Text Frames, while maintaining their contents.
------------------------------------------------------------------------------------------------------------------
Update by Naomi Kennedy
With modification by Chris Jennings
*/
//USER INPUTS//
chapter_spacing = 2
//0 = no blank pages, 1 = all chapters start on the right (there will only be blank pages if the prior chapter ended on the right), 2 = all chapters start on the right with a blank facing page
//----------------------------------------------------------------------------
if(app.documents.length != 0){
spaceandbookmarkandsplit();
alert("Done!")
}else{
alert("No Active Document Found.\nPlease open an InDesign document and select a Story to split.");
}
//----------------------------------------------------------------------------
function spaceandbookmarkandsplit(){
if(app.selection[0] == null){
alert("Please select a text frame.");
}
//define some doc wide variables
var the_story = app.selection[0].parentStory;
var doc = app.activeDocument;
var placeFrame = app.selection[0].endTextFrame
var reflow_pref = app.textPreferences.smartTextReflow;
//Autoflow the entire story
while(the_story.overflows == true){
frameProperties = placeFrame.previousTextFrame.geometricBounds
newPage = doc.pages.add();
newFrame = newPage.textFrames.add();
newFrame.geometricBounds = frameProperties;
placeFrame.nextTextFrame = newFrame;
placeFrame = newFrame
}
// Misc. housekeeping stuff
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
app.findGrepPreferences = NothingEnum.nothing;
app.textPreferences.smartTextReflow = false; //otherwise InDesign will NOT like you
// Set it to only find the chapter name paragraph style
app.findGrepPreferences.appliedParagraphStyle = "Header2"; //this is what the AO3 chapter titles are changed to by Pandoc
app.findGrepPreferences.findWhat = "^.";
// Now run the search
var found_paragraphs = the_story.findGrep();
// Go through each result and add spaces
Counter = 1; //ignore the metadata section
do{
//Add a frame break to send the next chapter to the next frame.
var myBfBreakFrame = found_paragraphs[Counter].parentTextFrames[0]
var myBreakStory = myBfBreakFrame.parentStory;
var insertionpt_wrong = found_paragraphs[Counter].insertionPoints.firstItem();
page_number = myBfBreakFrame.parentPage.documentOffset + 1;
var chapterheading = myBreakStory.insertionPoints[insertionpt_wrong.index + (2*Counter - 2)]; //Don't ask me why this works, all I know is it fixes a weird error I was getting. I think the frame break might mess with the index?
chapterheading.contents = SpecialCharacters.FRAME_BREAK;
//Determine number of inserted paged needed depending on Chapter Spacing input
if(chapter_spacing == 0){
n = 0
}
else if(chapter_spacing == 1){
if(page_number % 2 == 0){
n = 1
}
else{
n = 0
}
}
else if(chapter_spacing == 2){
if(page_number % 2 == 0){
n = 2
}
else{
n = 1
}
}
//Insert said pages
repeat = 0
while(repeat<n){
nextpage = myBfBreakFrame.parentPage;
if(nextpage == null){
continue
}
doc.pages.add(LocationOptions.AFTER, nextpage);
repeat++
}
//Add each chapter to bookmarks for ease of navigation
var bookmarkheading = myBreakStory.insertionPoints[insertionpt_wrong.index + (2*Counter + 1)]; //Otherwise it bookmarks the end of the previous chapter.
app.documents.item(0).bookmarks.add(app.documents.item(0).hyperlinkTextDestinations.add(bookmarkheading));
app.documents.item(0).bookmarks.lastItem().name = "Chapter " + (Counter + 1);
//Thread overflowing text
while(the_story.overflows == true){
frameProperties = placeFrame.previousTextFrame.geometricBounds
newPage = doc.pages.add();
newFrame = newPage.textFrames.add();
newFrame.geometricBounds = frameProperties;
placeFrame.nextTextFrame = newFrame;
placeFrame = newFrame
}
Counter++
}while(Counter < found_paragraphs.length);
Counter2 = 1
//Split text frame at each bookmark
do{
var bookmk = doc.bookmarks.lastItem()
var location = bookmk.destination.destinationText
var myAfBreakFrame = location.parentTextFrames[0]
var myBfBreakFrame = myAfBreakFrame.previousTextFrame;
var myBreakStory = myBfBreakFrame.parentStory;
//Split the text frames
location.parentTextFrames[0].previousTextFrame = null;
//Move the overset text to the next frame
if(myBfBreakFrame.overflows == true){
var myOversetText = myBreakStory.texts.itemByRange(myBfBreakFrame.insertionPoints[-1],myBreakStory.insertionPoints[-1]);
app.select(myOversetText);
app.cut();
app.select(myAfBreakFrame.insertionPoints[0]);
app.paste();
app.select(myAfBreakFrame.insertionPoints[0]);
}
Counter2++
}while(Counter2 < Counter);
//Re-bookmark each chapter for ease of use
chapter = 1
do{
var bookmarkheading = doc.stories[Counter2-chapter].insertionPoints[0]
doc.bookmarks.add(doc.hyperlinkTextDestinations.add(bookmarkheading));
doc.bookmarks.lastItem().name = "Chapter " + (chapter);
chapter++
}while(chapter < doc.stories.length)
//Return reflow to the previous setting
app.textPreferences.smartTextReflow = reflow_pref;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment