Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat(plugins): add FIGlet plugin #12

Merged
merged 20 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __pycache__/
*coverage*
.vscode/
dist/
*bat
39 changes: 39 additions & 0 deletions doteki/plugins/figlet.py
Original file line number Diff line number Diff line change
@@ -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)
welpo marked this conversation as resolved.
Show resolved Hide resolved
result = "<pre>" + result + "</pre>"
return str(result)


def validate_settings(settings: dict[str, Any]) -> bool:

text = settings.get("ascii_text")
bcignasi marked this conversation as resolved.
Show resolved Hide resolved
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)
bcignasi marked this conversation as resolved.
Show resolved Hide resolved

except pyfiglet.FontNotFound:
errors.append("Invalid font for the Figlet plugin")

if errors:
for error in errors:
logging.error(error)
return False

return True
16 changes: 14 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ 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]
# Plugin dependencies.
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"]
Expand Down
63 changes: 63 additions & 0 deletions tests/plugins/test_figlet.py
Original file line number Diff line number Diff line change
@@ -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 = "<pre>" + expected + "</pre>"
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():
bcignasi marked this conversation as resolved.
Show resolved Hide resolved
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
37 changes: 37 additions & 0 deletions website/docs/plugins/figlet.md
Original file line number Diff line number Diff line change
@@ -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.