galmark.py

#!/usr/bin/python2.1

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Copyright 2001 by Lennart Poettering <mz67616c6d61726b@poettering.de>
# This script will create a HTML page of your Galeon XML Bookmarks

# Further information, usage examples and current versions may be found on
# http://www.stud.uni-hamburg.de/users/lennart/projects/galmark/

# You might want to change the first line to adapt it to your local
# python installation

import sys, string, re, time, locale
from xml.sax import make_parser, handler
from xml.sax.saxutils import *

class myHandler(handler.ContentHandler):
    
    def startDocument(self):

        print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
        print '<html>\n<head>\n<title>Galeon Bookmarks for %s</title>\n<body bgcolor="white" text="black" link="red" alink="red" vlink="red">\n<a name="top"/>' % os.environ['USER']
        self._level = 0
        self._num = [0]
  
    def endDocument(self):

        print '<small><i>Produced with <a href="http://www.stud.uni-hamburg.de/users/lennart/projects/galmark/galmark.html">galmark.py</a> by Lennart Poettering 2001</i></small>'
        print '</body>\n</html>\n'
 
    def startElement(self, name, attrs):

        if name == "folder" and attrs.has_key("name"):

            self._num[self._level] += 1
            self._level += 1
            self._num.append(0)

            if (self._level > 1):
                print "<a name=\"%s\"/><h%i>%s %s</h%i>" % (self.level_str(), self._level, self.level_str(), self.rape_name(attrs["name"]), self._level)

        elif name == "site" and attrs.has_key("name") and attrs.has_key("url"):

            u2 = u = attrs["url"].encode("iso8859-1")

            if len(u2) > 66: u2 = u[:63]+"..."

            d = ""
            if attrs.has_key("time_added") :
                d = " - %s" % time.strftime("%x", time.gmtime(int(attrs["time_added"])))
            
            print '<b><a href="%s">%s</a></b>\n&nbsp;&nbsp;<small>%s%s</small><br/>' % (escape(u), self.rape_name(attrs["name"]), escape(u2), d)
        
    def endElement(self, name):

        if (name == "folder"):
            self._level -= 1
            self._num.pop()
            print "<br>"

            if self._level <= 1:
                print '<a href="#top">top</a><br/>\n';

    def level_str(self):

        s = `self._num[1]`
        
        for i in range(2, self._level):
            s = "%s.%i" % (s, self._num[i])

        return s
        
    def rape_name(self, s):

        s = s.replace("__", "_")
        
        while (1):
            m = re.search(r'%([0-9A-F][0-9A-F])', s)

            if m == None:
                break

            s = s[:m.start()] + unichr(int(m.group(1), 16)) + s[m.end():]
            
        return escape(unicode(s.encode("iso8859-1"), "utf-8").encode("iso-8859-1"))

locale.setlocale(locale.LC_ALL, '')

parser = make_parser()
parser.setContentHandler(myHandler())
parser.parse("%s/.galeon/bookmarks.xml" % os.environ['HOME'])


Generated by GNU enscript 1.6.3.