#!/usr/bin/env python # # Fix the html that iPhoto 1.1.1 outputs, by adding some # next/previous links, and making room for some commentary # (that I'll add later, by hand). # # You should be in the directory to which you exported your # photos book as html: it should have an index.html and # subdirectories named Images, Pages, and Thumbnails. # # Original "Pages" directory is renamed as "Pages-orig" # Output is a new directory named Pages-fixed, which # is then renamed as "Pages". # import os, re, sys oldDir = 'Pages' oldDirNewName = 'Pages-orig' newDir = 'Pages-fixed' namesRE = re.compile( 'Image([0-9][0-9]*)\.html' ) def figure_file_names (): all_files = os.listdir( oldDir ) file_names = [] nrs_names = [] for fn in all_files: mo = namesRE.match( fn ) if mo: file_names.append( fn ) nr = mo.groups()[0] nrs_names.append( ( int(nr), fn ) ) nrs_names.sort() next = map( lambda x: x[1], nrs_names ) next.append( '@NEXT@' ) this = [ '@PREV@' ] + next prev = [ 'TRASH' ] + this tpn = zip( this,prev,next ) print 'THIS PREV NEXT ' for ff in tpn: print ff[0], ff[1], ff[2] return tpn[1:] altalign = re.compile( ' alt=' ) brCaption = re.compile( '
(.*)' ) # The replacement string for the "
Caption" line # needs "previous" and "next" link file names subsituted # into it. brRepl_1 = r'''

\1

@@DESC@@

[Index]         [Last]' else: prevS = prev + '">[Previous]' # Fix the @NEXT@ part of that replacement string if next == '@NEXT@': first = this_prev_next[0][0] nextS = first + '">[First]' else: nextS = next + '">[Next]' brRepl = brRepl_1 + prevS + brRepl_2 + nextS + "" stage2 = brCaption.sub( brRepl, stage1, 1 ) outfn = os.path.join( newDir, this ) out = open( outfn, 'w' ) out.write( stage2 ) out.close() def in_iPhoto_dir (): files = os.listdir( '.' ) for dd in [ 'Images', 'Pages', 'Thumbnails' ]: if dd not in files: return 0 if not os.path.isdir(dd): return 0 return 1 def main (): if in_iPhoto_dir(): this_prev_next = figure_file_names() os.mkdir( newDir ) fix_the_files( this_prev_next ) os.rename( oldDir, oldDirNewName ) os.rename( newDir, oldDir ) if __name__ == "__main__": main()