From 65afa9e49d184db2a33d0f63fd7be5afd747facb Mon Sep 17 00:00:00 2001 From: bcignasi Date: Wed, 14 Feb 2024 21:13:58 +0100 Subject: [PATCH 01/20] Adds figlet plugin feature --- .gitignore | 1 + doteki/plugins/figlet.py | 39 +++++++++++++++++++++ tests/plugins/test_figlet.py | 63 ++++++++++++++++++++++++++++++++++ website/docs/plugins/figlet.md | 37 ++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 doteki/plugins/figlet.py create mode 100644 tests/plugins/test_figlet.py create mode 100644 website/docs/plugins/figlet.md diff --git a/.gitignore b/.gitignore index 3891c7c..376b3c5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *coverage* .vscode/ dist/ +*bat diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py new file mode 100644 index 0000000..66ba4c2 --- /dev/null +++ b/doteki/plugins/figlet.py @@ -0,0 +1,39 @@ +import logging +import pyfiglet # type: ignore +from typing import Any + + +def run(settings: dict[str, Any]) -> str | None: + + if not validate_settings(settings): + return None + text = settings.get("ascii_text") + font = settings.get("font", "standard") + result = pyfiglet.figlet_format(text, font) + result = "
" + result + "
" + return str(result) + + +def validate_settings(settings: dict[str, Any]) -> bool: + + text = settings.get("ascii_text") + font = settings.get("font", "standard") + + errors = [] + + # Check required setting. + if "ascii_text" not in settings: + errors.append("No text provided for the Figlet plugin") + + try: + pyfiglet.figlet_format("test", font) + + except pyfiglet.FontNotFound: + errors.append("Invalid font for the Figlet plugin") + + if errors: + for error in errors: + logging.error(error) + return False + + return True diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py new file mode 100644 index 0000000..df24579 --- /dev/null +++ b/tests/plugins/test_figlet.py @@ -0,0 +1,63 @@ +import logging +from doteki.plugins.figlet import run + + +def test_prototype(): + settings = {"ascii_text": "hola"} + expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" + expected = "
" + expected + "
" + result = run(settings) + expected = expected.replace(" ", "S").replace("\n","r") + result = result.replace(" ", "S").replace("\n","r") + + print(repr(expected)) + print(repr(result)) + assert result == expected + + +def test_proto_2(): + import pyfiglet + + expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" + result = pyfiglet.figlet_format("hola") + + #expected = expected.replace(" ", "S").replace("\n","r") + #result = result.replace(" ", "S").replace("\n","r") + + #print(repr(expected)) + #print(repr(result)) + assert result == expected + +#If no parameters, result is none +#Test is superseeded by test_empty_text +"""def test_empty_settings(caplog): + + settings = {} + + with caplog.at_level(logging.ERROR): + result = run(settings) + assert result is None + assert "No settings provided for the Figlet plugin" in caplog.text""" + +#If no text, result is none + +def test_empty_text(caplog): + + settings = {"font": "standard"} + + with caplog.at_level(logging.ERROR): + result = run(settings) + assert result is None + assert "No text provided for the Figlet plugin" in caplog.text + + +#If invalid font, result is none + +def test_invalid_font(caplog): + + settings = {"ascii_text": "hola", "font": "invalid_font"} + + with caplog.at_level(logging.ERROR): + result = run(settings) + assert result is None + assert "Invalid font for the Figlet plugin" in caplog.text diff --git a/website/docs/plugins/figlet.md b/website/docs/plugins/figlet.md new file mode 100644 index 0000000..712f7b1 --- /dev/null +++ b/website/docs/plugins/figlet.md @@ -0,0 +1,37 @@ +# Figlet Plugin + +Display text with customizable ASCII art using Figlet fonts. If no font is provided, it defaults to the standard output. Useful for adding decorative displays in your documents or applications. + +## Configuration + +The Figlet plugin can be configured with the following parameters: + +- `text`: Text to be rendered in ASCII font. +- `font`: Specify the Figlet font to use for rendering the text. Default is standard ASCII font. + +## Usage + +Using a custom format and prepending and appending some text: + +```toml title="doteki.toml" +[sections.ascii_art] +plugin = "figlet" +ascii_text = "text" +font = "standard" +``` + +dōteki would generate: + +```md + _ _ +| |_ _____ _| |_ +| __/ _ \ \/ / __| +| || __/> <| |_ + \__\___/_/\_\\__| +``` + +## Frequently Asked Questions + +### What are the available fonts? + +Check the [FIGlet official site](http://www.figlet.org/#format-codes) to see several samples of available fonts. From ce39bb035219feab19d7a9c6a10c31d51baff750 Mon Sep 17 00:00:00 2001 From: bcignasi Date: Thu, 15 Feb 2024 20:57:59 +0100 Subject: [PATCH 02/20] added pyfiglet dependencies --- poetry.lock | 13 ++++++++++++- pyproject.toml | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index d2fc425..8970d7c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -388,6 +388,17 @@ files = [ dev = ["pre-commit", "tox"] testing = ["pytest", "pytest-benchmark"] +[[package]] +name = "pyfiglet" +version = "1.0.2" +description = "Pure-python FIGlet implementation" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pyfiglet-1.0.2-py3-none-any.whl", hash = "sha256:889b351d79c99e50a3f619c8f8e6ffdb27fd8c939fc43ecbd7559bd57d5f93ea"}, + {file = "pyfiglet-1.0.2.tar.gz", hash = "sha256:758788018ab8faaddc0984e1ea05ff330d3c64be663c513cc1f105f6a3066dab"}, +] + [[package]] name = "pytest" version = "8.0.0" @@ -506,4 +517,4 @@ lastfm = ["requests"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "feb00fe505fc34afb577ba582ed9ca0b6a16aa9783ff188e66355bd5dda8e2a6" +content-hash = "1a00ee1dce291eb658c7d1859102b8cdac86d779df60991616677c82893eab3c" diff --git a/pyproject.toml b/pyproject.toml index d27d70a..c3e592d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ python = "^3.11" # Optional dependencies for plugins. feedparser = { version = "^6.0", optional = true } requests = { version = "^2.28", optional = true } +pyfiglet = { version = "^1.0.2", optional = true} [tool.poetry.extras] @@ -36,9 +37,11 @@ requests = { version = "^2.28", optional = true } all = [ # This enables `pip install doteki[all]`. Should contain all dependencies below. "requests", "feedparser", + "pyfiglet", ] lastfm = ["requests"] feed = ["requests", "feedparser"] +figlet = ["pyfiglet"] [build-system] requires = ["poetry-core"] From 791699218c8eca1505f1cf6f36f67a31899d17cc Mon Sep 17 00:00:00 2001 From: welpo Date: Thu, 15 Feb 2024 21:35:41 +0100 Subject: [PATCH 03/20] update poetry.lock --- poetry.lock | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index 8970d7c..52ce16b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -392,7 +392,7 @@ testing = ["pytest", "pytest-benchmark"] name = "pyfiglet" version = "1.0.2" description = "Pure-python FIGlet implementation" -optional = false +optional = true python-versions = ">=3.9" files = [ {file = "pyfiglet-1.0.2-py3-none-any.whl", hash = "sha256:889b351d79c99e50a3f619c8f8e6ffdb27fd8c939fc43ecbd7559bd57d5f93ea"}, @@ -510,11 +510,12 @@ socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] zstd = ["zstandard (>=0.18.0)"] [extras] -all = ["feedparser", "requests"] +all = ["feedparser", "pyfiglet", "requests"] feed = ["feedparser", "requests"] +figlet = ["pyfiglet"] lastfm = ["requests"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "1a00ee1dce291eb658c7d1859102b8cdac86d779df60991616677c82893eab3c" +content-hash = "b547f085367a4a065736dff67fa9fc6f442f56a4aa81089c1d340e1bc04759fc" From 7bb05d81b51b783ccf82dc255c3d31d17152df45 Mon Sep 17 00:00:00 2001 From: welpo Date: Sat, 17 Feb 2024 01:15:58 +0100 Subject: [PATCH 04/20] black formatting --- tests/plugins/test_figlet.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index df24579..a190d21 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -3,12 +3,12 @@ def test_prototype(): - settings = {"ascii_text": "hola"} + settings = {"ascii_text": "hola"} expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" expected = "
" + expected + "
" result = run(settings) - expected = expected.replace(" ", "S").replace("\n","r") - result = result.replace(" ", "S").replace("\n","r") + expected = expected.replace(" ", "S").replace("\n", "r") + result = result.replace(" ", "S").replace("\n", "r") print(repr(expected)) print(repr(result)) @@ -17,19 +17,20 @@ def test_prototype(): def test_proto_2(): import pyfiglet - + expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" result = pyfiglet.figlet_format("hola") - #expected = expected.replace(" ", "S").replace("\n","r") - #result = result.replace(" ", "S").replace("\n","r") + # expected = expected.replace(" ", "S").replace("\n","r") + # result = result.replace(" ", "S").replace("\n","r") - #print(repr(expected)) - #print(repr(result)) + # print(repr(expected)) + # print(repr(result)) assert result == expected -#If no parameters, result is none -#Test is superseeded by test_empty_text + +# If no parameters, result is none +# Test is superseeded by test_empty_text """def test_empty_settings(caplog): settings = {} @@ -39,20 +40,19 @@ def test_proto_2(): assert result is None assert "No settings provided for the Figlet plugin" in caplog.text""" -#If no text, result is none +# If no text, result is none def test_empty_text(caplog): settings = {"font": "standard"} - + with caplog.at_level(logging.ERROR): result = run(settings) assert result is None assert "No text provided for the Figlet plugin" in caplog.text -#If invalid font, result is none - +# If invalid font, result is none def test_invalid_font(caplog): settings = {"ascii_text": "hola", "font": "invalid_font"} From cb74978a64326950040ef5b7e41d54dad0506c75 Mon Sep 17 00:00:00 2001 From: welpo Date: Sat, 17 Feb 2024 01:31:42 +0100 Subject: [PATCH 05/20] add failing test to fix --- tests/plugins/test_figlet.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index a190d21..31a655b 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -61,3 +61,16 @@ def test_invalid_font(caplog): result = run(settings) assert result is None assert "Invalid font for the Figlet plugin" in caplog.text + + +def test_int_text(): + settings = {"ascii_text": 42, "font": "standard"} + + expected = r""" _ _ ____ +| || ||___ \ +| || |_ __) | +|__ _/ __/ + |_||_____|""" + result = run(settings) + + assert expected in str(result) From da82e5ae833452bc55af7f4f01c697eaa23ead80 Mon Sep 17 00:00:00 2001 From: welpo Date: Sat, 17 Feb 2024 01:32:04 +0100 Subject: [PATCH 06/20] simplify test --- tests/plugins/test_figlet.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index 31a655b..e4a2afc 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -4,15 +4,13 @@ def test_prototype(): settings = {"ascii_text": "hola"} - expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" - expected = "
" + expected + "
" + expected = r""" _ _ +| |__ ___ | | __ _ +| '_ \ / _ \| |/ _` | +| | | | (_) | | (_| | +|_| |_|\___/|_|\__,_|""" result = run(settings) - expected = expected.replace(" ", "S").replace("\n", "r") - result = result.replace(" ", "S").replace("\n", "r") - - print(repr(expected)) - print(repr(result)) - assert result == expected + assert expected in str(result) def test_proto_2(): From ea4cd3702ceff5214924a4c834ab2b7a9c42a4e1 Mon Sep 17 00:00:00 2001 From: welpo Date: Sat, 17 Feb 2024 01:41:24 +0100 Subject: [PATCH 07/20] use code block formatting to avoid syntax highlighting --- doteki/plugins/figlet.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 66ba4c2..5c19734 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -10,7 +10,9 @@ def run(settings: dict[str, Any]) -> str | None: text = settings.get("ascii_text") font = settings.get("font", "standard") result = pyfiglet.figlet_format(text, font) - result = "
" + result + "
" + pre_text = "```text\n" + post_text = "\n```" + result = pre_text + result.rstrip() + post_text return str(result) From f99797f51802aa4b1f0414031e39e4ff35c9ec27 Mon Sep 17 00:00:00 2001 From: welpo Date: Sat, 17 Feb 2024 01:44:22 +0100 Subject: [PATCH 08/20] capitalise to FIGlet, change example and docs link --- doteki/plugins/figlet.py | 4 ++-- tests/plugins/test_figlet.py | 6 +++--- website/docs/plugins/figlet.md | 32 +++++++++++++++++--------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 5c19734..12820bd 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -25,13 +25,13 @@ def validate_settings(settings: dict[str, Any]) -> bool: # Check required setting. if "ascii_text" not in settings: - errors.append("No text provided for the Figlet plugin") + errors.append("No text provided for the FIGlet plugin") try: pyfiglet.figlet_format("test", font) except pyfiglet.FontNotFound: - errors.append("Invalid font for the Figlet plugin") + errors.append("Invalid font for the FIGlet plugin") if errors: for error in errors: diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index e4a2afc..7536052 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -36,7 +36,7 @@ def test_proto_2(): with caplog.at_level(logging.ERROR): result = run(settings) assert result is None - assert "No settings provided for the Figlet plugin" in caplog.text""" + assert "No settings provided for the FIGlet plugin" in caplog.text""" # If no text, result is none @@ -47,7 +47,7 @@ def test_empty_text(caplog): with caplog.at_level(logging.ERROR): result = run(settings) assert result is None - assert "No text provided for the Figlet plugin" in caplog.text + assert "No text provided for the FIGlet plugin" in caplog.text # If invalid font, result is none @@ -58,7 +58,7 @@ def test_invalid_font(caplog): with caplog.at_level(logging.ERROR): result = run(settings) assert result is None - assert "Invalid font for the Figlet plugin" in caplog.text + assert "Invalid font for the FIGlet plugin" in caplog.text def test_int_text(): diff --git a/website/docs/plugins/figlet.md b/website/docs/plugins/figlet.md index 712f7b1..044ba63 100644 --- a/website/docs/plugins/figlet.md +++ b/website/docs/plugins/figlet.md @@ -1,37 +1,39 @@ -# Figlet Plugin +# FIGlet Plugin -Display text with customizable ASCII art using Figlet fonts. If no font is provided, it defaults to the standard output. Useful for adding decorative displays in your documents or applications. +Display text with customizable ASCII art using FIGlet fonts. If no font is provided, it defaults to the standard output. Useful for adding decorative displays in your documents or applications. ## Configuration -The Figlet plugin can be configured with the following parameters: +The FIGlet plugin can be configured with the following parameters: - `text`: Text to be rendered in ASCII font. -- `font`: Specify the Figlet font to use for rendering the text. Default is standard ASCII font. +- `font`: Specify the FIGlet font to use for rendering the text. Default is standard ASCII font. ## Usage -Using a custom format and prepending and appending some text: +Here's an example with a custom font: ```toml title="doteki.toml" [sections.ascii_art] plugin = "figlet" ascii_text = "text" -font = "standard" +font = "larry3d" ``` -dōteki would generate: +This configuration will render the following: -```md - _ _ -| |_ _____ _| |_ -| __/ _ \ \/ / __| -| || __/> <| |_ - \__\___/_/\_\\__| +```text + __ __ +/\ \__ /\ \__ +\ \ ,_\ __ ____\ \ ,_\ + \ \ \/ /'__`\ /',__\\ \ \/ + \ \ \_/\ __//\__, `\\ \ \_ + \ \__\ \____\/\____/ \ \__\ + \/__/\/____/\/___/ \/__/ ``` ## Frequently Asked Questions -### What are the available fonts? +### Where can I find a list of available fonts? -Check the [FIGlet official site](http://www.figlet.org/#format-codes) to see several samples of available fonts. +Check the [FIGlet official site](http://www.figlet.org/examples.html) to explore the available fonts. From 90b3c6cb952e0df8ab217b154d40ff39cad9238c Mon Sep 17 00:00:00 2001 From: bcignasi Date: Sun, 18 Feb 2024 22:09:01 +0100 Subject: [PATCH 09/20] Allow numbers to ascii text --- doteki/plugins/figlet.py | 1 + 1 file changed, 1 insertion(+) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 12820bd..1c68689 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -8,6 +8,7 @@ def run(settings: dict[str, Any]) -> str | None: if not validate_settings(settings): return None text = settings.get("ascii_text") + text = str(text) ## In case is int number font = settings.get("font", "standard") result = pyfiglet.figlet_format(text, font) pre_text = "```text\n" From 61071f299a3865c959ac16285155c360358f2334 Mon Sep 17 00:00:00 2001 From: bcignasi Date: Sun, 18 Feb 2024 22:29:47 +0100 Subject: [PATCH 10/20] change try-except to the run function. --- doteki/plugins/figlet.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 1c68689..99f1a20 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -10,7 +10,15 @@ def run(settings: dict[str, Any]) -> str | None: text = settings.get("ascii_text") text = str(text) ## In case is int number font = settings.get("font", "standard") - result = pyfiglet.figlet_format(text, font) + result = "" + + try: + result = pyfiglet.figlet_format(text, font) + + except pyfiglet.FontNotFound: + logging.error("Invalid font for the FIGlet plugin") + return None + pre_text = "```text\n" post_text = "\n```" result = pre_text + result.rstrip() + post_text @@ -28,12 +36,6 @@ def validate_settings(settings: dict[str, Any]) -> bool: if "ascii_text" not in settings: errors.append("No text provided for the FIGlet plugin") - try: - pyfiglet.figlet_format("test", font) - - except pyfiglet.FontNotFound: - errors.append("Invalid font for the FIGlet plugin") - if errors: for error in errors: logging.error(error) From 954a2f9d058d72024c49d1000a8eb96b89fbf86b Mon Sep 17 00:00:00 2001 From: bcignasi Date: Sun, 18 Feb 2024 22:37:58 +0100 Subject: [PATCH 11/20] Removed test_proto2 --- tests/plugins/test_figlet.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index 7536052..7993a3d 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -13,20 +13,6 @@ def test_prototype(): assert expected in str(result) -def test_proto_2(): - import pyfiglet - - expected = """ _ _ \n| |__ ___ | | __ _ \n| '_ \\ / _ \\| |/ _` |\n| | | | (_) | | (_| |\n|_| |_|\\___/|_|\\__,_|\n \n""" - result = pyfiglet.figlet_format("hola") - - # expected = expected.replace(" ", "S").replace("\n","r") - # result = result.replace(" ", "S").replace("\n","r") - - # print(repr(expected)) - # print(repr(result)) - assert result == expected - - # If no parameters, result is none # Test is superseeded by test_empty_text """def test_empty_settings(caplog): From dfc5bdbbb178d39224b7568e18a535496cc590b5 Mon Sep 17 00:00:00 2001 From: bcignasi Date: Sun, 18 Feb 2024 22:42:54 +0100 Subject: [PATCH 12/20] Removed unused variable from validate_settings --- doteki/plugins/figlet.py | 1 - 1 file changed, 1 deletion(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 99f1a20..d674c87 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -27,7 +27,6 @@ def run(settings: dict[str, Any]) -> str | None: def validate_settings(settings: dict[str, Any]) -> bool: - text = settings.get("ascii_text") font = settings.get("font", "standard") errors = [] From 8b3bdfa339a153e3a0f0e42efce6a9d5e93d8792 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 22:55:26 +0100 Subject: [PATCH 13/20] simplify code --- doteki/plugins/figlet.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index d674c87..c580f00 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -4,17 +4,14 @@ def run(settings: dict[str, Any]) -> str | None: - if not validate_settings(settings): return None - text = settings.get("ascii_text") - text = str(text) ## In case is int number + + text = str(settings.get("ascii_text")) font = settings.get("font", "standard") - result = "" try: result = pyfiglet.figlet_format(text, font) - except pyfiglet.FontNotFound: logging.error("Invalid font for the FIGlet plugin") return None @@ -26,18 +23,9 @@ def run(settings: dict[str, Any]) -> str | None: def validate_settings(settings: dict[str, Any]) -> bool: - - font = settings.get("font", "standard") - - errors = [] - # Check required setting. if "ascii_text" not in settings: - errors.append("No text provided for the FIGlet plugin") - - if errors: - for error in errors: - logging.error(error) + logging.error("No text provided for the FIGlet plugin") return False return True From 84965e6bc560603e9fcbe5dfa691aebf9f29b740 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:13:22 +0100 Subject: [PATCH 14/20] clean up tests --- tests/plugins/test_figlet.py | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index 7993a3d..95604f1 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -1,8 +1,8 @@ import logging -from doteki.plugins.figlet import run +from doteki.plugins.figlet import run, trim_leading_and_trailing_empty_lines -def test_prototype(): +def test_default_font(): settings = {"ascii_text": "hola"} expected = r""" _ _ | |__ ___ | | __ _ @@ -13,23 +13,8 @@ def test_prototype(): assert expected in str(result) -# If no parameters, result is none -# Test is superseeded by test_empty_text -"""def test_empty_settings(caplog): - - settings = {} - - with caplog.at_level(logging.ERROR): - result = run(settings) - assert result is None - assert "No settings provided for the FIGlet plugin" in caplog.text""" - - -# If no text, result is none def test_empty_text(caplog): - settings = {"font": "standard"} - with caplog.at_level(logging.ERROR): result = run(settings) assert result is None @@ -38,9 +23,7 @@ def test_empty_text(caplog): # If invalid font, result is none def test_invalid_font(caplog): - settings = {"ascii_text": "hola", "font": "invalid_font"} - with caplog.at_level(logging.ERROR): result = run(settings) assert result is None @@ -49,12 +32,10 @@ def test_invalid_font(caplog): def test_int_text(): settings = {"ascii_text": 42, "font": "standard"} - expected = r""" _ _ ____ | || ||___ \ | || |_ __) | |__ _/ __/ |_||_____|""" result = run(settings) - assert expected in str(result) From d47f3213eb7928ff9e041fe3ed1d08d6606ab1a6 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:14:25 +0100 Subject: [PATCH 15/20] trim empty leading/trailing lines --- doteki/plugins/figlet.py | 16 ++++++++++++++++ tests/plugins/test_figlet.py | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index c580f00..7c81b2e 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -16,6 +16,10 @@ def run(settings: dict[str, Any]) -> str | None: logging.error("Invalid font for the FIGlet plugin") return None + result_lines = result.splitlines() + trimmed_result_lines = trim_leading_and_trailing_empty_lines(result_lines) + result = "\n".join(trimmed_result_lines) + pre_text = "```text\n" post_text = "\n```" result = pre_text + result.rstrip() + post_text @@ -29,3 +33,15 @@ def validate_settings(settings: dict[str, Any]) -> bool: return False return True + + +def trim_leading_and_trailing_empty_lines(lines: list[str]) -> list[str]: + # Leading lines. + while lines and lines[0].strip() == "": + lines.pop(0) + + # Trailing lines. + while lines and lines[-1].strip() == "": + lines.pop() + + return lines diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index 95604f1..07b7dce 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -1,4 +1,5 @@ import logging +import pytest from doteki.plugins.figlet import run, trim_leading_and_trailing_empty_lines @@ -39,3 +40,22 @@ def test_int_text(): |_||_____|""" result = run(settings) assert expected in str(result) + + +@pytest.mark.parametrize( + "lines, expected", + [ + # Removes leading and trailing empty lines. + (["", " ", "Hello", "World", "", " "], ["Hello", "World"]), + # Do not remove empty lines in the middle. + (["Hello", "", " ", "World"], ["Hello", "", " ", "World"]), + # Leave non-empty lines as is. + (["Hello", "World"], ["Hello", "World"]), + # If there are only empty lines, return an empty list. + (["", " ", " "], []), + # If no lines are provided, return an empty list. + ([], []), + ], +) +def test_trim_leading_and_trailing_empty_lines(lines, expected): + assert trim_leading_and_trailing_empty_lines(lines) == expected From 1d89f787ea5cbc46d0ccc32cf04e6b42e3e8b520 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:16:49 +0100 Subject: [PATCH 16/20] restore gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 376b3c5..3891c7c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ __pycache__/ *coverage* .vscode/ dist/ -*bat From 55864c0a93feb88f3cadef6ee728ea8b8bdee723 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:19:34 +0100 Subject: [PATCH 17/20] remove self-explanatory comment --- doteki/plugins/figlet.py | 1 - 1 file changed, 1 deletion(-) diff --git a/doteki/plugins/figlet.py b/doteki/plugins/figlet.py index 7c81b2e..43bb856 100644 --- a/doteki/plugins/figlet.py +++ b/doteki/plugins/figlet.py @@ -27,7 +27,6 @@ def run(settings: dict[str, Any]) -> str | None: def validate_settings(settings: dict[str, Any]) -> bool: - # Check required setting. if "ascii_text" not in settings: logging.error("No text provided for the FIGlet plugin") return False From 31d06da6c56014c81aeb3715bab835a90a76107c Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:20:30 +0100 Subject: [PATCH 18/20] remove comment --- tests/plugins/test_figlet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/plugins/test_figlet.py b/tests/plugins/test_figlet.py index 07b7dce..4755f6c 100644 --- a/tests/plugins/test_figlet.py +++ b/tests/plugins/test_figlet.py @@ -22,8 +22,7 @@ def test_empty_text(caplog): assert "No text provided for the FIGlet plugin" in caplog.text -# If invalid font, result is none -def test_invalid_font(caplog): +def test_invalid_font_returns_none(caplog): settings = {"ascii_text": "hola", "font": "invalid_font"} with caplog.at_level(logging.ERROR): result = run(settings) From 6b20711d0c591c2e34d09bb5051dd9f1c86ce174 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:25:55 +0100 Subject: [PATCH 19/20] rephrase docs --- website/docs/plugins/figlet.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/plugins/figlet.md b/website/docs/plugins/figlet.md index 044ba63..a39aa96 100644 --- a/website/docs/plugins/figlet.md +++ b/website/docs/plugins/figlet.md @@ -1,17 +1,17 @@ # FIGlet Plugin -Display text with customizable ASCII art using FIGlet fonts. If no font is provided, it defaults to the standard output. Useful for adding decorative displays in your documents or applications. +Display text with customizable ASCII art using FIGlet fonts. ## Configuration The FIGlet plugin can be configured with the following parameters: - `text`: Text to be rendered in ASCII font. -- `font`: Specify the FIGlet font to use for rendering the text. Default is standard ASCII font. +- `font`: FIGlet font to use. Defaults to `standard`. ## Usage -Here's an example with a custom font: +Here's an example configuration: ```toml title="doteki.toml" [sections.ascii_art] @@ -20,7 +20,7 @@ ascii_text = "text" font = "larry3d" ``` -This configuration will render the following: +This will render the following: ```text __ __ From 45529f00543e199b210e4de33ec2abd2dc0d0e35 Mon Sep 17 00:00:00 2001 From: welpo Date: Sun, 18 Feb 2024 23:37:06 +0100 Subject: [PATCH 20/20] update example --- website/docs/plugins/figlet.md | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/website/docs/plugins/figlet.md b/website/docs/plugins/figlet.md index a39aa96..0294835 100644 --- a/website/docs/plugins/figlet.md +++ b/website/docs/plugins/figlet.md @@ -16,20 +16,24 @@ Here's an example configuration: ```toml title="doteki.toml" [sections.ascii_art] plugin = "figlet" -ascii_text = "text" -font = "larry3d" +ascii_text = "hello" +font = "isometric1" ``` This will render the following: ```text - __ __ -/\ \__ /\ \__ -\ \ ,_\ __ ____\ \ ,_\ - \ \ \/ /'__`\ /',__\\ \ \/ - \ \ \_/\ __//\__, `\\ \ \_ - \ \__\ \____\/\____/ \ \__\ - \/__/\/____/\/___/ \/__/ + ___ ___ ___ ___ ___ + /\__\ /\ \ /\__\ /\__\ /\ \ + /:/ / /::\ \ /:/ / /:/ / /::\ \ + /:/__/ /:/\:\ \ /:/ / /:/ / /:/\:\ \ + /::\ \ ___ /::\~\:\ \ /:/ / /:/ / /:/ \:\ \ + /:/\:\ /\__\ /:/\:\ \:\__\ /:/__/ /:/__/ /:/__/ \:\__\ + \/__\:\/:/ / \:\~\:\ \/__/ \:\ \ \:\ \ \:\ \ /:/ / + \::/ / \:\ \:\__\ \:\ \ \:\ \ \:\ /:/ / + /:/ / \:\ \/__/ \:\ \ \:\ \ \:\/:/ / + /:/ / \:\__\ \:\__\ \:\__\ \::/ / + \/__/ \/__/ \/__/ \/__/ \/__/ ``` ## Frequently Asked Questions