#!/usr/bin/env python # Copyright (C) 2005,2006 Adalbert Prokop, adalbert.prokop(%)web.de # 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import HTMLParser import htmlentitydefs import string import sys import getopt import base64 import os.path class MyHTMLParser(HTMLParser.HTMLParser): def outFileWrite(self, message): if not self.dryRun: self.outFile.write(message) def setOutFile(self, filename): if filename != "-": if os.path.exists(filename): print "File", filename, "already exist. Won't overwrite it." sys.exit(1) if not self.dryRun: try: self.outFile = open(filename, "w") except IOError: print "Can't open file", filename, "for writing" sys.exit(1) else: self.outFile = sys.stdout self.anchorBuffer = None self.nowURLText = False self.anchorTextBuffer = [] def dryRun(self, run): self.dryRun = run def setInsertList(self, insertList): self.insertList = insertList def setDeleteList(self, deleteList): self.deleteList = deleteList def handle_starttag(self, tag, attrs): if tag == "p": outTag = "p" # if tag == "h3": # self.nowCategory = True # self.lastCategory = "" elif tag == "a": self.nowURLText = True self.anchorTextBuffer = [ "", "" ] self.anchorBuffer = attrs # elif tag == "dl": # self.dlLevel = self.dlLevel + 1 # if self.dlLevel == 0: # return # # print "dl", self.lastCategory # newCategory = self.category.newSubcategory(self.lastCategory) # self.category = newCategory # # self.showCategories(self.category.parent) # # self.lastCategory = None else: outTag = tag.upper() if tag != "a": self.outFileWrite("<"+outTag) for key, value in attrs: self.outFileWrite(" "+key.upper()+"=\""+value+"\"") self.outFileWrite(">") def handle_endtag(self, tag): def removeIcon(anchorBuffer): for key, value in anchorBuffer: if key == "icon": anchorBuffer.remove( (key, value) ) def getEncodedIcon(filename): try: f = open(filename) except IOError: print "Cant open file", filename sys.exit(1) icon = base64.b64encode(f.read()) f.close() return icon if tag == "p": outTag = "p" if tag == "a": outTag = "A" self.outFileWrite("<"+outTag) indicator = "" for bookmark in self.insertList: if self.anchorTextBuffer[1] == bookmark: removeIcon(self.anchorBuffer) self.anchorBuffer.append( ("icon", "data:image/x-icon;base64,"+getEncodedIcon(self.insertList[bookmark])) ) indicator = "(+" + self.insertList[bookmark] + ")" for bookmark in self.deleteList: if self.anchorTextBuffer[1] == bookmark: removeIcon(self.anchorBuffer) indicator = "(-)" if indicator != "" or (self.insertList == {} and self.deleteList == []): print "\""+self.anchorTextBuffer[1]+"\"", indicator for key, value in self.anchorBuffer: self.outFileWrite(" "+key.upper()+"=\""+value+"\"") self.outFileWrite(">") self.outFileWrite(self.anchorTextBuffer[0]) self.nowURLText = False self.anchorTextBuffer = [] else: outTag = tag.upper() # if tag == "h3": # self.nowCategory = False # elif tag == "a": # # print "Adding Bookmark", self.lastURLText, "to", self.category.name # self.category.newBookmark(self.lastURLText, self.lastURL) # self.nowURLText = False # # self.bookmarks.show("") # elif tag == "dl": # self.dlLevel = self.dlLevel - 1 # if self.dlLevel < 0: # return # self.category = self.category.parent # # self.bookmarks.show("") self.outFileWrite("") def handle_startendtag(self, tag, attrs): if tag == "p": outTag = "p" else: outTag = tag.upper() self.outFileWrite("<"+outTag) for key, value in attrs: self.outFileWrite(" "+key.upper()+"=\""+value+"\"") self.outFileWrite("/>") def handle_data(self, data): # if string.strip(data) == "": # return # if self.nowCategory: # self.lastCategory = self.lastCategory + data if self.nowURLText: self.anchorTextBuffer[0] = self.anchorTextBuffer[0] + data self.anchorTextBuffer[1] = self.anchorTextBuffer[1] + data else: self.outFileWrite(data) def handle_charref(self, data): # if self.nowCategory: # self.lastCategory = self.lastCategory + chr(int(data)) if self.nowURLText: self.anchorTextBuffer[0] = self.anchorTextBuffer[0] + "&#"+data+";" self.anchorTextBuffer[1] = self.anchorTextBuffer[1] + chr(int(data)) else: self.outFileWrite("&#"+data+";") def handle_entityref(self, name): data = htmlentitydefs.entitydefs[name] # if self.nowCategory: # self.lastCategory = self.lastCategory + data if self.nowURLText: self.anchorTextBuffer[0] = self.anchorTextBuffer[0] + "&"+name+";" self.anchorTextBuffer[1] = self.anchorTextBuffer[1] + data else: self.outFileWrite("&"+name+";") def handle_comment(self, data): self.outFileWrite("") def handle_decl(self, decl): self.outFileWrite("") def printUsage(): print """Usage: icon_in_bookmarks [-h] [-d ] [-b -i ] from_file to_file -h Print this help page -t Test mode. Do everything besides writing to new file. -d Delete icon from bookmark -b Boomark entry to which add the icon. Must be followed by the -i option. -i Icon for previous bookmark. Must be preceeded by -b option. from_file Bookmark file which will be read to_file Modified bookmark file (with new icons) Bookmarks are specified by their name. This requires the bookmark names to be unique, unfortunately. """ def parseOptions(cmdLineArgs): """ Parse command line options. """ global lastBookmark, insertList, deleteList, dryRun if len(cmdLineArgs) == 0: printUsage() sys.exit(0) try: opts, args = getopt.getopt(cmdLineArgs, 'hb:i:d:t') except getopt.GetoptError, msg: print "Uknown options on command line:", msg printUsage() sys.exit(1) for o, v in opts: if o in ("-h", "--help"): print "HELP!" sys.exit(0) elif o == "-b": lastBookmark = v elif o == "-i": if not lastBookmark: print "No bookmark defined for icon", v sys.exit(1) try: f = open(v) except IOError: print "Can't open file", v sys.exit(1) f.close() insertList[lastBookmark] = v lastBookmark = None elif o in ("-d", "--dir"): deleteList.append(v) elif o == "-t": dryRun = True else: print "Unknown option:",o,v printUsage() sys.exit(1) return args insertList = {} deleteList = [] lastBookmark = None dryRun = False args = parseOptions(sys.argv[1:]) if lastBookmark and insertList == {}: print "No icon defined for bookmark", lastBookmark sys.exit(1) print "Reading from", args[0], "writing to", args[1] parser = MyHTMLParser() parser.dryRun(dryRun) parser.setOutFile(args[1]) parser.setInsertList(insertList) parser.setDeleteList(deleteList) parser.feed(open(args[0]).read())