Skip to content

Commit

Permalink
New config option: allow_spaces
Browse files Browse the repository at this point in the history
Determines whether spaces in filenames will be replaced with underscores
  • Loading branch information
kemayo committed Nov 30, 2024
1 parent 2f21280 commit e3c63bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions ebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def chapter_html(
return chapters


def generate_epub(story, cover_options={}, image_options=None, output_filename=None, output_dir=None, normalize=False):
def generate_epub(story, cover_options={}, image_options=None, output_filename=None, output_dir=None, normalize=False, allow_spaces=False):
if image_options is None:
image_options = {
'image_fetch': False,
Expand Down Expand Up @@ -225,5 +225,6 @@ def generate_epub(story, cover_options={}, image_options=None, output_filename=
contents=image.read(), filetype='image/png'),
],
metadata,
output_dir=output_dir
output_dir=output_dir,
allow_spaces=allow_spaces
)
9 changes: 5 additions & 4 deletions ebook/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EpubFile = namedtuple('EbookFile', 'path, contents, title, filetype', defaults=(False, False, "application/xhtml+xml"))


def sanitize_filename(s):
def sanitize_filename(s, allow_spaces=False):
"""Take a string and return a valid filename constructed from the string.
Uses a whitelist approach: any characters not present in valid_chars are
removed. Also spaces are replaced with underscores.
Expand All @@ -31,16 +31,17 @@ def sanitize_filename(s):
"""
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
filename = ''.join(c for c in s if c in valid_chars)
filename = filename.replace(' ', '_') # I don't like spaces in filenames.
if not allow_spaces:
filename = filename.replace(' ', '_') # I don't like spaces in filenames.
return filename


def make_epub(filename, files, meta, compress=True, output_dir=False):
def make_epub(filename, files, meta, compress=True, output_dir=False, allow_spaces=False):
unique_id = meta.get('unique_id', False)
if not unique_id:
unique_id = 'leech_book_' + str(uuid.uuid4())

filename = sanitize_filename(filename)
filename = sanitize_filename(filename, allow_spaces)
if output_dir:
filename = os.path.join(output_dir, filename)
epub = zipfile.ZipFile(filename, 'w', compression=compress and zipfile.ZIP_DEFLATED or zipfile.ZIP_STORED)
Expand Down
3 changes: 2 additions & 1 deletion leech.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def download(urls, site_options, cache, verbose, normalize, output_dir, **other_
'always_convert_images': options.get('always_convert_images', False)
},
normalize=normalize,
output_dir=output_dir or options.get('output_dir', os.getcwd())
output_dir=output_dir or options.get('output_dir', os.getcwd()),
allow_spaces=options.get('allow_spaces', False)
)
logger.info("File created: " + filename)
else:
Expand Down

0 comments on commit e3c63bc

Please sign in to comment.