''' Created on Apr 15, 2014 @author: Brett Paufler (c) Copyright Brett Paufler Poetry in Motion Python Program TODO: Note if page names have spaces in them, the created next link will likely not work, so don't use names with spaces or special characters in them (Unlike, say me, as that's how I found this out). Use at own risk. No warranty, rights, or guarantee are conferred. See paufler.net site terms of use for further information and limitations. Poetry in Motion does three things: 1) It converts ALL .TXT ".txt" text files in base directory in which is is place to html 2) It then outputs a html file for each txt file. The format of which is likely of little use to anyone other than me, Brett Paufler. But plug and paste will make this program usable by me in other instances. So, it might be a good place to start for somebody else. Or, if you know programming, it just might be eaiser to start from scratch 3) It Makes a poetryInMotion.html that links to ALL html in the directory (not just those made in previous step) NOTE: There are two different HTML page outputs and so two different html page templates below Python will need to be installed on your system for this to do you any good. I'm currently using Python 2.7.5, I believe The real beauty of this is to add a new webpage to this series all I have to do is: 1) add a txt file to this directory 2) run the beautyInMotion.py file 3) update the server Walla! Website updated. ''' import os def createPoetryInMotionHTML(): '''Input is all doc, pdf, txt files, output is a main, index sort of html poetryInMotion.HTLM that links to all those other documents with some premade text ''' print "createPoetryInMotionHTML started" outFile = "poetryInMotion.html" fileOut = open(outFile, 'w' ) header = ''' Poetry In Motion

Brett Paufler

Collected Poetic Works

...your life is now complete...




''' fileOut.write(header) footer = '''



Back to Main Writing Site

Brett Words




Disclaimer!
The contents of this site are Fiction.
Any resemblance to the contents within
and any specific or actual person, place, or thing
living or dead
real or imagined
is unintentional and purely coincidental.


(c) Copyright 2014 Brett Paufler

Brett@Paufler.net

Terms of Service

These webpages were assembled automatically from text files using the following python script.
Poetry In Motion Python File
''' #a href meat and potatoes for fN in os.listdir("."): #print fN end = fN[-3:].lower() #print end ''' limits which files types, easy to add another to the list ''' #types = ["doc", "txt", "pdf", "tml"] types = ["tml"] if end in types: if not fN == outFile: #no sense linking to itself fileOut.write('\n\n') print fN if fN.endswith("html"): name = fN[:-5] else: name = fN[:-4] fileOut.write(name) fileOut.write('\n\n

\n') fileOut.write(footer) fileOut.close() print "createPoetryInMotionHTML ended" #END createPoetryInMotionHTML() #createPoetryInMotionHTML() def fileToTitle(fN): '''takes a file path name and returns a string suitable for use as a title 01 - This Story.txt becomes This Story ''' fN = fN[:-4] fN = fN.replace("-", " ") fN = fN.replace("_", " ") fN = fN.replace(" ", " ") fN = fN.replace(" ", " ") fN = fN.replace(" ", " ") fN = "".join([i for i in fN if not i.isdigit()]) fN = fN.strip() print fN return fN def makePoetrySubPage(fN="KISS.txt", nextFile="EOF"): '''given txt file, spits out a html page based on contents ''' print "makePoetrySubPage() starts" p1 = ''' ''' #Title Goes Here p2 = '''

''' #Title Goes Here p3 = '''

by

Brett Paufler



''' # BODY GOES HERE p4 = '''

Disclaimer!
The contents of this site are Fiction.
Any resemblance to the contents within
and any specific or actual person, place, or thing
living or dead
real or imagined
is unintentional and purely coincidental.


Terms of Service

(c) Copyright 2014 Brett Paufler

Brett@Paufler.net
''' fileNameOut = fN[:-3] + "html" fileOut = open(fileNameOut, 'w' ) title = fileToTitle(fN) sOut = p1 + title + p2 + title + p3 fileOut.write(sOut.encode('ascii')) txtFile = open(fN, 'r') for line in txtFile: print line lineText = [] for item in line: #print item # This next bit strips out weird characters and eliminates spaces between letters if ord(item) > 1: if ord(item) < 127: lineText.append(item) stringOut = "".join(lineText) exclude = ["copyright", "Copyright", "Brett", "Paufler"] inExcludeList = False for word in exclude: if word in stringOut: inExcludeList = True if not inExcludeList: print stringOut fileOut.write(stringOut) fileOut.write('\n
\n') eofText = '''

That Was The Last Poem
Back To
Poetry In Motion Home ''' if nextFile == "EOF": print eofText fileOut.write(eofText) else: fileOut.write('

') fileOut.write("Next Poem") fileOut.write('') fileOut.write('



') fileOut.write('') fileOut.write('Poetry In Motion Home') fileOut.write(p4) fileOut.close() print "makePoetrySubPage() ends" #END makePoetrySubPage() #makePoetrySubPage("Kiss.txt", "reg.txt") def makeAllHTML(): '''Calls makePoetrySubpage for all html files in directory, save for the base poetryInMotion.html links each html in directory listing order to the next, the final linking back to poetryInMotion.html ''' print "makeAllHTML() starting" rawTxtList = [] for f in os.listdir("."): end = f[-3:].lower() print end if end == "txt": rawTxtList.append(f) print rawTxtList print "makeAllHTML() ending" eof = len(rawTxtList) - 1 for n in range(0, eof, 1): print "Making HTML for " + rawTxtList[n] makePoetrySubPage(rawTxtList[n], rawTxtList[n + 1]) print "Making LAST HTML for " + rawTxtList[eof] makePoetrySubPage(rawTxtList[eof], "EOF") print "makeAllHTML finished" #END makeAllHTML() #makeAllHTML() ''' COMBO OF ALL ''' def comboDoTheWorks(): '''Runs it all essentially a main ''' print "RUNNING comboDoTheWorks" #allWordToText() #Pulled, no longer in this file makeAllHTML() createPoetryInMotionHTML() comboDoTheWorks() print "The Magic Is Over (i.e. = end Poetry In Motion Module)"