Created
May 22, 2014 12:23
-
-
Save acspike/1edf260763577267d12f to your computer and use it in GitHub Desktop.
templatish tags for text plugins to avoid fragile internal urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from cms.models.pagemodel import Page | |
from django.contrib.sites.models import Site | |
from django.utils.safestring import mark_safe | |
TAG = re.compile(r''' | |
\{% # opening | |
\s* # optional whitespace | |
( | |
[a-z_-]+ # command | |
(?:\s+[^\s%}]+)* # optional arguments | |
) | |
\s* # optional whitespace | |
%\} # closing | |
''', re.VERBOSE) | |
def cms_plugin_templateish_tags(instance, placeholder, rendered_content, original_context): | |
site = Site.objects.get_current().id | |
rules = { | |
'static': lambda x: original_context['STATIC_URL'], | |
'page': lambda x: Page.objects.public().get(reverse_id=x[0], site=site).get_absolute_url(), | |
} | |
def replace(m): | |
args = m.groups()[0].split(' ') | |
cmd = args.pop(0) | |
try: | |
return rules[cmd](args) | |
except: | |
return '' | |
if instance.plugin_type == 'TextPlugin': | |
try: | |
return mark_safe(TAG.sub(replace, rendered_content)) | |
except: | |
return rendered_content | |
return rendered_content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment