本文整理汇总了Python中tornado.escape.xhtml_escape方法的典型用法代码示例。如果您正苦于以下问题:Python escape.xhtml_escape方法的具体用法?Python escape.xhtml_escape怎么用?Python escape.xhtml_escape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.escape
的用法示例。
在下文中一共展示了escape.xhtml_escape方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _render_parts
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def _render_parts(self, value, parts=[]):
if isinstance(value, (unicode, bytes)):
parts.append(escape.xhtml_escape(value))
elif isinstance(value, int) or isinstance(value, long):
parts.append(str(value))
elif isinstance(value, datetime.datetime):
parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
elif isinstance(value, dict):
for name, subvalue in value.iteritems():
if not isinstance(subvalue, list):
subvalue = [subvalue]
for subsubvalue in subvalue:
parts.append('<' + escape.utf8(name) + '>')
self._render_parts(subsubvalue, parts)
parts.append('</' + escape.utf8(name) + '>')
else:
raise Exception("Unknown S3 value type %r", value)
示例2: __init__
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def __init__(self, autoescape=_DEFAULT_AUTOESCAPE, namespace=None,
whitespace=None):
"""构造一个模板加载器.
:arg str autoescape: 在模板命名空间中的函数名, 例如 "xhtml_escape",
或默认情况下为 ``None`` 来禁用自动转义.
:arg dict namespace: 一个被加入默认模板命名空间中的字典或 ``None``.
:arg str whitespace: 一个指定模板中whitespace默认行为的字符串;
参见 `filter_whitespace` 查看可选项. 默认是 "single" 对于
".html" 和 ".js" 文件的结束, "all" 是为了其他文件.
.. versionchanged:: 4.3
添加 ``whitespace`` 参数.
"""
self.autoescape = autoescape
self.namespace = namespace or {}
self.whitespace = whitespace
self.templates = {}
# self.lock protects self.templates. It's a reentrant lock
# because templates may load other templates via `include` or
# `extends`. Note that thanks to the GIL this code would be safe
# even without the lock, but could lead to wasted work as multiple
# threads tried to compile the same template simultaneously.
self.lock = threading.RLock()
示例3: render_linked_js
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def render_linked_js(self, js_files: Iterable[str]) -> str:
"""Default method used to render the final js links for the
rendered webpage.
Override this method in a sub-classed controller to change the output.
"""
paths = []
unique_paths = set() # type: Set[str]
for path in js_files:
if not is_absolute(path):
path = self.static_url(path)
if path not in unique_paths:
paths.append(path)
unique_paths.add(path)
return "".join(
'<script src="'
+ escape.xhtml_escape(p)
+ '" type="text/javascript"></script>'
for p in paths
)
示例4: render_linked_css
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def render_linked_css(self, css_files: Iterable[str]) -> str:
"""Default method used to render the final css links for the
rendered webpage.
Override this method in a sub-classed controller to change the output.
"""
paths = []
unique_paths = set() # type: Set[str]
for path in css_files:
if not is_absolute(path):
path = self.static_url(path)
if path not in unique_paths:
paths.append(path)
unique_paths.add(path)
return "".join(
'<link href="' + escape.xhtml_escape(p) + '" '
'type="text/css" rel="stylesheet"/>'
for p in paths
)
示例5: xsrf_form_html
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def xsrf_form_html(self) -> str:
"""An HTML ``<input/>`` element to be included with all POST forms.
It defines the ``_xsrf`` input value, which we check on all POST
requests to prevent cross-site request forgery. If you have set
the ``xsrf_cookies`` application setting, you must include this
HTML within all of your HTML forms.
In a template, this method should be called with ``{% module
xsrf_form_html() %}``
See `check_xsrf_cookie()` above for more information.
"""
return (
'<input type="hidden" name="_xsrf" value="'
+ escape.xhtml_escape(self.xsrf_token)
+ '"/>'
)
示例6: render_linked_js
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def render_linked_js(self, js_files):
"""Default method used to render the final js links for the
rendered webpage.
Override this method in a sub-classed controller to change the output.
"""
paths = []
unique_paths = set()
for path in js_files:
if not is_absolute(path):
path = self.static_url(path)
if path not in unique_paths:
paths.append(path)
unique_paths.add(path)
return ''.join('<script src="' + escape.xhtml_escape(p) +
'" type="text/javascript"></script>'
for p in paths)
示例7: render_linked_css
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def render_linked_css(self, css_files):
"""Default method used to render the final css links for the
rendered webpage.
Override this method in a sub-classed controller to change the output.
"""
paths = []
unique_paths = set()
for path in css_files:
if not is_absolute(path):
path = self.static_url(path)
if path not in unique_paths:
paths.append(path)
unique_paths.add(path)
return ''.join('<link href="' + escape.xhtml_escape(p) + '" '
'type="text/css" rel="stylesheet"/>'
for p in paths)
示例8: _render_parts
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def _render_parts(self, value, parts=[]):
if isinstance(value, (unicode, bytes_type)):
parts.append(escape.xhtml_escape(value))
elif isinstance(value, int) or isinstance(value, long):
parts.append(str(value))
elif isinstance(value, datetime.datetime):
parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
elif isinstance(value, dict):
for name, subvalue in value.iteritems():
if not isinstance(subvalue, list):
subvalue = [subvalue]
for subsubvalue in subvalue:
parts.append('<' + escape.utf8(name) + '>')
self._render_parts(subsubvalue, parts)
parts.append('</' + escape.utf8(name) + '>')
else:
raise Exception("Unknown S3 value type %r", value)
示例9: __init__
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def __init__(self, autoescape=_DEFAULT_AUTOESCAPE, namespace=None):
"""Creates a template loader.
root_directory may be the empty string if this loader does not
use the filesystem.
autoescape must be either None or a string naming a function
in the template namespace, such as "xhtml_escape".
"""
self.autoescape = autoescape
self.namespace = namespace or {}
self.templates = {}
# self.lock protects self.templates. It's a reentrant lock
# because templates may load other templates via `include` or
# `extends`. Note that thanks to the GIL this code would be safe
# even without the lock, but could lead to wasted work as multiple
# threads tried to compile the same template simultaneously.
self.lock = threading.RLock()
示例10: init_app
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def init_app(cls, application, jinja_options=None):
"""Init the application."""
app_settings = application.settings
_loader = FileSystemLoader(
app_settings.get('template_path', 'templates')
)
_jinja_config = {
'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
'auto_reload': app_settings.get('autoreload', False),
'loader': _loader,
'cache_size': 50 if app_settings.get('compiled_template_cache', True) else 0,
'autoescape': app_settings.get('autoescape', 'xhtml_escape') == "xhtml_escape"
}
_jinja_config.update(**(jinja_options or {}))
environment = Environment(**_jinja_config)
application.jinja_environment = environment
app_settings['jinja_environment'] = environment
environment.filters.update(tojson=tojson_filter, xhtml_escape=xhtml_escape, url_escape=url_escape, squeeze=squeeze, linkify=linkify)
return environment
示例11: get_description
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def get_description(self, raw=False):
"""
Returns desciption, escapes double quotes if sans_quotes is True, used
for rendering description inside fields.
"""
description = self.description
if not description:
description = ''
if not raw:
#description = escape.xhtml_escape(description)
extra_params = 'target="_blank" rel="nofollow"'
description = escape.linkify(description, True,
extra_params=extra_params)
#re_hash = re.compile(r'#[0-9a-zA-Z+]*',re.IGNORECASE)
#for iterator in re_hash.finditer(description):
description = re.sub(r'(\A|\s)#(\w+)', r'\1<a href="/tag/\2">#\2</a>', description)
description = description.replace('\n', '<br>')
return description
示例12: xsrf_form_html
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def xsrf_form_html(self):
"""一个将被包含在所有POST表单中的HTML ``<input/>`` 标签.
它定义了我们在所有POST请求中为了预防伪造跨站请求所检查的
``_xsrf`` 的输入值. 如果你设置了 ``xsrf_cookies`` application设置,
你必须包含这个HTML 在你所有的HTML表单.
在一个模板中, 这个方法应该使用 ``{% module xsrf_form_html() %}``
这种方式调用
查看上面的 `check_xsrf_cookie()` 了解更多信息.
"""
return '<input type="hidden" name="_xsrf" value="' + \
escape.xhtml_escape(self.xsrf_token) + '"/>'
示例13: generate
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def generate(self, **kwargs):
"""用给定参数生成此模板."""
namespace = {
"escape": escape.xhtml_escape,
"xhtml_escape": escape.xhtml_escape,
"url_escape": escape.url_escape,
"json_encode": escape.json_encode,
"squeeze": escape.squeeze,
"linkify": escape.linkify,
"datetime": datetime,
"_tt_utf8": escape.utf8, # for internal use
"_tt_string_types": (unicode_type, bytes),
# __name__ and __loader__ allow the traceback mechanism to find
# the generated source code.
"__name__": self.name.replace('.', '_'),
"__loader__": ObjectDict(get_source=lambda name: self.code),
}
namespace.update(self.namespace)
namespace.update(kwargs)
exec_in(self.compiled, namespace)
execute = namespace["_tt_execute"]
# Clear the traceback module's cache of source data now that
# we've generated a new template (mainly for this module's
# unittests, where different tests reuse the same name).
linecache.clearcache()
return execute()
示例14: test_escape_return_types
# 需要导入模块: from tornado import escape [as 别名]
# 或者: from tornado.escape import xhtml_escape [as 别名]
def test_escape_return_types(self):
# On python2 the escape methods should generally return the same
# type as their argument
self.assertEqual(type(xhtml_escape("foo")), str)
self.assertEqual(type(xhtml_escape(u("foo"))), unicode_type)