Tuesday, 15 October 2013

Zim hacks part 2: split a single-file manuscript into chapters and scenes

In a followup to Zim hacks part 1: compile a manuscript, this time I'm going to split a single file into chapters and scenes (pages and sub-pages) within Zim. For me this is actually the more useful of the two tools, since I write in FocusWriter and only transfer completed drafts into Zim for ease of reading and note-making when I'm preparing to do some rewriting. Anyway...

The script: split_manuscript.sh

#split_manuscript.sh: splits a txt file into Chapters and Scenes

chapnum='1' #for starting the loop

#make a directory for the chapter and go into it
mkdir -p "$1"
cd "$1"

#split the manuscript into chapters
awk -v RS="CHAPTER\n" 'NR > 1 { print $0 > "Chapter_" (NR-1) ".txt"; close("Chapter_" (NR-1) ".txt") }' "$1"".txt"

#get the number of chapters
maxnumchaps=`ls -1 | grep -i .*.txt | wc -l`

while [ $chapnum -le $maxnumchaps ] ; do

 #split scenes within a chapter
 mkdir -p "Chapter_"$chapnum
 cd "Chapter_"$chapnum
 awk -v RS="\* \* \*\n" 'NR > 1 { print $0 > "Scene_" (NR-1) ".txt"; close("Scene_" (NR-1) ".txt") }' "../Chapter_"$chapnum".txt"
 cd .. 

 #only leave the 'chapter' info in the Chapter_n.txt file
 echo 'CHAPTER' | cat - "Chapter_"$chapnum".txt" > temp && mv temp "Chapter_"$chapnum".txt"
 awk '/END/{flag=0}flag;/CHAPTER/{flag=1}' "Chapter_"$chapnum".txt" > temp && mv temp "Chapter_"$chapnum".txt"

 #increment so we can do the next chapter
 chapnum=$(( $chapnum + 1 ))

How to use

The script can be run as a Zim ‘custom tool’ (Tools –> Custom Tools) like this: [path to location of script]/split_manuscript.sh %d. Name the tool ‘Split Manuscript’ and put whatever you like for the description. Paste your manuscript file into a Zim page or manually drop it into the appropriate spot in the directory structure. In Zim, right-click on the page, select 'Split Manuscript' from the right-click menu and sit back and wait for a while. If only it was really that easy. Okay, it is that easy IF your manuscript has exactly the right formatting.

Formatting rules

  1. The opening word of your manuscript file must be 'CHAPTER'
  2. Immediately following the word 'CHAPTER' must be the specified scene break sequence: '* * *'
  3. All scenes must be divided using '* * *'
You can change the scene break sequence in the source code on this line:
 awk -v RS="\* \* \*\n" 'NR > 1 { print $0 > "Scene_" (NR-1) ".txt"; close("Scene_" (NR-1) ".txt") }' "../Chapter_"$chapnum".txt"

For example, if you wanted to use the # character, you'd change "\* \* \*\n" to "#\n". Enjoy :).

No comments:

Post a Comment