From 75974ae11957b8a65d607b1f00ad2fae41a6840b Mon Sep 17 00:00:00 2001 From: eneller Date: Sun, 6 Apr 2025 10:29:19 +0200 Subject: [PATCH] fix: slugify filenames --- pyproject.toml | 2 +- src/epub2go/convert.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fcd9368..2d943c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "epub2go" -version = "2.2.2" +version = "2.2.3" description = "EPUB converter using wget, pandoc and python glue" readme = "README.md" requires-python = ">=3.12" diff --git a/src/epub2go/convert.py b/src/epub2go/convert.py index b42ea43..e695e77 100644 --- a/src/epub2go/convert.py +++ b/src/epub2go/convert.py @@ -7,7 +7,7 @@ from tqdm import tqdm from pyfzf.pyfzf import FzfPrompt import click -import os, subprocess, shlex, logging +import os, subprocess, shlex, logging, re import importlib.resources as pkg_resources from dataclasses import dataclass from typing import List @@ -105,7 +105,7 @@ class GBConvert(): def create_epub(self, author, title, chapters, dir_output): #TODO --epub-cover-image #TODO toc if it isnt described by tags, e.g. https://www.projekt-gutenberg.org/adlersfe/maskenba/ - filename = f'{title} - {author}.epub' + filename = slugify(f'{title} - {author}.epub') command = f'''pandoc -f html -t epub \ -o "{filename}" \ --reference-location=section \ @@ -160,6 +160,13 @@ def get_all_books() -> List[Book]: books.append(book) return books +def slugify(value, replacement='_'): + value = re.sub(r'[<>:"/\\|?*\x00-\x1F]', replacement, value) + # Remove leading/trailing whitespace or dots + value = value.strip().strip(".") + # Optionally truncate to safe length (e.g. 255 chars for most filesystems) + return value[:255] or "untitled" + # run main cli @click.command() #TODO include images flag