Commits

David Chambers committed be1372d

Changed post view so that /foo/ will display the converted version of "foo.text" if such a file exists in the posts directory, even if the posts directory contains a subdirectory named "foo".

  • Participants
  • Parent commits 74bf58e

Comments (0)

Files changed (1)

     is_short = False
     fragments = path.split('/')
     last_index = len(fragments) - 1
+
+    def match_fragment(dirnames=[], filenames=[]):
+        for name in dirnames or filenames:
+            match = re.match(RE['alias=>canon'], name if dirnames else os.path.splitext(name)[0])
+            if fragment in match.groupdict().values():
+                return (True, os.path.join(filepath, name), is_short or fragment == match.group('alias'))
+        return (False, filepath, is_short)
+
     for index, fragment in enumerate(fragments):
         found = False
-        for entry in os.listdir(filepath):
-            name = entry
+        for dirpath, dirnames, filenames in os.walk(filepath):
             if index == last_index:
-                name, ext = os.path.splitext(entry)
-            match = re.match(RE['alias=>canon'], name)
-            if fragment in match.groupdict().values():
-                found = True
-                if fragment == match.group('alias'):
-                    is_short = True
-                filepath = os.path.join(filepath, entry)
+                found, filepath, is_short = match_fragment(filenames=filenames) # files take precedence
+                if found:
+                    break
+            found, filepath, is_short = match_fragment(dirnames=dirnames)
+            if found:
                 break
-        if not found:
-            raise Http404
+
+    if not found:
+        raise Http404
 
     short_urls, canon_urls = utils.post_urls(filepath)