Skip to content

Instantly share code, notes, and snippets.

@PogiNate
Last active August 2, 2018 05:56
Show Gist options
  • Save PogiNate/f13f88ed4d5cdf1af1f3ff295fe54d32 to your computer and use it in GitHub Desktop.
Save PogiNate/f13f88ed4d5cdf1af1f3ff295fe54d32 to your computer and use it in GitHub Desktop.
A pre-commit hook for generating a leanpub book.txt file.
#!/usr/bin/python
# Prep output from Scrivener for publication by Leanpub.
# By Nate Dickson <nate@natedickson.com>
# 1 August 2018
import os, shutil, errno
# Remove the manuscript images folder before creating book.txt
# Scrivener will probably have wiped it out already, but let's make sure.
shutil.rmtree("manuscript/images",True)
# Get a sorted list of all files in the manuscript and add them to book.txt.
os.chdir("./manuscript")
file_list = []
for root, dirs, files in os.walk("./"):
if '.git' in dirs:
dirs.remove('.git')
for name in sorted(files):
if name != ".DS_Store" and name !="book.txt":
full_path_string = os.path.join(root,name)[2:]+"\n"
file_list.append(full_path_string)
book_file = open("./book.txt","w")
book_file.writelines(sorted(file_list))
book_file.close()
# Copy the images directory into the manuscript
# Because Scrivener wipes it out on export.
shutil.copytree("../images","images")
os.system("git add --all")
@PogiNate
Copy link
Author

PogiNate commented Aug 2, 2018

Some notes:

  • This is a very purpose driven script. If you're not taking text exported from Scrivener and publishing it in Leanpub this doesn't really get you much. But the pattern might be useful.
  • On line 12 we are stripping off the initial ./ because Leanpub doesn't need it.
  • I could probably make line 11 more extensible by telling it to look for anything in an array and skip those. But I deliberately chose not to optimize early. And it is killing me.
  • Written for python 2 because I'm lazy. I know Python 3 has easier ways to do some of this stuff, I just don't have it installed on all my machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment