#----------------------------------------------------------------------
#	marksub.py				JJS		3/25/97
#
#	This module translates markup tags in a variety of ways.
#----------------------------------------------------------------------
from regsub import gsub
import string

gMarkupCodes = [
	'<b>',				# bold on
	'<i>',				# italic or underline on
	'<u>',				# underline on
	'<l>',				# blink on

	'<t>']				# plain (normal) text


#----------------------------------------------------------------------
# FUNCTION: marksub
#	Given a string s and a translation dictionary trans, 
#   replace each markup tag with its translated version. 
#
#	This function also splits the lines if linelen > 0.
#	It returns a list of strings to output.
#----------------------------------------------------------------------

def marksub( s, trans, linelen=0 ):
	if linelen > 0:
		lines = splitLines(s,linelen)
	else:
		lines = [s]
	for i in range(0,len(lines)):
		for pat in trans.keys():
			lines[i] = gsub( pat, trans[pat], lines[i])
	return lines
			
#----------------------------------------------------------------------
# FUNCTION: splitLines
#	This function splits the given string into lines.
#	Ideally, it takes into account markup tags which are invisible.
#	(Currently, it just ignores them, which may cause lines to
#	come out a little shorter when lots of markups are present.)
#----------------------------------------------------------------------
def splitLines(s, linelen):
	lines = string.split(s, "\n")
	outlines = []
	for line in lines:
		while len(line) > linelen:
			pos = string.rfind(line[:linelen],' ')
			if pos < 0: break
			outlines.append(line[:pos])
			line = line[pos+1:]
		outlines.append(line)
	return outlines

#----------------------------------------------------------------------
# TRANSLATION SET: plain()
#	This set strips all markups.
#----------------------------------------------------------------------
def plain():
	out = {}
	for c in gMarkupCodes: out[c] = ''
	return out

#----------------------------------------------------------------------
# TRANSLATION SET: showcodes()
#	This set leaves all codes visible and untranslated.
#----------------------------------------------------------------------
def showcodes():
	out = {}
	for c in gMarkupCodes: out[c] = c
	return out

#----------------------------------------------------------------------
# TRANSLATION SET: html()
#	This generates mostly standard HTML.
#----------------------------------------------------------------------
def html():
	out = {}
	for c in gMarkupCodes: out[c] = c
	out['<u>'] = ''
	out['<t>'] = '</i></b>'
	return out

#----------------------------------------------------------------------
# TRANSLATION SET: vt100()
#	This set generates VT100 escape sequences.
#----------------------------------------------------------------------
def vt100():
	out = {}
	for c in gMarkupCodes: out[c] = ''
	ATT = chr(27)+'['
	out['<b>'] = ATT + '1m'
	out['<l>'] = ATT + '5m'
	out['<u>'] = ATT + '4m'
	out['<i>'] = ATT + '7m'
	out['<t>'] = ATT + '0m'
	return out

#----------------------------------------------------------------------
# FUNCTION: test() -- tests the module
#----------------------------------------------------------------------
#def test():
#	msg = 'This is <b>bold, <i>italic<t>, <u>underlined<t> text.'
#	print msg
#	print marksub( msg, plain() )
#	print marksub( msg, showcodes() )
#	print marksub( msg, html() )
#	print marksub( msg, vt100(), 40 )
#	print
#	raw_input()