-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgal2nsbm.py
executable file
·120 lines (83 loc) · 4.3 KB
/
gal2nsbm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
## 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 <[email protected]>
# adapted to Netscape Bookmarks by Olaf Klischat <[email protected]>
# 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 *
def get_uniqe_id():
import random
return 'NC:BookmarksRoot#$%08x' % (random.random() * 0x7fffffff)
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']
print '<!DOCTYPE NETSCAPE-Bookmark-file-1>'
print '<!-- This is an automatically generated file.'
print 'It will be read and overwritten.'
print 'Do Not Edit! -->'
print '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'
print '<TITLE>Bookmarks</TITLE>'
print '<H1>Bookmarks</H1>'
print "<DL><p>"
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'
print "</DL><p>"
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 '<DT><H3 ADD_DATE="1049880984" ID="%s">%s</H3>' % (get_uniqe_id(), self.rape_name(attrs["name"]))
print '<DL><p>'
elif name == "site" and attrs.has_key("name") and attrs.has_key("url"):
u = attrs["url"].encode("iso8859-1")
#d = ""
#if attrs.has_key("time_added") :
# d = " - %s" % time.strftime("%x", time.gmtime(int(attrs["time_added"])))
print '<DT><A HREF="%s" ADD_DATE="1049880984" LAST_VISIT="1049880984" LAST_MODIFIED="1049880984">%s</A>' % (escape(u), self.rape_name(attrs["name"]))
#print '<b><a href="%s">%s</a></b>\n <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 "</DL><p>"
#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("file://%s/.galeon/bookmarks.xml" % os.environ['HOME'])