## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be
## consistent with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is reddit.
##
## The Original Developer is the Initial Developer. The Initial Developer of
## the Original Code is reddit Inc.
##
## All portions of the code written by reddit are Copyright (c) 2006-2012
## reddit Inc. All Rights Reserved.
###############################################################################
<%!
import json
from r2.models import FakeSubreddit
from r2.lib.filters import spaceCompress, unsafe, safemarkdown
from r2.lib.template_helpers import add_sr, js_config, static
from r2.lib.utils import cols, long_datetime, timesince
from r2.lib.tracking import UserInfo
from datetime import datetime
%>
<%def name="tags(**kw)">
%for k, v in kw.iteritems():
%if v:
${k.strip('_')}="${v}" \
%endif
%endfor
%def>
## override the link _class so that we can globally update
## the way links are handled (if need be)
<%def name="_a(**kw)">
${caller.body()}
%def>
<%def name="_a_buffered(body, **kw)" buffered="True">
${body}
%def>
## thing should be global
<%def name="_id(arg)">
id="${arg}_${thing and thing._fullname or ''}"
%def>
<%def name="css_class(thing)">
${' '+thing.css_class if hasattr(thing, 'css_class') and thing.css_class else ''}
%def>
<%def name="submit_form(onsubmit='', action='', _class='', method='post', _id='', **params)">
%def>
<%def name="first_defined(*kw)">
%if not kw or kw[0] == UNDEFINED or not kw[0]:
${first_defined(kw[1:])}
%endif
%def>
<%def name="error_field(error_name, field_name, kind='span')">
<% error_key = (error_name, field_name) %>
<${kind} class="error ${error_name} field-${field_name}"
style="${'' if error_key in c.errors else 'display:none'}">
%if error_key in c.errors:
${c.errors[error_key].message}
%endif
${kind}>
%def>
<%def name="success_field(success_str, kind='p', successful=False, hide='')">
<${kind} class="error success">
%if successful:
${success_str}
%endif
${kind}>
%def>
<%def name="img_link(link_text, img, path, _id='', target='', img_id=None, size=None, **kw)">
<%
if (not target or target == '_parent') and c.cname:
target = '_top'
if target:
kw['target'] = target
path = add_sr(path, sr_path = False)
kw['target'] = target
if size is None:
size_str = ""
else:
size_str = "width='%d' height='%d'" % (size[0], size[1])
%>
<%call expr="_a(href=path, _id=_id, **kw)">
%call>
%def>
<%def name="plain_link(link_text, path, _sr_path = True, nocname=False, fmt='', target='', **kw)">
## caching comment:
## in addition to the args, this function also makes use of c.cname,
## and, via add_sr, both c.site.name and c.render_style.
##
## This function is called by (among other places) NavMenu as the
## primary rendering view. Any changes to the c-usage of this function
## will have to be propagated up.
<%
if (not target or target == '_parent') and c.cname:
target = '_top'
if c.cname and path.startswith('http://'):
target = '_top'
if target:
kw['target'] = target
link = _a_buffered(link_text,
href=path and add_sr(path, sr_path=_sr_path, nocname=nocname),
**kw)
%>
${unsafe((fmt % link) if fmt else link)}
%def>
<%def name="post_link(link_text, post_path, redir_path, params, _sr_path=True,
nocname=False, fmt='', target='', **kw)">
<%
action = add_sr(post_path, sr_path=_sr_path, nocname=nocname)
href = add_sr(redir_path, sr_path=_sr_path, nocname=nocname)
if (not target or target == '_parent') and c.cname:
target = '_top'
if c.cname and redir_path.startswith('http://'):
target = '_top'
if target:
kw['target'] = target
onclick = "$(this).parent().submit(); return false;"
link = _a_buffered(link_text, href=href, onclick=onclick, **kw)
%>
%def>
<%def name="text_with_links(txt, _sr_path = False, nocname=False, **kw)">
<%
from r2.lib.filters import _force_unicode
for key, link_args in kw.iteritems():
link_args.setdefault("_sr_path", _sr_path)
link_args.setdefault("nocname", nocname)
kw[key]=spaceCompress(capture(plain_link, **link_args))
txt = _force_unicode(txt) % kw
txt = txt.replace(" <", " <").replace("> ", "> ")
%>
${unsafe(txt)}
%def>
<%def name="language_tool(name='lang', allow_blank = False,
default_lang = g.lang,
show_regions = False,
all_langs = False)">
<%
langs = g.all_languages if all_langs else g.languages
if not show_regions:
langs = [x for x in langs if len(x) == 2]
%>
%if langs:
%endif
%def>
<%def name="language_checkboxes(default = 'all')">
<%
all_checked, some_checked = "checked='checked'", ""
if default != "all":
all_checked, some_checked = some_checked, all_checked
%>
<%
all_langs = [x for x in g.all_languages if len(x) == 2]
%>
%for col in cols(all_langs, 3):
%for lang in col:
<%
idname = "lang-" + lang
if default != "all" and lang in default:
checked = "checked='checked'"
else:
checked = ""
%>
%def>
<%def name="timestamp(date, since=None)">
## todo: use pubdate attribute once things are tags.
## note: comment and link templates will pass a CachedVariable stub as since.
%def>
<%def name="thing_timestamp(thing, since=None)">
## todo: use pubdate attribute once things are tags.
## note: comment and link templates will pass a CachedVariable stub as since.
${timestamp(thing._date, since=since)}
%def>
<%def name="percentage(slice, total)">
%if total is None or total == "" or total == 0 or slice is None or slice == "":
--
%else:
${int(100 * slice / total)}%
%endif
%def>
<%def name="pretty_button(label, func=None, func_vars='', extra_class='')">
${label}
%def>
<%def name="edited(thing, lastedited=None)">
%if isinstance(thing.editted, datetime):
%elif thing.editted:
*
%endif
%def>
<%def name="_md(text, wrap=False)">
${unsafe(safemarkdown(_(text), wrap=wrap))}
%def>