#!/usr/bin/env python

# this should really be rewritten as a psp or something

import sys
sys.path.append('/home/acb/null_htdoc/_inc')

LINKFILE="/www/null.org/data/links/links.dat"

#from nullcgi import *

import string
import re

# read the file

file = open(LINKFILE,"r")

urls = {}
desc = {}

cats = []

line = file.readline()

catname = ''
name = ''

while len(line)>0:
  line = line.rstrip()
  if line != '' and line[0]!='#':  
    if line[0:4] == '    ':
      desc[catname][name] = desc[catname][name] + line.strip()
    elif line[0:2] == '  ':
      (name, url) = re.split(', *', line.strip())
      urls[catname][name] = url
      desc[catname][name] = ''
    else:
      catname = line
      urls[catname] = {}
      desc[catname] = {}
      cats.append(catname)
  line = file.readline()


colours = [ '#ffcc99', '#ccccff', '#99ff99' ]

def tblcolour(n): return colours[n%len(colours)]

print """Content-Type: text/html

<HTML><HEAD><TITLE>links</TITLE>
<STYLE TYPE="text/css">
<!--
A { font-weight: medium; text-decoration: none; }
H1 { font-family: gill-sans, verdana, arial, geneva, sans-serif; font-size: 30pt; font-weight: normal; text-align: right; color: white; }
H2 { font-family: helvetica, arial, geneva; font-size: 12pt; }
#topmenu { 
  position:absolute; top:0px; left:0px; }
A.tmenu { color:black; font-family:times; font-size:10pt;
font-variant:small-caps; }
P.ctmenu { color:#999999; font-family:times; font-size:10pt; font-weight:bold;
font-variant:small-caps; }
-->
</STYLE>
</HEAD><BODY BGCOLOR="#9999ff" VLINK="#333399">
"""

#emitTopMenu('ELSEWHERE')

#cats = urls.keys()
#cats.sort()

print "<H1>>> Anywhere, out of this site</H1>"

print "<BR><TABLE BORDER=0 CELLSPACING=6><TR><TD VALIGN=top WIDTH=512>"

itemcount = 0
count = 0
rowcount = 0

def _to_sortkey(s):
  """converts a string into a sort key; i.e., by case-folding and stripping
  prefixes"""
  _acceptable = string.letters+' '
  s = filter(lambda a:a in _acceptable, s.lower().strip())
  if s[:4]=='the ':  s=s[4:]
  return s

for cat in cats:
  #if rowcount>=4:
  if itemcount+rowcount>=14:
    print "</TD>"
    #print "</TR><TR>"
    print "<TD VALIGN=top WIDTH=512>"
    rowcount = 0
    itemcount = 0
  colour = tblcolour(count)
  print "<TABLE BGCOLOR=%s "%colour,
  print"WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR>"
  print "<TD WIDTH=20>&nbsp;</TD><TD VALIGN=TOP ALIGN=left WIDTH=360>"
  items = [ (_to_sortkey(s),s) for s in urls[cat].keys() ]
  items.sort()
  items = [ i[1] for i in items ]
  print "<B>%s</B>"%cat
  #print "</TD><TD>&nbsp;"
  #print "</TD></TR><TR HEIGHT=100%><TD VALIGN=TOP HEIGHT=100% BGCOLOR=#ffffff>"
  print "</TD></TR><TR HEIGHT=100%><TD VALIGN=TOP HEIGHT=100% COLSPAN=2>"
  print '<TABLE BGCOLOR="#ffffff" BORDER=0 WIDTH=100% HEIGHT=100% CELLPADDING=3 CELLSPACING=0><TR><TD WIDTH=5>&nbsp;</TD><TD>'
  for item in items:
    print '<A HREF="%s" TITLE="%s">%s</A>'%(urls[cat][item], desc[cat][item], item),
    #if desc[cat][item] != '':
      #print "- %s"%desc[cat][item]
      #print "<BR><FONT SIZE=-2>%s</FONT>"%desc[cat][item]
    print '<BR>'
    itemcount = itemcount + 1
  print "</TD></TR></TABLE>"
  print "</TD></TR></TABLE><BR>"
  rowcount = rowcount + 1
  count = count + 1

print "</TD></TR></TABLE>"
