import glob
SAMPLES_PATH = 'D:\\Devel\\pythonocc\\src\\samples'
OUTDIR = '.\\html' #the place where html files are created
#
# First get the list of all samples with a recursive glob
# see: http://www.faqts.com/knowledge_base/view.phtml/aid/2682/fid/245
#
import os
import fnmatch

class GlobDirectoryWalker:
    # a forward iterator that traverses a directory tree

    def __init__(self, directory, pattern="*"):
        self.stack = [directory]
        self.pattern = pattern
        self.files = []
        self.index = 0

    def __getitem__(self, index):
        while 1:
            try:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                # pop next directory from stack
                self.directory = self.stack.pop()
                self.files = os.listdir(self.directory)
                self.index = 0
            else:
                # got a filename
                fullname = os.path.join(self.directory, file)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                    self.stack.append(fullname)
                if fnmatch.fnmatch(file, self.pattern):
                    return fullname

#
# create the index_file
#
index_fp = open(os.path.join(OUTDIR,'index.html'),'w')
index_fp.write('<html><head><title>pythonOCC samples page</title></head><body>')

previous_path = ""
#
# Get each sample and create html file
#
for python_sample_fullname in GlobDirectoryWalker(SAMPLES_PATH, '*.py'):
    basename = os.path.basename(python_sample_fullname)
    path = os.path.dirname(python_sample_fullname)    # the html file for the sample
    # Check if the path name is different, i.e. a different 'category
    if path!=previous_path:
        index_fp.write('<h2>%s</h2>\n'%path)
    previous_path = path
    html_filename = basename.replace('.py','.html')
    html_fullname = os.path.join(OUTDIR,html_filename)
    # Write the link to the sample
    index_fp.write('<a href="./%s">%s</a><br>\n'%(html_filename,basename))
    # Create the file
    html_fp = open(html_fullname,'w+')
    # define html headers
    # Note: javascript code come from http://google-code-prettify.googlecode.com/svn/trunk/README.html
    html_fp.write('<html><head><title>pythonOCC %s sample</title></head><body onload="prettyPrint()" bgcolor="white">'%basename)
    # Adds js header
    html_fp.write('<link href="prettify.css" type="text/css" rel="stylesheet" />')
    html_fp.write('<script type="text/javascript" src="prettify.js"></script>')
    # Begin snippet
    html_fp.write('<code class="prettyprint lang-python">')
    # Copy the content of the python file to the html file
    python_sample_content = open(python_sample_fullname).read()
    html_fp.write(python_sample_content)
    # End snippet
    html_fp.write('</code></body>')
    # Close the files
    html_fp.close()
# close the index_file
index_fp.write('</body></html>')
index_fp.close()
