diff --git a/ebook/__init__.py b/ebook/__init__.py index e064d1b..3b7bb0e 100644 --- a/ebook/__init__.py +++ b/ebook/__init__.py @@ -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, @@ -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 ) diff --git a/ebook/epub.py b/ebook/epub.py index 7a1bfcb..4315977 100644 --- a/ebook/epub.py +++ b/ebook/epub.py @@ -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. @@ -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) diff --git a/leech.py b/leech.py index 3bb728a..3b11abf 100755 --- a/leech.py +++ b/leech.py @@ -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: