''' Created on May 2, 2014 @author: Brett Paufler (c) Copyright Brett Paufler TODO: This has an error wherein if the titles have spaces in them, the link to the next page will be set up wrong. Need to replace " " with "%20" in the next link NOTE TO SELF: without the base text files, if this is run again with additional txt files added, the links between pages will not be updated, so it's a one shot for this directory (hence the reason it's saved as a txt) directory is intended to be static RecipeFormatter.py is a Python Program (or will be if the .txt file is saved as a .py file an the appropriate text editor) Use at own risk. No warranty, rights, or guarantee are conferred. See paufler.net site terms of use for further information and limitations. The Recipe Formatter does two things: 2) It then outputs a html file for each txt file in base directory in which it is placed 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. (for instance this program is a direct port of Poetry In Motion which does a similar thing) So, it might be a good place to start for somebody else (or me). But if you know programming, it just might be easier to start from scratch 3) It Makes a recipes.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 recipeFormatter.py file 3) update the server Walla! Website updated. ''' import os def createRecipeHTML(): '''Input is all html index sort of html recipes.html that links to all those other html ''' print "createRecipe started" outFile = "recipes.html" fileOut = open(outFile, 'w' ) header = ''' Brett's Recipes Landing Page

Brett Paufler

Recipes of Note

. . . individual results may vary . . .



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


Back to Main Food Site

Brett Food Home

Disclaimer!
Use at own risk.

No warranty or guarantee.

Recipes are online,
at my own site,
for my own use.

I can't even remember making
some of these anymore.

What you do with information
is a matter of freewill...


(c) Copyright 2014 Brett Paufler
-- though most recipes are from the 2006-10 era --

Brett@Paufler.net

Terms of Service

These webpages were assembled automatically from text files using the following python script.
recipeFormatter
''' #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 "createRecipeHTML ended" 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 makeRecipeSubPage(fN="KISS.txt", nextFile="EOF"): '''given txt file, spits out a html page based on contents ''' print "makeReciptSubPage() starts" p1 = ''' ''' #Title Goes Here p2 = '''

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

by

Brett Paufler



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

Disclaimer!
Use at own risk.

No warranty or guarantee.

Recipes are online,
at my own site,
for my own use.

I can't even remember making
some of these anymore.

What you do with information
is a matter of freewill...

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 Recipe
Back To
Recipes Home ''' if nextFile == "EOF": print eofText fileOut.write(eofText) else: fileOut.write('

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



') fileOut.write('') fileOut.write('Recipes Home') fileOut.write(p4) fileOut.close() print "makeRecipeSubPage() ends" def makeAllHTML(): '''Calls makeRecipeSubpage for all html files in directory, save for the base recipes.html links each html in directory listing order to the next, the final linking back to poetry_in_motion.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] makeRecipeSubPage(rawTxtList[n], rawTxtList[n + 1]) print "Making LAST HTML for " + rawTxtList[eof] makeRecipeSubPage(rawTxtList[eof], "EOF") print "makeAllHTML finished" #END makeAllHTML() #makeAllHTML() ''' COMBO OF ALL ''' def comboDoTheWorks(): '''Runs it all essentially a main ''' print "RUNNING comboDoTheWorks" makeAllHTML() createRecipeHTML() comboDoTheWorks() print "The Magic Is Over (i.e. = end Poetry In Motion Module)"