#!/usr/bin/env python
# 5 minutes programm
# (c) lg 2002

import sys

backcol = '#000000'
def write_header():
    print "<html><head></head><body bgcolor=%s><basefont size=1><pre>" %(backcol)

def write_tailer():
    print "</body></html>\n"

av = sys.argv
ac = len(av)

progn = av[0]
try:
    xpm_file = open(av[1], "r");
    txt_file = open(av[2], "r");
except IOError, er:
    print er
    sys.exit(-1);

# parse XPM
str = xpm_file.readline()
while (str[0] != '"'):
    str = xpm_file.readline()
    
str = str[1:-3]
# [ width, height, colors, symbols per color ]
# XXX doesn't handle strings like "   123      45      67       89"
xpm_props = str.split(" ");

def insert_color(str, cols):
    key = str[1:int(xpm_props[3])+1]
    str = str[str.index("c")+2:-3]
    if (str == "None"):
        str = backcol
    cols[key] = str
    

def make_string(str, cols):
    ft = 1; # firt time
    
    rb = int(xpm_props[3]);
    findex = str.index('"') + 1
    str = str[findex:]
    lindex = str.index('"');
    str = str[:lindex]
    strlen = len(str)
    prevcol = '';
    while (strlen > 0):
        col = str[0:rb]

        # get next character from txt_file
        char = txt_file.read(1);
        while (char == '\n' or char == '\r' or char == ' ' or char == '\t' or char == '_'):
            char = txt_file.read(1);

        # according to ISO8859
        if (char == '<'):
            char = "&lt;"
        elif (char == '>'):
            char = "&gt;"
        elif (char == '"'):
            char = "&quot;"
        elif (char == '&'):
            char = "&amp;"
        
        if (prevcol == col):
            sys.stdout.write("%s" %char)
        elif (ft == 1):
            sys.stdout.write("<font color=%s>%s" %(cols[col], char))
        else:
            sys.stdout.write("</font><font color=%s>%s" %(cols[col], char))
            ft = 0;
            
        prevcol = col;
        strlen -= rb
        str = str[rb:]

    if (ft == 1):
        print
    else:
        print "</font>"
    

# fill colors
cols = {}
cnt = 0;
while (cnt < int(xpm_props[2])):
    str = xpm_file.readline()
    try:
        insert_color(str, cols)
    except:
        cnt = cnt - 1;
    cnt = cnt + 1;

write_header()

str = xpm_file.readline();
while (str != ""):
    try:
        make_string(str, cols)
    except:
        pass
    str = xpm_file.readline()
    
write_tailer()
Hosted by uCoz