Skip to main content

Lektor Shortcodes

PyPI

The Lektor Shortcodes plugin allows you to use shortcodes (shortcodes are something like custom tags) in your fields (not templates), so your content doesn't have to have repetitive snippets over and over.

For example, my blog has some specific HTML that I add when I want an image with a border and caption to be displayed. The HTML looks like this:

    <div class="alignright">
        <a href="image-large.jpg">
            <img src="image.jpg" />
            <span class="caption">The caption</span>
        </a>
    </div>

Copy-pasting this every time gets tedious, and I have to search and replace it in all the content files every time I want to make a change. With the shortcodes plugin, this can be written as:

[% image align=right link="image-large.jpg" image=image.jpg caption="The caption" %]

Much easier, cleaner and less repetitive.

Installation

To install the plugin, just add lektor-shortcodes to your plugins from the command line:

lektor plugins add lektor-shortcodes

Usage

Using the plugin is simple. Just create a config file called shortcodes.ini in your configs directory and specify some shortcode templates. The templates are full Jinja templates, although (due to some limitations of ini files) they need to be on one line.

For instance, for the example above, the config file could be:

[global]
image = '<div class="align{{ align }}">{% if link %}<a href="{{ link }}"{% if not link.startswith("http") %} data-lightbox="gallery"{% endif %}>{% endif %}<img src="{{ image }}">{% if link %}</a>{% endif %}{% if caption %}<span class="caption">{{ caption }}</span>{% endif %}</div>'

This will allow you to use shortcodes with optional arguments, like so:

# An image with no caption or link:
[% image align=right image=hello.jpg %]


# An image with a link:
[% image align=right link=http://www.example.com image=hello.jpg %]

# Link and caption:
[% image align=right link=http://www.example.com image=hello.jpg caption="Hello!" %]

Shortcodes defined within the section named global will be processed automatically inside any of your site’s Markdown content. It is also possible to define shortcodes which are only expanded when the Jinja2 template for a page explicitly requests it. Shortcodes defined in any section not named global will only be applied when the template passed the content through a Jinja2 filter named shortcode. For example, if your HTML template references a field called body, and your config has a section called [special], you may request expanding shortcodes defined within the special section like so:

{{ body|shortcodes(section="special") }}

This will enable all shortcodes from the specified section, in addition to all globally defined shortcodes. If no section is specified, the filter defaults to the section named main (so it will include shortcodes in main unless you request a different section, and it will always include shortcodes in global no matter what).

Miscellanea

If you find a bug or have a feature request, please open an issue or file a PR. Thanks!

Release files for lektor-shortcodes 0.2.7

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

Source distribution (sdist)

Source distribution for lektor-shortcodes 0.2.7
File Size Uploaded
lektor-shortcodes-0.2.7.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lektor-shortcodes 0.2.7
File Interpreter ABI Platform
lektor_shortcodes-0.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / lektor-shortcodes-0.2.7.tar.gz

Download URL lektor-shortcodes-0.2.7.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
93ed20de1efd9e58ccad083018c980cf55a490b4f88c00afaefc18e65a63ba75
BLAKE2b-256 checksum
How to use checksums
895c465fd2e310937d2dde06b1681ed32acafdc13b8a0bc14305831408e2d639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / lektor_shortcodes-0.2.7-py3-none-any.whl

Download URL lektor_shortcodes-0.2.7-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7bc9c3ba5d09023201a14f42a6ede7c890bcd07f2221de2ec92ea00e71d83e8b
BLAKE2b-256 checksum
How to use checksums
efb814974f3b0f50077e9c14da16bc721beb44a75f331743dab99f79ec730d79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

1 release file

0.2.5

3 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

1 release file

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