Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcodes for markdown templates #1174

Open
wants to merge 5 commits into
base: 2.3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,7 @@

# necessary since django 3.2, explicitly set primary key type to avoid warnings
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

MARKDOWN_TEMPLATES: dict[str, str] = {
# for example: 'not_empty': 'core/text_blocks/template_for_not_empty.html',
}
16 changes: 14 additions & 2 deletions rdmo/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.apps import apps
from django.conf import settings
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.template.loader import get_template
from django.template.loader import get_template, render_to_string
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -392,6 +392,18 @@ def markdown2html(markdown_string):
f'<span class="show-less" onclick="showLess(this)"> ({hide_string})</span></p>',
html
)
html = inject_textblocks(html)
return html


def inject_textblocks(html):
'''textblocks (e.g. for help texts) can be injected into free text fields as small templates via Markdown'''
for textblock_id in re.findall(r'{{(.*?)}}', html): # strings between curly brackets
template_path: str = settings.MARKDOWN_TEMPLATES[textblock_id]
html = re.sub( '{{' + textblock_id + '}}',
render_to_string(template_path),
html
)
return html


Expand All @@ -417,4 +429,4 @@ def save_metadata(metadata):
json.dump(metadata, f)
f = open(tmp_metadata_file)
log.info('Save metadata file %s %s', tmp_metadata_file, str(metadata))
return tmp_metadata_file
return tmp_metadata_file