Precompile the path separator regex in order to improve performance, readability and amintainability

This commit is contained in:
fri 2015-01-03 13:36:58 +01:00
commit 3ee823173c

View file

@ -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']: