def render_tag(tag, data=None, attrs=None, children_kwargs=None, request=None, **attrs_kwargs): """ Renders HTML tag. Args: tag: Tag name. data: Children elements for the given tag. Each element is rendered with the render_html() function. attrs: A dictionary of attributes. request: A request object that is passed to the render function when it is applied to children. **attr_kwargs: Keyword arguments are converted to additional attributes. Examples: >>> render_tag('a', 'Click me!', href='www.python.org') '<a href="www.python.org">Click me!</a> """ if data is None: data = '' children_kwargs = children_kwargs or {} data = render(data, request=request, **children_kwargs) attrs = _attrs(attrs, request=request, **attrs_kwargs) if attrs: attrs = safe(' ') + attrs return safe('<%s%s>%s</%s>' % (tag, attrs, data, tag))
def markdown(text, *, output_format='html5', **kwargs): """ Renders Markdown content as HTML and return as a safe string. """ from markdown import markdown return safe(markdown(text, output_format=output_format, **kwargs))
def __new__(cls, data, escape=True, parent=None): if isinstance(data, Markup): return Markup.__new__(cls, data) elif escape: try: data = safe(data.__html__()) return Markup.__new__(cls, data) except AttributeError: return Markup.__new__(cls, _escape(data)) else: return Markup.__new__(cls, data)
def quill_script(id=None, read_only=False, theme='snow'): names = dict( id=id, read_only=str(read_only).lower(), theme='snow', ) return \ script(safe(""" $(function(){ var editor = new Quill(#%(id)s{ debug: 'info', modules: { toolbar: '#%(id)s-toolbar' }, readOnly: %(read_only)s, theme: %(theme)r }); }) """ % names))
def test_json_conversion(): tag = div(class_='foo')[h1('bar'), safe('foo <b>bar</b>')] pprint(tag.json()) assert tag.json() == { 'tag': 'div', 'classes': ['foo'], 'children': [ { 'tag': 'h1', 'children': [{ 'tag': 'text', 'text': 'bar' }] }, { 'tag': 'text', 'text': 'foo <b>bar</b>' }, ] }
def _(attrs, request=None, **kwargs): return safe(attrs.render(request, **kwargs))
dt(_('Grade')), dd(final_grade + '%'), dt(_('Num submissions')), dd(num_submissions), ] ], div()[ h2(_('List of submissions')), p(_('(Click on an option to expand)')), mdl.div(shadow=4)[ [submission(sub) for sub in submissions], ] ], submission_script, ] submission_script = script( safe(""" function expandSubmission(obj) { var expandable = $(obj).find('.cs-submission__content'); if (expandable.length && expandable[0].classList.contains('hidden')) { expandable.removeClass('hidden').hide().show(200); } else { expandable.hide(200); window.setTimeout(function () { expandable.addClass('hidden') }, 200); } } """))
dl()[ dt(_('Grade')), dd(final_grade + '%'), dt(_('Num submissions')), dd(num_submissions), ] ], div()[ h2(_('List of submissions')), p(_('(Click on an option to expand)')), mdl.div(shadow=4)[ [submission(sub) for sub in submissions], ] ], submission_script, ] submission_script = script(safe(""" function expandSubmission(obj) { var expandable = $(obj).find('.cs-submission__content'); if (expandable.length && expandable[0].classList.contains('hidden')) { expandable.removeClass('hidden').hide().show(200); } else { expandable.hide(200); window.setTimeout(function () { expandable.addClass('hidden') }, 200); } } """))
def _(x, request=None, **kwargs): return safe(x.render(request or _request(), **kwargs))
def _(seq, **kwargs): return safe('\n'.join(render(x, **kwargs) for x in seq))
def render(self, request, **kwargs): return safe(''.join( render(x, request=request, **kwargs) for x in self))