Add polymorphic field type to Lektor
Project description
Lektor Polymorphic Type
This plugin adds a new polymorphic lektor field type, polymorphic
.
The determination of the actual type implementation of the field value
is deferred until evaluation time.
A motivating use case is to support having a “body” field whose
formatting can be switched between, e.g., markdown
, and html
(or
some other formatted type, such as reStructuredText.)
Installation
Add lektor-polymorphic-type to your project from command line:
lektor plugins add lektor-polymorphic-type
See the Lektor plugin documentation for more information.
How It Works
If the field has a polymorphic_type
option set, that value is evaluated
and the result is interpreted as the name of the final type for the
field.
If no polymorphic_type
option is set for the field, then we look for a
field on the current record whose name is name of the current field
with “_type
” appended.
Examples
Simple Example
Here is an example model file for a simple page, with a selectable body format:
# page.ini
[model]
name = Page
label = {{ this.title }}
[fields.title]
label = Title
type = string
[fields.body]
label = Body
type = polymorphic
[fields.body_type]
label = Body Type
type = select
choices = markdown, html, text
default = markdown
Here, the value of the body_type
field on a particular page will
determine whether the body
field is interpreted as being markdown
,
html
or text
.
Contrived Example
Here is a contrived example showing the use of the polymorphic_type
option:
# page.ini
[model]
name = Page
label = {{ this.title }}
[fields.title]
label = Title
type = string
[fields.body]
label = Body
type = polymorphic
polymorphic_type = 'html' if this.body.lstrip().startswith('<') else 'markdown'
In this case, the body
field will be interpreted as raw HTML if the
content of that field starts with a “<
”, otherwise it will be
interpreted as Markdown text.
Author
Jeff Dairiki dairiki@dairiki.org
Changelog
0.1 — 2021-02-05
No code changes.
Update development status classifier to "stable".
Test under python 3.9. Stop testing under 3.5.
0.1b1 — 2020-05-04
Initial release.
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
Built Distribution
Hashes for lektor-polymorphic-type-0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f348940430a9339f28f0bba80cb151ea40d4e39cf75e1f73ffed3c3b826a326 |
|
MD5 | 867ac9a88333030c110648aa91e1d8c2 |
|
BLAKE2b-256 | b6f17a2ac865e2cd3ad4e11447882e19b90cac9fb5baa192296fc6cdd32085ca |
Hashes for lektor_polymorphic_type-0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5539d4badcca0ec548544341fd95e888c2fd948e57852cf096d44c39ae6fd4f |
|
MD5 | ae2012fb5a5577732f72c86e6732ee7c |
|
BLAKE2b-256 | 44a56e415da4b447642a5765fee67fccbf1a76d01e5b8cc1671fde83c92e3932 |