educaweb2/shortcodes/url.tmpl

87 lines
1.9 KiB
Cheetah

<%doc>
Return URL from email and some social networks.
Template engine: Mako
Usage:
{{% url <site> [suffix=<suffix>] [delprt=<delprt>] [account=<account>] %}}
where <site> is one of 'email', 'xmpp', 'mastodon', 'peertube' or 'rss', corresponding to GLOBAL_CONTEXT variable of EMAIL_URL, XMPP_URL, MASTODON_URL, PEERTUBE_URL, GIT_URL and RSS_URL respectively, sufix variable allow to add a suffix <suffix> to the URL, if delprt parameter is True then remove protocol from URL, if account parameter is True then return the user account
Example: {{% url 'mastodon' %}} will show the content of MASTODON_URL defined in GLOBAL_CONTEXT of config.py
</%doc>
<%!
def del_prt(url):
i = url.find('//') + 1
if i == 0:
i = url.find(':')
return url[i+1:]
%>
<%!
def acc_extr(url):
url = del_prt(url)
i = url.find('/')
if i > -1:
host = url[0:i]
j = url.rfind('/') + 1
if j > 0:
user = url[j:]
else:
user=''
return user + '@' + host
else:
i = url.find('@')
if i > -1:
user = url[:i]
host = url[i+1:]
return user + '@' + host
else:
return url
%>
% if suffix is UNDEFINED:
<% s = '' %>
% else:
<% s = suffix %>
% endif
% if delprt is UNDEFINED:
<% d = False %>
% else:
<% d = delprt %>
% endif
% if account is UNDEFINED:
<% a = False %>
% else:
<% a = account %>
% endif
% if _args[0] == 'email':
<% url = EMAIL_URL + s %>
% elif _args[0] == 'xmpp':
<% url = XMPP_URL + s %>
% elif _args[0] == 'mastodon':
<% url = MASTODON_URL + s %>
% elif _args[0] == 'peertube':
<% url = PEERTUBE_URL + s %>
% elif _args[0] == 'git':
<% url = GIT_URL + s %>
% elif _args[0] == 'rss':
<% url = RSS_URL + s %>
% else:
Unknown
% endif
% if a:
${url | acc_extr}
% elif d:
${url | del_prt}
% else:
${url}
% endif