Commits

Matthew Scott  committed 97d70b4

+ Use stylesheet_path instead of stylesheet_url to support relative paths

  • Participants
  • Parent commits d9762ff

Comments (0)

Files changed (4)

 # Specify the cache directory to use.
 slipper.DEFAULT.cache_dir = %(here)s/cache
 
-# Specify the stylesheet URL to refer to in generated documents.
+# Specify the absolute path of the stylesheet within the repository 
+# to refer to in generated documents.  It is converted to a relative path
+# to support URL rewriting.
+#
 # Comment this out to use default in-line Docutils CSS.
 # You can optionally override in configuration for specific repositories.
-slipper.DEFAULT.stylesheet_url = /slipper-dev/tip/doc/slipper.css
+slipper.DEFAULT.stylesheet_path = /doc/slipper.css
 
 # For each repository, choose a name, then specify both the location
 # of the repository on the local filesystem ('local_dir') and the base

File slipper/app.py

 
 from slipper.config import repo_map_from_config
 import slipper.directives
+from slipper.path import relative
 
 
 class Application(object):
                 # Not in cache, convert to HTML.
                 w = Writer()
                 settings = dict()
-                if config.stylesheet_url:
+                if config.stylesheet_path:
+                    # Find relative URL of stylesheet as compared to
+                    # file being rendered.
+                    abs_file_path = '/' + file_path
+                    rel_stylesheet_url = relative(
+                        abs_file_path, config.stylesheet_path)
                     settings.update(dict(
                         embed_stylesheet = False,
-                        stylesheet = config.stylesheet_url,
+                        stylesheet = rel_stylesheet_url,
                         stylesheet_path = None,
                         ))
                 data = publish_string(

File slipper/config.py

     default = RepositoryInfo(
         cache_dir = config.pop('slipper.DEFAULT.cache_dir', None),
         rst_extension = config.pop('slipper.DEFAULT.rst_extension', '.rst'),
-        stylesheet_url = config.pop('slipper.DEFAULT.stylesheet_url', None),
+        stylesheet_path = config.pop('slipper.DEFAULT.stylesheet_path', None),
         local_dir = None,
         hgweb_url = None,
         )

File slipper/path.py

+def relative(abs_from, abs_to):
+    """Return the relative path to reach `abs_to` from `abs_from`.
+    
+    Examples::
+    
+        >>> relative(u'/abc/def/ghi', u'/abc/def/jkl')
+        u'jkl'
+        
+        >>> relative(u'/abc/def/ghi', u'/abc/mno/pqr')
+        u'../mno/pqr'
+        
+        >>> relative(u'/abc/def/', u'/abc/mno/pqr')
+        u'../mno/pqr'
+        
+        >>> relative(u'/abc/def/ghi', u'/abc/mno/')
+        u'../mno/'
+    """
+    from_list = parts(abs_from)
+    to_list = parts(abs_to)
+    # Find the location where the two paths start to differ.
+    for i, (from_part, to_part) in enumerate(zip(from_list, to_list)):
+        if from_part != to_part:
+            break
+    # i is the point where the two paths diverge. Find the number of
+    # parent directories needed to work up to the point of divergence.
+    segments = [u'..'] * (len(from_list) - i - 1)
+    # Add the diverging part.
+    segments += to_list[i:]
+    if len(segments) == 0:
+        url = u'.'
+    else:
+        url = u'/'.join(segments)
+    return url
+        
+    
+def parts(path):
+    """Return the parts of the absolute path given."""
+    L = path.split(u'/')
+    while not L[0]:
+        L.pop(0)
+    return L