David Chambers avatar David Chambers committed 45693f5

Improved file path handling. `PATH_TO_POSTS` must now be in one of two formats: absolute, or relative to the project directory (e.g. "mango/examples"). Never include a trailing slash.

Comments (0)

Files changed (3)

settings/defaults.py

 
 SHORT_URL_BASE = '' # defaults to BASE_URL when set to the empty string
 
-PATH_TO_POSTS = './mango/examples/'
-PATH_TO_STATIC = './mango/static/'
+PATH_TO_POSTS = 'mango/examples'
+PATH_TO_STATIC = 'mango/static'
 
 SITE_TITLE = 'I Love Mangoes'
 
     f.close()
     return u
 
+def absolute_path_to_posts():
+    """
+    Returns the Unix style absolute path to the posts directory
+    """
+    path_to_posts = mango.settings.PATH_TO_POSTS
+    if not path_to_posts.startswith('/'):
+        path_to_this = os.path.split(__file__)[0] # strip /utils.py
+        project_path = os.path.split(path_to_this)[0] # strip /mango
+        path_to_posts = os.path.join(project_path, *path_to_posts.split(u'/'))
+        fragments = [u'']
+        head, tail = os.path.split(path_to_posts)
+        while tail:
+            fragments.insert(1, tail)
+            head, tail = os.path.split(head)
+        path_to_posts = u'/'.join(fragments)
+    return path_to_posts
+
 def post_urls(filepath):
     """
     Returns a post's short and canonical URLs
     
-    >>> path_to_posts = os.path.join(*PATH_TO_POSTS.split('/'))
+    >>> path_to_posts = mango.utils.absolute_path_to_posts()
     >>> setattr(mango.settings, 'SHORT_URL_BASE', 'http://✪df.ws/')
     >>> post_urls(os.path.join(path_to_posts, '01=>my-first-post.text'))
     (u'http://\u272adf.ws/1/', u'/my-first-post/')
     ...         'js=>javascript', 'libs=>libraries', 'prototype.js', '$.text'))
     (u'/js/libs/prototype.js/$/', u'/javascript/libraries/prototype.js/$/')
     """
-    canon_fragments = [u'']
-    alias_fragments = [u'']
+    canon_fragments = [u'', u'']
+    alias_fragments = [u'', u'']
 
     head, tail = os.path.split(os.path.abspath(filepath))
     match = re.match(RE['alias=>canon'], tail)
     if match:
         canon = os.path.splitext(match.group('canon'))[0] # strip extension
-        canon_fragments.insert(0, canon)
-        alias_fragments.insert(0, match.group('alias') or canon)
+        canon_fragments.insert(1, canon)
+        alias_fragments.insert(1, match.group('alias') or canon)
 
     while tail:
         head, tail = os.path.split(head)
         match = re.match(RE['alias=>canon'], tail)
         if match:
             canon = match.group('canon')
-            canon_fragments.insert(0, canon)
-            alias_fragments.insert(0, match.group('alias') or canon)
+            canon_fragments.insert(1, canon)
+            alias_fragments.insert(1, match.group('alias') or canon)
 
-    fragments = []
-    absolute_path = os.path.abspath(os.path.join(*PATH_TO_POSTS.split(u'/')))
-    head, tail = os.path.split(absolute_path)
-    while tail:
-        fragments.insert(0, tail)
-        head, tail = os.path.split(head)
-    absolute_path = u'/'.join(fragments)
+    path_to_posts = absolute_path_to_posts()
     short_url_base = mango.settings.SHORT_URL_BASE.decode('utf-8').rstrip(u'/')
-    short_url = u'/'.join(alias_fragments).replace(absolute_path, short_url_base, 1)
-    canon_url = u'/'.join(canon_fragments).replace(absolute_path, u'', 1)
+    short_url = u'/'.join(alias_fragments).replace(path_to_posts, short_url_base, 1)
+    canon_url = u'/'.join(canon_fragments).replace(path_to_posts, u'', 1)
 
     return (short_url, canon_url)
 
                     'type': 'post' if 'datetime' in context['meta'] else 'page',
                 }, **context_defaults(request))))
 
-    filepath = os.path.join(os.sep, *PATH_TO_POSTS.split('/'))
+    filepath = os.path.join(os.sep, *utils.absolute_path_to_posts().split('/'))
     is_short = False
     fragments = path.split('/')
     last_index = len(fragments) - 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.