# -*- coding: utf-8 -*- 

# lp4all: literate programming embedded in source code as wiki comments
# Copyright (C) 2006 Jean-Marie Favreau, Frédéric Lehobey, David Mentré
#                    and Thomas Petazzoni
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import wiki_comment_processor
from iface import *
from time import gmtime, strftime
from html_tools import *

def format_dirpath(project, path):
    path = os.path.normpath(os.path.join(project, path))
    rpath = ""
    pos = 1
    for e in path.split('/'):
        rpath += " / " + '<a href="' + ("../" * (len(path.split('/'))-pos)) + 'index.html">' + e + "</a>"
        pos += 1
    return rpath

def format_filepath(project, path):
    path = os.path.normpath(os.path.join(project, path))
    rpath = ""
    pos = 2
    for e in path.split('/')[:-1]:
        rpath += " / " + '<a href="' + ("../" * (len(path.split('/'))-pos)) + 'index.html">' + e + "</a>"
        pos += 1
    rpath += " / " + path.split('/')[-1]
    return rpath

def html_footer():
    date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    return "<div id=\"footer\">Generated by <a href=\"http://www.linux-france.org/~dmentre/lp4all/\">lp4all</a> - " + date + "</div>"

def index(outdir, project, curdir, files, dirs, comments = []):
    path = os.path.join(outdir, "./" + curdir, "index.html")
    fp = open(path, "w")
    csspath = "../" * (len(curdir.split('/'))-1)
    fp.write("<html>\n")
    fp.write("<head>\n")
    fp.write("<title>Index of %s</title>\n" % curdir)
    fp.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>')
    fp.write('<link rel="stylesheet" type="text/css" href="%slp4all.css" media="screen"/>\n' % csspath)
    fp.write("</head>\n")
    fp.write("<body>\n")
    fp.write('<div id="container">\n')
    fp.write('<h1>' + format_dirpath(project, curdir) + '</h1>\n')
    fp.write('<div id="contents">\n')
    if len(comments) != 0:
        fp.write('<div class="comment_header">\n')
        for c in comments:
            fp.write(c.toHtml())
        fp.write('</div>\n')
    fp.write('<ul>\n')
    if curdir != ".":
        fp.write('<li><a href="../index.html">..</a></li>\n')
    for dir in dirs:
        link = os.path.join(dir, "index.html")
        fp.write('<li><a href="' + link + '">' + dir + '/</a></li>\n')
    for file in files:
        link = file + ".html"
        fp.write('<li><a href="' + link + '">' + file + '</a></li>\n')
    fp.write('</ul>\n')
    fp.write("</div>\n")
    fp.write(html_footer() + "</div>\n")
    fp.write("</body>\n")
    fp.write("</html>")

def file(outdir, project, blocks, filename):
    outfile = os.path.join(outdir, filename + ".html")
    csspath = "../" * (len(os.path.normpath(filename).split('/'))-1)
    file = open(outfile, "w")

    file.write("<html>\n")
    file.write("<head>\n")
    file.write("<title>Source code of %s</title>\n" % filename)
    file.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>')
    file.write('<link rel="stylesheet" type="text/css" href="%slp4all.css" media="screen"/>\n' % csspath)
    file.write("</head>\n")
    file.write("<body>\n")
    file.write('<div id="container">\n')
    file.write('<div id="header">\n')
    file.write('<h1>' + format_filepath(project, filename) + '</h1>\n')
    file.write('</div>\n')
    file.write('<div id="contents">\n')
    for block in blocks:
        file.write(block.toHtml())
    file.write(html_footer() + "</div>\n")
    file.write("</body>\n")
    file.write("</html>")