From 3ee823173c1f8b115bcc43a1d6d77b96cf5e86b7 Mon Sep 17 00:00:00 2001 From: fri Date: Sat, 3 Jan 2015 13:36:58 +0100 Subject: [PATCH] Precompile the path separator regex in order to improve performance, readability and amintainability --- pelican/contents.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index b885091e..77c87970 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -23,6 +23,7 @@ from pelican.utils import (slugify, truncate_html_words, memoized, strftime, from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA logger = logging.getLogger(__name__) +path_sep_regexp = re.compile(r'[\\/]') class Content(object): @@ -230,13 +231,13 @@ class Content(object): # In order to make everything work in a platform-independent way, let's accept both characters # as valid path separators for {filename} links, and rebuild the correct path internally by # using os.path.join, so that the resulting path will have the correct separator for this platform. - if re.match(r'[\\/]', path): + if path_sep_regexp.match(path): # Path begins with the separator character, so it is relative to the root of the website - path = os.path.join(*re.split(r'[\\/]', path[1:])) + path = os.path.join(*path_sep_regexp.split(path[1:])) else: # relative to the source path of this content path = self.get_relative_source_path( - os.path.join(self.relative_dir, os.path.join(*re.split(r'[\\/]', path))) + os.path.join(self.relative_dir, os.path.join(*path_sep_regexp.split(path))) ) if path not in self._context['filenames']: