# -*- 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

from wiki_comment_processor import *
from tree_rendering import *
from tree_struct import *
from label_dict import *
from html_tools import *

class Contents:
    def __init__(self, filename, linestart, lineend, text):
        self.filename  = filename
        self.linestart = linestart
        self.lineend   = lineend
        self.text      = text

class Code(Contents):
    def __init__(self, filename, linestart, lineend, text, language):
        Contents.__init__(self, filename, linestart, lineend, text)
        self.language = language

    def buildTree(self):
        pass

    def findLabels(self):
        return []

    def toHtml(self):
        if self.language == "unknown":
            return '<pre>' + html_escape(self.text) + '</pre>\n'
        else:
            (fin, fout, ferr) = os.popen3("source-highlight -s %s -f xhtml --no-doc" % self.language)
            fin.write(self.text)
            # catch IOError on closing pipe
            try:
                fin.close()
            except IOError:
                return '<pre>' + html_escape(self.text) + '</pre>\n'
            ret = fout.read()
            err = ferr.read()
            fout.close()
            ferr.close()
            # if stderr not empty, source-highlight result cannot be used: return non highlighted text
            if err == "":
                return ret
            else:
                return '<pre>' + html_escape(self.text) + '</pre>\n'

    def __str__(self):
        return "[Code] %s:(%d-%d) : %s" % (self.filename, self.linestart,
                                           self.lineend, self.text)

class Comment(Contents):
    def __init__(self, indentation, filename, linestart, lineend, text):
        Contents.__init__(self, filename, linestart, lineend, text)
        self.indentation = indentation
        self.tree = None

    def buildTree(self):
        try:
            wcp = WikiCommentProcessor.getSingleton()
            self.tree = wcp.buildTree(self)
        except ParseException, pe:
            pe.setCommentProperties(self.filename, self.linestart + 1, self.lineend + 1)
            print "WARNING: ", pe
            self.tree = NodeContentPre(text = self.text)

    def findLabels(self):
        wcp = WikiCommentProcessor.getSingleton()
        return wcp.findLabels(self.tree)

    def toHtml(self):
        htr = HtmlTreeRendering(filename = self.filename)
        indent = self.indentation * 0.6 + 0.5
        ret = '<div class="code" style="padding-left: %.1fem">\n' % indent
        ret += ' <div class="comment">\n'
        ret += htr.treeToHtml(self.tree)
        ret += ' </div>\n'
        ret += '</div>\n'
        return ret


    def __str__(self):
        return "[Comment] %s:(%d-%d) : %s" % (self.filename, self.linestart,
                                              self.lineend, self.text)