'''
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 = """