# eclipthtml.py 
# Some routines for creating HTML code from Python
# (c) 1998 by Martin Preishuber <mpreishu@edu.uni-klu.ac.at>
# (c) 1999 by Emanuel Pirker <epirker@edu.uni-klu.ac.at>
# Distributed under the GNU GPL. 
# See http://www.gnu.org/copyleft/gpl.html#SEC1 for the full licence

from string import strip, lower, rjust, ljust, center, find, atoi

import copy
import sys

# ----------------------------------------------------------------------
# Definition of some module specific constants

HTML = "html"
Roxen = "roxen"
Plain = "plain"

True = 1
False = 0

def cfill(source, char, width):
    while len(source) < width:
	source = source + char
    return source

# ----------------------------------------------------------------------
# Change default format 

# default Format
format = HTML

# change the default Format
def ChangeFormat(newformat):
    global format
    format = newformat

# ----------------------------------------------------------------------
# Removes html-tags from some text

def Cleanup(text):
    if (format == Plain):
	while (find(text, "<") != -1) and (find(text, ">") != -1) and (find(text, "<") < find(text, ">")):
	    text = text[:find(text, "<")] + text[find(text, ">") + 1:]
    return text

# ----------------------------------------------------------------------
# Creates a html-header with title string

def Head(title = "", style = ""):
    if (format == HTML) or (format == Roxen):
	line = "<head>\n"
	if (title != ""):
	    line = "%s  <title>\n    %s\n  </title>\n" % (line, strip(title))
	if (style != ""):
	    line = "%s<style type=\"text/css\">\n<!--\n%s\n//-->\n</style>\n" % (line, style)
        line = "%s</head>\n\n" % line
    elif (format == Plain):
	line = ""
    return line

# ----------------------------------------------------------------------
# Start of the Body 

def BodyStart(text = "", bgcolor = "", link = "", alink = "", vlink = "", background = ""):
    if (format == HTML) or (format == Roxen):
	line = "<body"
	if (text != ""):
	    line = "%s text=\"%s\"" % (line, text)
	if (bgcolor != ""):
	    line = "%s bgcolor=\"%s\"" % (line, bgcolor)
	if (link != ""):
	    line = "%s link=\"%s\"" % (line, link)
	if (alink != ""):
	    line = "%s alink=\"%s\"" % (line, alink)
	if (vlink != ""):
	    line = "%s vlink=\"%s\"" % (line, vlink)
	if (background != "") and (background != None):
	    line = "%s background=\"%s\"" % (line, background)
	line = "%s>\n\n" % line
    elif (format == Plain):
	line = ""
    return line

# ----------------------------------------------------------------------
# End of the Body 

def BodyEnd():
    if (format == HTML) or (format == Roxen):
	line = "</body>\n"
    elif (format == Plain):
	line = ""
    return line

# ----------------------------------------------------------------------
# Definition of a Table, TR, TH and TD

def TD(text = "", align = "", valign = "", bgcolor = "", colspan = 0, 
       nowrap = False, width = "", widthplain = 25, tag = "TD"):
	if (format == HTML) or (format == Roxen):
	    line = "    <%s" % tag
	    if (bgcolor != ""):
		line = "%s bgcolor=\"%s\"" % (line, bgcolor)
	    if (align != ""):
		line = "%s align=\"%s\"" % (line, align)
	    if (valign != ""):
		line = "%s valign=\"%s\"" % (line, valign)
	    if (width != ""):
		line = "%s width=\"%s\"" % (line, width)
	    if (colspan != 0):
		line = "%s colspan=%d" % (line, colspan)
	    if (nowrap == True):
		line = "%s nowrap" % line
	    line = "%s>\n      &nbsp;%s&nbsp;\n    </%s>\n" % (line, text, tag)

	elif (format == Plain):
	    if (lower(align) == "right"):
		line = "%s" % rjust(str(text), widthplain - 1) + " "
	    elif (lower(align) == "left") or (align == ""):
		line = "%s" % ljust(str(text), widthplain)
	    elif (lower(align) == "center"):
		line = "%s" % center(str(text), widthplain)

	return line

def TR(columns):
    if (format == HTML) or (format == Roxen):
	line = ("  <tr>\n")
	for i in columns:
	    line = line + i
	line = line + "  </tr>\n"

    elif (format == Plain):
	for i in columns:
	    line = line + i

    return line

def TableRow( columns, align = "" ):
    tdcols = []
    for i in columns:
	tdcols.append( TD( i ) )

    return TR( tdcols )

def Table( rows = [], border = -1, width = "", bgcolor = "", cellpadding = -1, cellspacing = -1):

    if (format == Roxen) or (format == HTML):
	tablehead = "<table"
	if (border != -1):
	    tablehead = tablehead + " border=%d" % border
	if (width != ""):
	    tablehead = tablehead + " width=\"%s\"" % width
	if (bgcolor != ""):
	    tablehead = tablehead + " bgcolor=\"%s\"" % bgcolor
	if (cellpadding != -1):
	    tablehead = tablehead + " cellpadding=%d" % cellpadding
	if (cellspacing != -1):
	    tablehead = tablehead + " cellspacing=%d" % cellspacing

	line = tablehead + '>\n'
	for i in rows:
	    if (i != "") and (i != None):
		line = line + i + '\n'
	line = line + '</table>\n'

    elif (format == Plain):
	for i in range(len(rows)):
	    if (rows[i] != "") and (rows[i] != None):
		line = line + rows[i] + '\n'

    return line


def TH(text = "", align = "", valign = "", bgcolor = "", colspan = 0, nowrap = False, width = "", widthplain = 25, tag = "TH"):
    return TD(text, align, valign, bgcolor, colspan, nowrap, width, widthplain, tag)


# ----------------------------------------------------------------------
# Standard-Paragraph

def P(paragraph = ""):
    if (format == HTML) or (format == Roxen):
	if (paragraph == ""):
	    line = "<p></p>\n\n"
	else:
	    line = "<p>\n  %s\n</p>\n\n" % paragraph
    elif (format == Plain):
	line = "\n%s\n" % paragraph
    return line

def PStart():
    if (format == HTML) or (format == Roxen):
	line = "<p>"
    elif (format == Plain):
	line = ""
    return line

def PEnd():
    if (format == HTML) or (format == Roxen):
	line = "</p>"
    elif (format == Plain):
	line = "\n"
    return line

# ----------------------------------------------------------------------
# Unordered Lists

def ULStart(type = None):
    if (format == HTML) or (format == Roxen):
	line = "<ul"
	if (type != None):
	    line = "%s type=\"%s\">" % (line, type)
	else:
	    line = "%s>" % line
    elif (format == Plain):
	line = ""
    return line

def ULEnd():
    if (format == HTML) or (format == Roxen):
	line = "</ul>\n"
    elif (format == Plain):
	line = "\n"
    return line

def LI(item):
    if (format == HTML) or (format == Roxen):
	line = "<li>%s</li>" % item
    elif (format == Plain):
	line = ""
    return line


# ----------------------------------------------------------------------
# The Font-Tag

def Font(text, size = "", color = "", bold = False, italic = False):
    if (format == HTML) or (format == Roxen):
	line = "<font"
	size = str(size)
	if (size != ""):
	    line = "%s size=%s" % (line, size)
	if (color != ""):
	    line = "%s color=\"%s\"" % (line, color)
	if (bold == True):
	    text = "<b>%s</b>" % str(text)
	if (italic == True):
	    text = "<i>%s</i>" % str(text)
	line = "%s>%s</font>" % (line, str(text))
    elif (format == Plain):
	line = text
    return line

# ----------------------------------------------------------------------
# Bold Font (extra)

def Bold(text):
    if (format == HTML) or (format == Roxen):
	line = "<b>%s</b>" % text
    elif (format == Plain):
	line = text
    return line

# ----------------------------------------------------------------------
# The Center-Tag

def Center(line):
    if (format == HTML) or (format == Roxen):
	result = "<center>" + line + "</center>\n"
    elif (format == Plain):
	result = center(line, 80)
    return result

def CenterHead():
    if (format == HTML) or (format == Roxen):
	result = "\n<center>\n"
    elif (format == Plain):
	result = ""
    return result

def CenterTail():
    if (format == HTML) or (format == Roxen):
	result = "\n</center>\n"
    elif (format == Plain):
	result = ""
    return result

# ----------------------------------------------------------------------
# The GH (Graphical Header) Tag

def GH(text, size = 4, color = "", bold = False):
    if (format == Roxen):
	line = "\n<gh%d" % size
	if (color != ""):
	    line = "%s fg=\"%s\"" % (line, color)
	if (bold == True):
	    line = "%s bold" % line
	line = "%s>%s</gh%d>\n\n" % (line, text, size)
	line = line + P("")
    elif (format == HTML):
	if (color != "") or (bold == True):
	    text = Font(text, color = color, bold = bold)
	line = P("") + text + P("")
    elif (format == Plain):
	line = "\n%s\n\n" % text
    return line

# ----------------------------------------------------------------------
# A Paragraph, using a Definition List

def Paragraph(paragraph):
    if (format == Roxen) or (format == HTML):
	line = "<dl><dd>\n%s</dd></dl>\n" % paragraph
    elif (format == Plain):
	line = "\n%s\n" % paragraph
    return line

def ParagraphHead():
    if (format == Roxen) or (format == HTML):
	line = "<dl><dd>\n"
    elif (format == Plain):
	line = ""
    return line

def ParagraphTail():
    if (format == Roxen) or (format == HTML):
	line = "</dd></dl>\n"
    elif (format == Plain):
	line = ""
    return line

# ----------------------------------------------------------------------
# Comment

def Comment(line):
    line = "<!-- %s -->\n" % line
    return line

# ----------------------------------------------------------------------
# Link

def Link(name, filename, target = ""):
    if (format == HTML) or (format == Roxen):
	line = "<a href=\"%s\"" % filename
	if (target != ""):
	    line = "%s target=\"%s\"" % (line, target)
	line = "%s>%s</a>" % (line, name)
    elif (format == Plain):
	line = name
    return line

# ----------------------------------------------------------------------
# In-Document Link

def InLink(name, filename):
    if (format == HTML) or (format == Roxen):
	line = "<a name=\"%s\"" % filename
	line = "%s>%s</a>" % (line, name)
    elif (format == Plain):
	line = name
    return line

# ----------------------------------------------------------------------
# Spacer

def Spacer(type = "", size = 0, sizeplain = 0):
    line = ""
    if (format == HTML) or (format == Roxen):
	if (size != 0):
	    line = "<spacer type=\"%s\" size=%d>\n\n" % (type, size)
    elif (format == Plain):
	if (sizeplain != 0):
	    if (lower(type) == "horizontal"):
		line = cfill("", "\n", sizeplain)
	    elif (lower(type) == "vertical"):
		line = cfill("", " ", sizeplain)
    return line

# ----------------------------------------------------------------------
# Horizontal Rule

def HR(width = "", noshade = False, size = -1, align = "", color = ""):
    if (format == HTML) or (format == Roxen):
	line = "<HR"
	if (width != ""):
	    line = "%s width=\"%s\"" % (line, width)
	if (noshade == True):
	    line = "%s noshade" % line
	if (size != -1):
	    line = "%s size=%d" % (line, size)
	if (align != ""):
	    line = "%s align=\"%s\"" % (line, align)
	if (color != ""):
	    line = "%s color=\"%s\"" % (line, color)
	line = "%s>\n\n" % line
    elif (format == Plain):
	if (width != ""):
	    line = cfill("", "-", atoi(width))
	else:
	    line = cfill("", "-", 40)
	if (lower(align) == "center"):
	    line = center(line, 80)
	elif (lower(align) == "right"):
	    line = rjust(line, 80)
    return line

# ----------------------------------------------------------------------
# Image

def Image(filename, alt, options=""):
    if (format == HTML) or (format == Roxen):
	line = "<img src=\"%s\" alt=\"%s\" %s>" % (filename, alt, options)
    elif (format == Plain):
	line = name
    return line
