''' HTML to Python Recursive Loop If you are looking at a web page: Copy and Paste this text into an empty recurse.py file Run the Python Script And This web page will be recreated and outputed as a result. (Or download Download Text File: recurse.txt and use a text editor to change into a Python file) If you are reading this in a Python file: Run the Python script And view the resulting recurse.html in your favorite browsing tool Or in short, this is a test program that outputs a html file that if loaded into a python environment will excute code that outputs working html that creates python that outputs html ad infinitum... ''' import jinja2 import cgi def readSelf(): ''' Creates duplicate of this (the calling python __file__) in text format. It's .txt because my current hosting environment will not serve up .py files. Otherwise, I could just link to __file__ itself at the end. Note: The saved file is not a requirement for the html conversion, as text is returned, and the returned value is what I use in parseSelf(). ''' text = open(__file__,"r").read() open(__file__[:-2]+"txt", "w").write(text) return text def parseSelf(text): ''' Given the text of this python script from readSelf(), the text is returned as parsed html code ''' #This is the sole use of the cgi library, so with a few more lines of code #The cgi library could be made redundant parsed = "" text = text.split("\n") for line in text: #Most of the file is converted to html at this step #The only lines that aren't include the given text #Everything paste cgi.escape is to preserve the down load links #And the only reason to do that was proof of concent #If I can preserve one type of link, given effort, #I could preserve them all if "Download Text File" not in line: line = cgi.escape(line) line = line.replace(" ", " ") elif "Download" in line and \ ".txt" in line: #A regex might work better here #But this is what I got to work #File links are hard coded to names 7 characters long line = line.split(".txt")[0][-7:] line = line print line nb = (" " * 8) href = 'Download' #Notice that d = ' Text File: ' #These Three Lines end = '.txt' #Are Seperated line = nb + href + line + c + d + line + end print line else: line = (" " * 8) + line.strip() parsed += line + "
\n" print parsed #And that's the hard part #This here is another step that's not formally required #This project started as a jinja2 project #And I thought it would be much more involved than it turned out to be #In the end, parsed = parsed would work here parsed = parsed.split("
\n
\n") #The following is all commented out as not needed to solve this problem #These are values to input into the rawTemplate #headInfo = "#headInfo HERE" #headerText = "#headerText HERE" #footerText = "#footerText HERE" #Once again, a more involved template would require more information #This is the dictionary required for a 'context' to render the template mapping = {#"headInfo":headInfo, #"headerText":headerText, #"footerText":footerText, "functionMapping":parsed } #This is the raw template # '#' is a comment in python # '{# {headerText} #}', this is a comment in jinja2 #So, this a raw html skeleton with a single loop #For this example, the for loop could be replaced by a simple variable rawTemplate = """ {# {headInfo} #} {# {headerText} #} {% for item in functionMapping %} {{item}}

{% endfor %} {# {footerText} #} """ #And finally, #This converts all of the above into a template #and returns it webTemplate = jinja2.Template(rawTemplate) return webTemplate.render(mapping) if __name__ == '__main__': ''' Putting it all together: readSelf() returns text parseSelf() returns parsedText which is saved to file ''' temp = readSelf() parsedText = parseSelf(temp) print parsedText open(__file__[:-3]+".html", "w").write(parsedText) #The following two functions have nothing to do with this project #And so, occur after main #They are the default way to run jinja2 #And are simpy reminders to myself def loadTemplates(): ''' loads all files in the current directory into jinja2 ''' env = jinja2.Environment() env.loader = jinja2.FileSystemLoader(".") print env.list_templates() return env def useTemplate(): ''' An example usage case, which requires the following text files to be loaded in the same directory Download Text File: basehtm.txt Download Text File: bodyhtm.txt Download Text File: divhtml.txt jinja2 will only follow one fork at a time Jinja2 loads from most specific to general So, divhtml loads bodyhtm, which loads basehtm to give the final product Since I was expecting the opposite, this took me more than a few hours to figure out ''' env = loadTemplates() a = env.get_template("div.txt") page = a.render() return page ''' A text version of this program (which if renamed with a .py extension) can be found at: Download Text File: recurse.txt (c) Copyright Brett Paufler 12-3-14 Brett@Paufler.net No warranties expressed or implied. No money has changed hands. You get what you pay for. And why your would use my code (rather than just look it over. to get past any hard part, say the conditional use of the cgi library replace) is beyond me. '''