From f1eee98bb5144efc1aa242bad0e5676e12d882a5 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 17 Sep 2023 15:12:36 +0200 Subject: Test formatting --- bin/test | 4 ++++ cli/new/command.py | 4 ++-- cli/new/format.py | 20 +++++++++++--------- cli/new/test_format.py | 25 +++++++++++++++++++++++++ flake.nix | 1 + 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100755 bin/test create mode 100644 cli/new/test_format.py diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..cd6c0de --- /dev/null +++ b/bin/test @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname $0)/.." +python -m pytest diff --git a/cli/new/command.py b/cli/new/command.py index 9f5e5dc..dae0be7 100644 --- a/cli/new/command.py +++ b/cli/new/command.py @@ -42,9 +42,9 @@ def run(books_library, book_source=None): metadata = f"""title = "{title}" subtitle = "{subtitle}" - authors = {format.format_list(authors)} + authors = {format.list(authors)} authorsSort = "{author_sort}" - genres = {format.format_list(genres)} + genres = {format.list(genres)} date = {year} summary = \"\"\" {summary} diff --git a/cli/new/format.py b/cli/new/format.py index c004f82..7f66f44 100644 --- a/cli/new/format.py +++ b/cli/new/format.py @@ -2,15 +2,15 @@ import pathlib import re import unicodedata -def format_list(xs): - return '[ ' + ', '.join([f'"{x}"' for x in xs]) + ' ]' +def list(xs): + return '[' + ', '.join([f'"{x}"' for x in xs]) + ']' def path_part(name): simplified = ''.join([alnum_or_space(c) for c in unaccent(name.lower())]) return '-'.join(simplified.split()) def unaccent(s): - return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') + return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') def alnum_or_space(c): if c.isalnum(): @@ -23,9 +23,9 @@ def extension(path): def cleanup_text(s, lang): s = re.sub('\'', '’', s) - s = re.sub('\.\.\.', '…', s) - s = re.sub('\. \. \.', '…', s) - s = cleanup_quotes(s, lang) + s = re.sub(r'\.\.\.', '…', s) + s = re.sub(r'\. \. \.', '…', s) + s = cleanup_double_quotes(s, lang) if lang == 'fr': s = re.sub('“', '«', s) @@ -36,7 +36,10 @@ def cleanup_text(s, lang): s = re.sub('« ', '« ', s) # Add missing insecable spaces - s = re.sub(r'([^ ])([:?\!»])', r'\1 \2', s) + s = re.sub(r'([^ ]):', r'\1 :', s) + s = re.sub(r'([^ ])\?', r'\1 ?', s) + s = re.sub(r'([^ ])\!', r'\1 !', s) + s = re.sub(r'([^ ])»', r'\1 »', s) s = re.sub(r'«([^ ])', r'« \1', s) elif lang == 'en': @@ -45,7 +48,7 @@ def cleanup_text(s, lang): return s -def cleanup_quotes(s, lang): +def cleanup_double_quotes(s, lang): res = '' quoted = False for c in s: @@ -65,4 +68,3 @@ def cleanup_quotes(s, lang): else: res += c return res - diff --git a/cli/new/test_format.py b/cli/new/test_format.py new file mode 100644 index 0000000..d6269d0 --- /dev/null +++ b/cli/new/test_format.py @@ -0,0 +1,25 @@ +import cli.new.format as format + +def test_list(): + assert format.list([]) == '[]' + assert format.list(['a', 'b', 'c']) == '["a", "b", "c"]' + +def test_unaccent(): + assert format.unaccent('AuieTsrn') == 'AuieTsrn' + assert format.unaccent('âàéèêëîïôù') == 'aaeeeeiiou' + assert format.unaccent('ÂÀÉÈÊËÎÏÔÙ') == 'AAEEEEIIOU' + +def test_path_part(): + assert format.path_part('L’Homme à la béquille') == 'l-homme-a-la-bequille' + +def test_extension(): + assert format.extension('https://website.de/file.webp') == '.webp' + assert format.extension('/home/toto/extension-test/auie.notepad') == '.notepad' + +def test_cleaneup_quotes(): + assert format.cleanup_double_quotes('Bonjour, "ceci" où “cela”.', 'fr') == 'Bonjour, «ceci» où “cela”.' + assert format.cleanup_double_quotes('Hello, "this" or «that».', 'en') == 'Hello, “this” or «that».' + +def test_cleaneup_text(): + assert format.cleanup_text('l\'"est": ici... Là? OK! Yes !', 'fr') == 'l’« est » : ici… Là ? OK ! Yes !' + assert format.cleanup_text('Is it "ok" or «not»?', 'en') == 'Is it “ok” or “not”?' diff --git a/flake.nix b/flake.nix index 97d9261..7d00ae7 100644 --- a/flake.nix +++ b/flake.nix @@ -24,6 +24,7 @@ # CLI python311 python311Packages.pillow + python311Packages.pytest python311Packages.requests ebook-convert ]; -- cgit v1.2.3