From 30079725606eadb2f8ea695db5f2e8f33cedac6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Thu, 25 Jun 2026 07:24:27 +0200 Subject: [PATCH] feat(mobi): allow to pass custom arguments to kindlegen --- doc/p/mobi.md | 1 + plugins-meta/index.json | 6 ++++++ pyglossary/plugins/ebook_mobi/__init__.py | 4 ++++ pyglossary/plugins/ebook_mobi/writer.py | 15 ++++++++++----- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/doc/p/mobi.md b/doc/p/mobi.md index 0924614a7..a0f976bb5 100644 --- a/doc/p/mobi.md +++ b/doc/p/mobi.md @@ -31,6 +31,7 @@ To update, modify plugins/mobi/__init__.py file, then run ./scripts/gen | css | | str | Path to css file | | cover_path | | str | Path to cover file | | kindlegen_path | | str | Path to kindlegen executable | +| kindlegen_args | `None` | list | Additional arguments forwarded to kindlegen | | file_size_approx | `271360` | int | Approximate size of each xhtml file (example: 200kb) | | hide_word_index | `False` | bool | Hide headword in tap-to-check interface | | spellcheck | `True` | bool | Enable wildcard search and spell correction during word lookup | diff --git a/plugins-meta/index.json b/plugins-meta/index.json index 93d1d9804..21e93e97c 100644 --- a/plugins-meta/index.json +++ b/plugins-meta/index.json @@ -852,6 +852,11 @@ "customValue": true, "comment": "Path to kindlegen executable" }, + "kindlegen_args": { + "class": "ListOption", + "type": "list", + "comment": "Additional arguments forwarded to kindlegen" + }, "compress": { "class": "BoolOption", "type": "bool", @@ -913,6 +918,7 @@ "css": "", "cover_path": "", "kindlegen_path": "", + "kindlegen_args": null, "file_size_approx": 271360, "hide_word_index": false, "spellcheck": true, diff --git a/pyglossary/plugins/ebook_mobi/__init__.py b/pyglossary/plugins/ebook_mobi/__init__.py index 8bbb03059..667a473ac 100644 --- a/pyglossary/plugins/ebook_mobi/__init__.py +++ b/pyglossary/plugins/ebook_mobi/__init__.py @@ -9,6 +9,7 @@ BoolOption, FileSizeOption, IntOption, + ListOption, StrOption, ) @@ -58,6 +59,9 @@ "kindlegen_path": StrOption( comment="Path to kindlegen executable", ), + "kindlegen_args": ListOption( + comment="Additional arguments forwarded to kindlegen", + ), "compress": BoolOption( disabled=True, comment="Enable compression", diff --git a/pyglossary/plugins/ebook_mobi/writer.py b/pyglossary/plugins/ebook_mobi/writer.py index 1d06bbeec..ccff9eeee 100644 --- a/pyglossary/plugins/ebook_mobi/writer.py +++ b/pyglossary/plugins/ebook_mobi/writer.py @@ -61,6 +61,7 @@ class Writer(EbookWriter): _compress: bool = False _keep: bool = False _kindlegen_path: str = "" + _kindlegen_args: list | None = None _file_size_approx: int = 271360 _hide_word_index: bool = False _spellcheck: bool = True @@ -296,15 +297,19 @@ def write(self) -> Generator[None, EntryType, None]: # name = self._glos.getInfo("name") log.info(f"Creating .mobi file with kindlegen, using {kindlegen_path!r}") direc, filename = split(filename) + + kindlegen_args = self._kindlegen_args or [] + kindlegen_args.extend(["-gen_ff_mobi7", "-dont_append_source"]) + if isDebug(): + kindlegen_args.append("-verbose") + kindlegen_args.extend(["-o", "content.mobi"]) + log.info(f"Effective kindlegen arguments: {' '.join(kindlegen_args)}") + cmd = [ kindlegen_path, join(filename, "OEBPS", "content.opf"), - "-gen_ff_mobi7", - "-dont_append_source", + *kindlegen_args, ] - if isDebug(): - cmd.append("-verbose") - cmd += ["-o", "content.mobi"] proc = subprocess.Popen( cmd, cwd=direc,