Django-Spellbook

DjangoLikeTagProcessor (Markdown Extensions)

Welcome to the advanced guide for the DjangoLikeTagProcessor. This processor is a core component of Django Spellbook's markdown rendering pipeline, enabling the powerful blend of markdown with Django template tag syntax. Understanding its inner workings can help you customize parsing behavior or debug complex rendering scenarios.

What is DjangoLikeTagProcessor?

The DjangoLikeTagProcessor is a markdown.blockprocessors.BlockProcessor subclass. Its primary role is to identify and process blocks of text starting with Django-like template tags.

It intelligently distinguishes between:

  1. Custom HTML-like elements: Tags like % div .my-class %...% enddiv % are converted into actual HTML elements (<div class="my-class">...</div>) with their content recursively parsed as markdown.
  2. Built-in Django template tags: Tags like % if user.is_authenticated %, % for item in list %, % url 'my_view' %, etc., are preserved within special <django-tag> elements, ensuring they are passed through to the final Django template rendering engine untouched.

See the official python-markdown docs for more information.

How to use the DjangoLikeTagProcessor

To use the processor directly, you would typically initialize the markdown.Markdown class and include the processor in its extensions.


import markdown
from django_spellbook.markdown.extensions import DjangoLikeTagExtension

# Example Markdown Input (read below for full example)
with open('example.md', 'r') as f:
    markdown_input = f.read()

# Initialize Markdown with the extension
md = markdown.Markdown(extensions=[DjangoLikeTagExtension()])

# Convert Markdown to HTML
html_output = md.convert(markdown_input)

print(html_output)

Example Input and Output

Given the markdown_input in the Python snippet above:

Input Markdown:


# Example Document

{% div .content-wrapper #main %}
This is the main content area.

{% if user.is_logged_in %}
    Welcome back, {{ user.name }}!
    Look at this {% strong %}important{% endstrong %} message.
{% else %}
    Please log in.
{% endif %}

Here is a list:
- Item 1
- Item 2
{% enddiv %}

Regular paragraph outside the custom tag.

Expected HTML Output:

The processor converts the custom % div % and % strong % tags into standard HTML elements with the specified attributes. Crucially, it preserves the Django % if %, % else %, and % endif % tags by wrapping them in <django-tag> elements. This ensures they are not processed as literal text by the markdown parser but are passed through to be interpreted by the Django template engine later.


<h1>Example Document</h1>
<div class="content-wrapper" id="main"><p>This is the main content area.</p>
<django-tag>{% if user.is_logged_in %}</django-tag>
<p>Welcome back, {{ user.name }}!
Look at this <strong>important</strong> message.</p>
<django-tag>{% else %}</django-tag>
<p>Please log in.</p>
<django-tag>{% endif %}</django-tag>
<p>Here is a list:</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul></div>
<p>Regular paragraph outside the custom tag.</p>

This output HTML can then be included in a Django template. When Django renders that template, it will process the content within the <django-tag> elements as standard Django template logic, effectively merging markdown structure with dynamic Django rendering.

Core Processing Logic

The processor uses regular expressions and distinct handling logic based on the tag identified.