#!/dcs/bin/python import sys, re, string, os.path #""" # Start of a HTML file, remember to give title as a parameter. html_open = \ """ %s Note: This web page was automatically created from a PalmOS "pedit32" memo.

""" html_close = "" 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 '%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 "

%s

" % title print "
"
  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 (
      ('&', '&'),
      ('<', '<'),
      ('>', '>')
      ):
      line = re.sub(orig_char, new_char, line)

    # Give URLs hyperlinks

    line = re.sub(url_re, r'\1', line)

    # Give local link words hyperlinks
    line = re.sub(locallink_re, r'\1\2', line)

    print line,

  if img:
    print img_tag(img)

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