Simple shortcodes resolver utility lib.
Project description
Text shortcodes resolver
Installation
pip install wc-shortcodes
Usage
Simple usage:
from wc_shortcodes.registry import CodesRegistry
from wc_shortcodes.transformer import transform
registry = CodesRegistry()
# Adding handler for a specific shortcode:
@registry.register()
def shortcode(entity, context={}):
return entity.content
# Transforming some content using registered resolvers.
transform(registry, '''
[shortcode unhandled-parameter="it's value"]
Content.
[/shortcode]
''')
# Result will be:
# > 'Content.'
Shortcode handler can also be defined with additional parameters:
@registry.register(
# Shortcode custom name:
'name',
# Does this shortcode can handle multiple entities at a time.
# `False` by default.
multiple=False,
# Should entity's `content` property be parsed and resolved before it
# passed to this handler.
# Or you should do something with raw content instead.
# `True` by default.
autoresolve_content=True,
)
Multiresolver
Multiresolver is not the same as singular one(duh..).
This configuration was made for performance reasons.
For example if there is, lets say, 10 same shortcuts with different parameters passed inside one text. It's obvious that whose 10 items will do same work for 10 times. So to prevent such unefficiency multihandlers had been added.
@registry.register(multiple=True)
def shortcode(
# They receive all current entities as list.
entities: List[Entity],
context={}
) -> List[str]:
return [
# And must return result in the exact same order as they have been passed.
entity.content for entity in entities
]
Django
Installation
In settings.py
:
INSTALLED_APPS += [
'wc_shortcodes.contrib.django',
]
Usage
Specific implementation not yet ready. Can be used as described before.
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[Unreleased]
[0.1.1]
Fixed
- Empty registry entities matcher finds
''
key. Not anymore. - Register code with empty name prohibited since now.
[0.1.0]
Initial version.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file wc-shortcodes-0.1.1.tar.gz
.
File metadata
- Download URL: wc-shortcodes-0.1.1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7249b8559e9db7fa839cc581498b513b055b855858ee1dba12c794fe19dfef9 |
|
MD5 | 9688e5e6e17544e7d3a22092436b6595 |
|
BLAKE2b-256 | 1b34814434691ead4e36a0756329da3d5f96a30896e7981889443eb537685627 |