From b2aabdc02ba98111cad0229a1af645a49e0e5f97 Mon Sep 17 00:00:00 2001 From: Rogdham Date: Sun, 14 Apr 2013 09:48:20 +0100 Subject: [PATCH] Do not generate invalid filenames. Fixes #814. Turn invalid characters into underscores, remove leading dots and enforce a maximum length. Should be fine on main file systems used by Windows, Mac OS and Linux. Thanks to @Avaris for helping to clean my code. --- pelican/tools/pelican_import.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pelican/tools/pelican_import.py b/pelican/tools/pelican_import.py index 420c3fba..9e477c2c 100755 --- a/pelican/tools/pelican_import.py +++ b/pelican/tools/pelican_import.py @@ -376,6 +376,15 @@ def fields2pelican(fields, out_markup, output_path, dircat=False, strip_raw=Fals filename = os.path.basename(filename) + # Enforce filename restrictions for various filesystems at once; see + # http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words + # we do not need to filter words because an extension will be appended + filename = re.sub(r'[<>:"/\\|?*^% ]', '-', filename) # invalid chars + filename = filename.lstrip('.') # should not start with a dot + if not filename: + filename = '_' + filename = filename[:249] # allow for 5 extra characters + # option to put files in directories with categories names if dircat and (len(categories) > 0): catname = slugify(categories[0])