Skip to main content

Anansi Tags

Apply simple BBCode-like tags to Python strings to get ANSI

No frills. No dependencies. Just strings!

ko-fi

Installation

pip install anansi-tags

Usage

This library aims to be as dead-simple as possible. All functions take a string and return a string. There are only 4 functions that you'll need to worry about as well, providing ways to convert back and forth between ANSI and the custom tags, and even to strip either ANSI or the tags from a string.

Simple Usage

Basic tags can be handled in a nice simple way:

from anansi import parse_tags

print(parse_tags('[bold]Hello world[/bold]'))

You can make colored text. Colored text ("foreground") is cancelled with /fg:

from anansi import parse_tags

print(parse_tags('[red]Hello world[/fg]'))

You can do background colors. Background colors are cancelled with /bg:

from anansi import parse_tags

print(parse_tags('[bg_white]Hello world[/bg]'))

You can combine any of these to make some super complex tags too: Note: You can cancel ALL formatting you have added so far with [/]!

from anansi import parse_tags

print(parse_tags('[yellow bg_green italic]Oh god this is unreadable[/]'))

Let's get CRAZY:

from anansi import parse_tags

print(parse_tags('[yellow bg_green italic]Oh god this is [bold]unreadable[/bold /italic under] Please remove[/bg] the colors[/] oh thank you.'))

Hyperlinks

Anansi also supports hyperlinking, as long as your terminal does too:

from anansi import parse_tags

print(parse_tags('[link=https://github.com/3digitdev/anansi-tags]Anansi[/link] is a very simple library!'))

You can even style hyperlinks!

from anansi import parse_tags

print(parse_tags('[link=https://github.com/3digitdev/anansi-tags][bold]Anansi[/][/link] is a very simple library!'))

Note: Remember that you can't combine link tags with any other type of tag! They are incompatible.

ANSI-Strings → Anansi Tags

This will work for all supported Anansi tags, including links:

from anansi import parse_ansi

# Yep, ansi is ugly.
print(parse_ansi('\x1b[1mHello\x1b[22m'))
# Result:  '[bold]Hello[/bold]'

Stripping Anansi Tags

from anansi import strip_tags

print(strip_tags('[bold]Hello[/bold]'))
# Result:  'Hello'

If you strip a hyperlink, by default it will remove the URL, leaving only the text:

from anansi import strip_tags

print(strip_tags('[link=www.google.com]A link to Google[/link]'))
# Result:  'A link to Google'

You can configure this behavior with keep_url:

from anansi import strip_tags

print(strip_tags('[link=www.google.com]A link to Google[/link]', keep_url=True))
# Result:  'www.google.com'

Stripping ANSI codes

from anansi import strip_ansi

print(strip_ansi('\x1b[1mHello\x1b[22m'))
# Result:  'Hello'

Just like with strip_tags(), stripping a hyperlink removes the URL, leaving only the text:

from anansi import strip_ansi

print(strip_ansi('\x1b]8;;www.google.com\x1b\x5cA link to Google\x1b]8;;\x1b\x5c'))
# Result:  'A link to Google'

Also, just like with strip_tags(), you can tell strip_ansi() to only keep the URL:

from anansi import strip_ansi

print(strip_ansi('\x1b]8;;www.google.com\x1b\x5cA link to Google\x1b]8;;\x1b\x5c', keep_url=True))
# Result:  'www.google.com'

FAQ

Didn't you know that {other library} supports this?

I probably didn't tbh -- I didn't look too hard. Odds are though that whatever library you're thinking of probably is much bigger with way more features and support and fanciness to it. If that's better for you, then great!

My intention here was to create a no-frills way to just...deal with strings. I found this surprisingly difficult in other similar works, and that's what prompted me to write Anansi!

Why do you only support 8 colors??

Honestly, if you have a need for the difference between 216 ("light_salmon1") and 217 ("light_pink1"), it's likely that your needs are severely more complex than what I intended for this library. I highly rcommend you go check out awesome libraries like Rich instead!

Why don't you support {other ANSI code}

Kinda the same reason as above? There's a lot of ANSI stuff out there, and this library is really just for the most basic usage that you should usually need I think.

{Supported ANSI code} isn't doing anything!

Yeah I added some in despite them not being widely supported. Of note, I've found that blink, frame, circle, and overline all seem to just...do nothing for me too? Using iTerm over here. Maybe some support it so I wanted them as options I guess, but your mileage may vary, sorry! Terminal standards are all over the place.

My link is acting weird

I haven't done much thorough testing of links, and url-encoding is another complex standard. If you find scenarios that don't work feel free to open an issue! I can't promise I'll make supporting URL standards a full priority, but it doesn't hurt to ask!

Metadata

Release files for anansi-tags 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anansi-tags 1.0.0
File Size Uploaded Provenance Actions
anansi_tags-1.0.0.tar.gz 19.8 kB Without provenance Details for anansi_tags-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anansi-tags 1.0.0
File Interpreter ABI Platform Actions
anansi_tags-1.0.0-py3-none-any.whl Python 3 none any Details for anansi_tags-1.0.0-py3-none-any.whl

Total release size: 37.2 kB

Release files / anansi_tags-1.0.0.tar.gz

Download URL anansi_tags-1.0.0.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e234f9bb2f05e6605e58fcb5da131b3ff87660e068eff992d78a68282eec44ed
BLAKE2b-256 checksum
How to use checksums
bc77066e6c880c5ae0e04b7aa4dda184aaa7d1e9764647466a897bf4ea65fe17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release files / anansi_tags-1.0.0-py3-none-any.whl

Download URL anansi_tags-1.0.0-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe39a4fa006992273c8e8c6971527b0f6751fbb971f54984aaf0e45836e835e0
BLAKE2b-256 checksum
How to use checksums
837280903297de42e68a1a0d812554274e088c8fb15b66464e5161540383c4c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Security for anansi-tags 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page