Skip to content

Commit

Permalink
Add template filter add_room_links
Browse files Browse the repository at this point in the history
Add a filter which greps room patterns in a string and converts them to
a link to our twiki room map.
  • Loading branch information
donald committed Aug 13, 2020
1 parent 5f045ce commit 93bccb1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions mpicms/base/templatetags/base_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import re

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

from mpicms.base.utils import get_room_link

Expand Down Expand Up @@ -26,3 +31,30 @@ def remove_i18n(url):
if url.startswith('/en') or url.startswith('/de'):
return url[3:]
return url


# The ROOM_PATTERN should be in sync with the twiki plugin setting
# in http://twiki.molgen.mpg.de/foswiki/System/RoomsPlugin

ROOM_PATTERN = re.compile(
"("
r"R[0-4]\d\d[abc]?"
r"|[012]\.[012][012]\d\.?\d?[ab]?"
r"|B[01]\.[01][0-9]"
r"|KB\.0[01]|E[012]\da?"
r"|K4?\.[012][012]\d?(?:\.\d)?"
r"|K[B123]\.\d\d\d?[abc]?"
r"|W[012]\d[abc]?"
r"|K[TV]1?\.[012]\d[abcdefg]?"
r"|TH[0-5]\d[ab]?"
r"|HS\d\d"
r"|GH[01]\d"
r"|[K0123]\.[1234]\.F?\d\d"
")" )

@register.filter(needs_autoescape=True)
@stringfilter
def add_room_links(rooms, autoescape=True):
if autoescape:
rooms = conditional_escape(rooms)
return mark_safe(ROOM_PATTERN.sub(r'<a href="http://twiki.molgen.mpg.de/foswiki/bin/room/\1">\1</a>', rooms))

0 comments on commit 93bccb1

Please sign in to comment.