html_writer.py :  » Network » Grail-Internet-Browser » grail-0.6 » bookmarks » formats » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Grail Internet Browser 
Grail Internet Browser » grail 0.6 » bookmarks » formats » html_writer.py
"""Writer for Netscape HTML bookmarks."""

__version__ = '$Revision: 1.4 $'


import bookmarks                        # parent
import bookmarks.walker
import string
import sys


class Writer(bookmarks.walker.TreeWalker):
    __depth = 1
    __need_header = 1
    __alias_id = ''
    __id_next = 0

    # public interface

    def get_filetype(self):
        return "html"

    def write_tree(self, fp):
        self.__id_map = {}
        stdout = sys.stdout
        root = self.get_root()
        try:
            sys.stdout = fp
            self.walk()
            print '</DL><p>'
        finally:
            sys.stdout = stdout
            fp.close()

    # node-type handlers

    def start_Separator(self, node):
        print '%s<HR>' % self.__tab()

    def start_Bookmark(self, node):
        alias = self.__compute_alias_info(node)
        modified = node.last_modified() or ''
        if modified:
            modified = ' LAST_MODIFIED="%d"' % modified
        add_date = node.add_date() or ''
        if add_date:
            add_date = ' ADD_DATE="%d"' % add_date
        last_visit = node.last_visited()
        if last_visit:
            last_visit = ' LAST_VISIT="%d"' % last_visit
        print '%s<DT><A HREF="%s"%s%s%s%s>%s</A>' % \
              (self.__tab(), node.uri(), alias, add_date,
               last_visit, modified, bookmarks._prepstring(node.title()))
        self.__write_description(node.description())

    def start_Alias(self, node):
        refnode = node.get_refnode()
        if refnode is None or refnode.get_nodetype() == "Folder":
            return
        idref = node.idref()
        if not self.__id_map.has_key(idref):
            self.__id_map[idref] = self.__id_next
            self.__id_next = self.__id_next + 1
        self.__alias_id = ' ALIASOF="%d"' % self.__id_map[idref]
        self.start_Bookmark(node.get_refnode())

    def start_Folder(self, node):
        if self.__need_header:
            self.__need_header = 0
            self.__write_header(node)
            self.__write_description(node.description())
            print "<DL><p>"
            return
        tab = self.__tab()
        if node.expanded_p(): folded = ''
        else: folded = ' FOLDED'
        add_date = node.add_date() or ''
        if add_date:
            add_date = ' ADD_DATE="%d"' % add_date
        print '%s<DT><H3%s%s>%s</H3>' % \
              (tab, folded, add_date, node.title())
        self.__write_description(node.description())
        print tab + '<DL><p>'
        self.__depth = self.__depth + 1

    def end_Folder(self, node):
        self.__depth = self.__depth - 1
        print self.__tab() + '</DL><p>'

    # support methods

    def __compute_alias_info(self, node):
        alias = self.__alias_id
        if not alias:
            id = node.id()
            if id:
                if not self.__id_map.has_key(id):
                    self.__id_map[id] = self.__id_next
                    self.__id_next = self.__id_next + 1
                alias = ' ALIASID="%d"' % self.__id_map[id]
        self.__alias_id = ''
        return alias

    def __tab(self):
        return "    " * self.__depth

    def __write_description(self, desc):
        if not desc: return
        # write the description, sans leading and trailing whitespace
        print '<DD>%s' % string.strip(bookmarks._prepstring(desc))

    __header = """\
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
    It will be read and overwritten.
    Do Not Edit! -->
<TITLE>%(title)s</TITLE>
<H1>%(title)s</H1>"""

    def __write_header(self, root):
        print self.__header % {'title': root.title()}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.