Skip to content

Instantly share code, notes, and snippets.

@acspike
Created May 22, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acspike/1edf260763577267d12f to your computer and use it in GitHub Desktop.
Save acspike/1edf260763577267d12f to your computer and use it in GitHub Desktop.
templatish tags for text plugins to avoid fragile internal urls
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