David Chambers avatar David Chambers committed b921469

Updated `post_urls` – `filepath` is no longer required to be an absolute path.

Comments (0)

Files changed (1)

 import markdown
 import pytz
 
-from django.conf import settings as project_settings
+from django.conf import settings
 from django.core.cache import cache
 from django.template import Context, loader
-from mango import settings
+import mango.settings
 from mango.settings import *
 
 block = [r'(?m)^(', r'(?=[ \n\r])[^\n\r]*(\r?\n|$))+']
 					meta[key] = value[0]
 
 	if meta.has_key('date') and meta.has_key('time'):
-		tz = pytz.timezone(project_settings.TIME_ZONE)
+		tz = pytz.timezone(settings.TIME_ZONE)
 		dt_format = ' '.join([MARKDOWN_DATE_FORMAT, MARKDOWN_TIME_FORMAT])
 		try:
 			meta['datetime'] = tz.localize(datetime.datetime.strptime(' '.join([meta['date'], meta['time']]), dt_format)).astimezone(pytz.utc)
 	"""
 	Returns a post's short and canonical URLs
 	
-	>>> path_to_posts = os.path.join(os.sep, *PATH_TO_POSTS.split('/'))
-	>>> setattr(settings, 'SHORT_URL_BASE', 'http://✪df.ws/')
+	>>> path_to_posts = os.path.join(*PATH_TO_POSTS.split('/'))
+	>>> 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/')
-	>>> setattr(settings, 'SHORT_URL_BASE', '')
+	>>> setattr(mango.settings, 'SHORT_URL_BASE', '')
 	>>> post_urls(os.path.join(path_to_posts, '01=>my-first-post.text'))
 	(u'/1/', u'/my-first-post/')
 	>>> post_urls(os.path.join(path_to_posts,
 	...         'js=>javascript', 'libs=>libraries', 'prototype.js', '$.text'))
 	(u'/js/libs/prototype.js/$/', u'/javascript/libraries/prototype.js/$/')
 	"""
-	canon_fragments = ['', '']
-	alias_fragments = ['', '']
+	canon_fragments = [u'']
+	alias_fragments = [u'']
 
-	head, tail = os.path.split(filepath)
+	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(1, canon)
-		alias_fragments.insert(1, match.group('alias') or canon)
+		canon_fragments.insert(0, canon)
+		alias_fragments.insert(0, 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(1, canon)
-			alias_fragments.insert(1, match.group('alias') or canon)
+			canon_fragments.insert(0, canon)
+			alias_fragments.insert(0, match.group('alias') or canon)
 
-	short_url_base = settings.SHORT_URL_BASE.decode('utf-8') or '/'
-	short_url = u'/'.join(alias_fragments).replace(PATH_TO_POSTS, short_url_base, 1)
-	canon_url = u'/'.join(canon_fragments).replace(PATH_TO_POSTS, '/', 1)
+	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)
+	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)
 
 	return (short_url, canon_url)
 
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.