#!/dcs/bin/python

import sys, re, string, os.path

#""" <?xml version="1.0" encoding="UTF-8"?>

# Start of a HTML file, remember to give title as a parameter.
html_open = \
""" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>%s</title>
</head>
<LINK REL="SHORTCUT ICON" HREF="http://stromberg.dnsalias.org/~dstromberg/favicon.ico">
<body bgcolor="#000000" background="Volcanic.gif" text="#f0f0f0"
vlink="#e0e000" link="#e0e0e0">
<link href="style.css" rel="stylesheet" type="text/css" \>
<body>
Note: This web page was automatically created from a PalmOS "pedit32" memo.
<p>
"""

html_close = "</body></html>"

empty_re = re.compile("^\s*$")

# Image link must be the only thing on the line to be converted.
# Does not validate very strictly.
#image_re = re.compile("^\s*[\w.\/]*\.(png|jpg|gif)\s*$")
# Made it even less strict...
image_re = re.compile("^\s*[^\s]+\.(png|jpg|gif)\s*$")

url_re = re.compile("((?:http|https|ftp)://[^\s<>\,)]+)")

#wikiword_re = re.compile("(\s|^)([A-Z][a-z]+(?:[A-Z][a-z]+)+)(\s|$)")
locallink_re = re.compile(r"((?:\s|^)`)([\w-]+)")

def img_tag(imageline):
  img = string.strip(imageline)
  return '<img src="%s" alt="%s"/ >' % (img, img)

def main(filename):
  #title = re.sub('(.*)[.].*$', '\\1', os.path.basename(filename))
  file = open(filename)
  title = file.readline()

  prev_blank = 1
  img = None
  print html_open % title
  print "<h1>%s</h1><!--<hr/>-->" % title
  print "<pre>"
  for line in file.readlines():

    # Check for an image link
    if image_re.match(line) and prev_blank:
      img = line
      continue

    # Check for empty lines for image insertion
    if empty_re.match(line):
      prev_blank = 1
      if img:
        print img_tag(img)
        img = None
    else:
      prev_blank = 0
      if img:
        print img,
        img = None

    # Mangle HTML chars

    for orig_char, new_char in (
      ('&', '&amp;'),
      ('<', '&lt;'),
      ('>', '&gt;')
      ):
      line = re.sub(orig_char, new_char, line)

    # Give URLs hyperlinks

    line = re.sub(url_re, r'<a href="\1">\1</a>', line)

    # Give local link words hyperlinks
    line = re.sub(locallink_re, r'\1<a href="\2.html">\2</a>', line)

    print line,

  if img:
    print img_tag(img)

  print "</pre>"
  print html_close

if __name__ == '__main__':
  if len(sys.argv) >= 2:
    main(sys.argv[1])
  else:
    print "Usage: txt2html.py [SOURCE] > [TARGET]"

