toc.py :  » Template-Engines » Mako » Mako-0.3.2 » doc » build » lib » 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 » Template Engines » Mako 
Mako » Mako 0.3.2 » doc » build » lib » toc.py
"""
defines a pickleable, recursive "table of contents" datastructure.

TOCElements define a name, a description, and also a uniquely-identifying "path" which is
used to generate hyperlinks between document sections.
"""
import time, re

toc_by_file = {}
toc_by_path = {}
filenames = []

class TOCElement(object):
    def __init__(self, filename, name, description, parent=None, version=None, last_updated=None, doctitle=None, **kwargs):
        self.filename = filename
        self.name = re.sub(r'[<>&;%]', '', name)
        self.description = description
        self.parent = parent
        self.content = None
        self.filenames = filenames
        self.toc_by_path = toc_by_path
        self.toc_by_file = toc_by_file
        self.last_updated = time.time()
        self.version = version
        self.doctitle = doctitle
        (self.path, self.depth) = self._create_path()
        #print "NEW TOC:", self.path
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

        toc_by_path[self.path] = self
            
        self.is_top = (self.parent is not None and self.parent.filename != self.filename) or self.parent is None
        if self.is_top:
            toc_by_file[self.filename] = self
            if self.filename:
                filenames.append(self.filename)
                
        self.root = self.parent or self

        self.content = None
        self.previous = None
        self.next = None
        self.children = []
        if parent:
            if len(parent.children):
                self.previous = parent.children[-1]
                parent.children[-1].next = self
            parent.children.append(self)

    def get_page_root(self):
        return self.toc_by_file[self.filename]
        
    def get_by_path(self, path):
        return self.toc_by_path.get(path)

    def get_by_file(self, filename):
        return self.toc_by_file[filename]

    def get_link(self, extension='html', anchor=True, usefilename=True):
        if usefilename:
            if anchor:
                return "%s.%s#%s" % (self.filename, extension, self.path) 
            else:
                return "%s.%s" % (self.filename, extension)
        else:
            return "#%s" % (self.path) 


    def _create_path(self):
        elem = self
        tokens = []
        depth = 0
        while elem.parent is not None:
            tokens.insert(0, elem.name)
            elem = elem.parent
            depth +=1
        return ('_'.join(tokens), depth)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.