Exemplo n.º 1
0
def compile_webpy_template(source, filename):
    """Compiles a web.py template.
    """
    from web.template import Template
    text = Template.normalize_text(source)
    python_source = Template.generate_code(source, filename)
    python_source = "from webpy_ext import escape_, TemplateResult, ForLoop\n" + python_source
    return pyjs_translate(python_source, filename)
Exemplo n.º 2
0
def _compile(template_name):
    """
    Compiles a template file and returns the result.
    """
    template = _template_cache.get(template_name)

    if template is None or reload_templates:
        _template_cache[template_name] = template = Template(
            pkgutil.get_data(__name__, 'templates/' + template_name).decode('utf-8'),
            filename=template_name,
            globals={
                "STR": str,
                "LOGIN": get_sysname,
                "TOKEN": get_token,
                "CSRF": _get_csrf_input,
                "USER_TYPE": user_type,
                "DATE": convert_date,
                "ISO8601_DATE": iso8601_date,
                "TIME": _convert_time,
                "LOCAL_ARROW": local_arrow,
                "PRICE": text_price_amount,
                "SYMBOL": text_price_symbol,
                "TITLE": titlebar,
                "RENDER": render,
                "COMPILE": _compile,
                "CAPTCHA": _captcha_public,
                "MARKDOWN": text.markdown,
                "MARKDOWN_EXCERPT": text.markdown_excerpt,
                "SUMMARIZE": summarize,
                "SHA": CURRENT_SHA,
                "NOW": get_time,
                "THUMB": thumb_for_sub,
                "WEBP_THUMB": webp_thumb_for_sub,
                "M": macro,
                "R": ratings,
                "SLUG": text.slug_for,
                "QUERY_STRING": query_string,
                "INLINE_JSON": html.inline_json,
                "PATH": _get_path,
                "arrow": arrow,
                "constants": libweasyl.constants,
                "getattr": getattr,
                "json": json,
                "sorted": sorted,
                "staff": staff,
                "resource_path": get_resource_path,
            })

    return template
Exemplo n.º 3
0
def extract_webpy(fileobj, keywords, comment_tags, options):
    """Extract messages from webpy templates files.
    :param fileobj: the file-like object the messages should be extracted
                    from
    :param keywords: a list of keywords (i.e. function names) that should
                     be recognized as translation functions
    :param comment_tags: a list of translator tags to search for and
                         include in the results
    :param options: a dictionary of additional options (optional)
    :return: an iterator over ``(lineno, funcname, message, comments)``
             tuples
    :rtype: ``iterator``
    """
    code = Template.generate_code(fileobj.read().decode("utf-8"), fileobj.name)
    return extract_python(StringIO.StringIO(code), keywords,
                          comment_tags, options)
def t(code, **keywords):
    tmpl = Template(code, **keywords)
    return lambda *a, **kw: TestResult(tmpl(*a, **kw))
Exemplo n.º 5
0
def getTemplate(name: str) -> Template:
    with open(f"html/{name}", "r", encoding='utf8') as f:
        t = f.read()
        return Template(t)
    return None
Exemplo n.º 6
0
def gettemplate(s: str) -> Template:
    f = open(f"webuihtml/{s}.html", 'r', encoding='utf8')
    t = Template(f.read())
    f.close()
    return t
Exemplo n.º 7
0
def template_reader(path, **keywords):
    if Template:
        return Template(open(path).read(), filename=path, **keywords)
    return file_reader(path)