mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
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.
This commit is contained in:
parent
a6167f64f1
commit
b2aabdc02b
1 changed files with 9 additions and 0 deletions
|
|
@ -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])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue