"Fossies" - the Fresh Open Source Software Archive

Member "Tahchee-1.0.0/Sources/tahchee/plugins/linking.py" (24 Oct 2009, 3970 Bytes) of package /linux/privat/old/tahchee-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "linking.py" see the Fossies "Dox" file reference documentation.

    1 # vim: ts=4
    2 # -----------------------------------------------------------------------------
    3 # Project           :   Tahchee                      <http://www.ivy.fr/tachee>
    4 # -----------------------------------------------------------------------------
    5 # Author            :   Sebastien Pierre                     <sebastien@ivy.fr>
    6 # License           :   Revised BSD License
    7 # -----------------------------------------------------------------------------
    8 # Creation date     :   25-Feb-2006
    9 # Last mod.         :   02-May-2007
   10 # -----------------------------------------------------------------------------
   11 
   12 import os, sys
   13 
   14 __doc__ = """
   15 
   16 """
   17 
   18 NAME    = "linking"
   19 VERSION = None
   20 SUMMARY = "Useful functions to manage links within a site"
   21 
   22 class LinkingPlugin:
   23 
   24     def __init__( self, site ):
   25         self.site = site
   26 
   27     def name( self ): return NAME
   28     def summary( self ): return SUMMARY
   29     def version( self ): return VERSION
   30     def doc( self ): return __doc__
   31 
   32     def install( self, localdict ):
   33         localdict["linking"] = self
   34 
   35     def hierarchy( self, pagePath ):
   36         """Creates an HTML string that contains the clickable path from the
   37         website root to the current page. This can be placed in a navigation
   38         bar."""
   39 
   40         parents  = (pagePath.split("/"))
   41         page     = parents[-1]
   42         parents  = parents[:-1]
   43 
   44         # The first element is the site root
   45         res = "<a href='%s'>%s</a> / " %\
   46         ( self.link(pagePath, "/index.html"), self.name())
   47         path = ""
   48 
   49         # Now we add links for the parents
   50         for path_index in range(0, len(parents)):
   51             path += parents[path_index] + "/"
   52             res += "<a href='%s'>%s</a> / " % (
   53                 self.link(pagePath,
   54                 path+"/index.html"),
   55                 parents[path_index]
   56             )
   57 
   58         # We add the last link ofr the file
   59         radix = os.path.splitext(page)[0]
   60         if radix!="index":
   61             return res+"<a href='%s'>%s</a> " % (
   62                 self.link(pagePath, radix), radix
   63             )
   64         else:
   65             # We get rid of the trailing " / "
   66             return res[:-3]
   67 
   68     def _normalize( self, path ):
   69         """Normalizes the given path, fixing some issues with Windows \\ in
   70         paths."""
   71         return path.replace("\\", "/")
   72 
   73     def _abspath( self, path ):
   74         """Ensures that the path is absolute. This does not use the Python
   75         os.abspath method, but simply ensures that the path does not starts with
   76         '.' and starts with a '/'."""
   77         if not path: return "/"
   78         path = self._normalize(path)
   79         if path[0] == ".": path = path[1:]
   80         if not path or not path[0] == "/": path = "/" + path
   81         return path
   82 
   83     def a( self, target, content ):
   84         return "<a href='%s'>%s</a>" % (target, content)
   85 
   86     def link( self, fromPath, toPath, checkLink=True ):
   87         """Creates a relative or absolute link (if the site is in local mode,
   88         then the link is relative, otherwise it is absolute) from the given path
   89         to the other path. The 'fromPath' is RELATIVE TO THE PAGES DIRECTORY."""
   90         # WE SHOULD ASSERT THAT FROM PATH IS A FILE, OR IF IT IS A DIRECTORY, IT
   91         # MUST END WITH /
   92         fromPath = self._abspath(fromPath)
   93         toPath   = self._abspath(toPath)
   94         # Now, all paths are of the form
   95         # - '/'
   96         # - '/FILE' or '/DIR'
   97         # - '/DIR/FILE' or '/DIR/DIR'
   98         # - ...
   99         from_el   = fromPath.split("/")[1:]
  100         to_el     = toPath.split("/")[1:]
  101         from_dirs = len(from_el) > 1 and from_el[:-1] or []
  102         to_dirs   = len(to_el) > 1 and to_el[:-1] or []
  103         from_file = from_el[-1]
  104         to_file   = to_el[-1]
  105         common    = -1
  106         for c in range(0, min(len(from_dirs), len(to_dirs))):
  107             if from_dirs[c] != to_dirs[c]:
  108                 break
  109             common = c
  110         # If there is no "to_file", we force the "/"
  111         if not to_file: to_file = "/"
  112         res = None
  113         # Both paths have the same directories in common
  114         if from_dirs == to_dirs:
  115             res = to_file
  116         else:
  117             prefix  = ""
  118             if common == -1:
  119                 prefix += "../" * (len(from_dirs))
  120                 prefix += "/".join(to_dirs)
  121             else:
  122                 common += 1
  123                 prefix += "../" * (len(from_dirs[common:]))
  124                 if common < len(to_dirs):
  125                     prefix +=  "/".join(to_dirs[common:])
  126             if prefix and not prefix[-1] == "/": prefix += "/"
  127             res = prefix + to_file
  128         return res
  129 
  130 # EOF