本文整理匯總了Python中webhelpers2.html.HTML.script方法的典型用法代碼示例。如果您正苦於以下問題:Python HTML.script方法的具體用法?Python HTML.script怎麽用?Python HTML.script使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類webhelpers2.html.HTML
的用法示例。
在下文中一共展示了HTML.script方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: javascript_link
# 需要導入模塊: from webhelpers2.html import HTML [as 別名]
# 或者: from webhelpers2.html.HTML import script [as 別名]
def javascript_link(*urls, **attrs):
"""Return script include tags for the specified javascript URLs.
``urls`` should be the exact URLs desired. A previous version of this
helper added magic prefixes; this is no longer the case.
Specify the keyword argument ``defer=True`` to enable the script
defer attribute.
Examples::
>>> print javascript_link('/javascripts/prototype.js', '/other-javascripts/util.js')
<script src="/javascripts/prototype.js" type="text/javascript"></script>
<script src="/other-javascripts/util.js" type="text/javascript"></script>
>>> print javascript_link('/app.js', '/test/test.1.js')
<script src="/app.js" type="text/javascript"></script>
<script src="/test/test.1.js" type="text/javascript"></script>
"""
convert_boolean_attrs(attrs, ["defer"])
tags = []
for url in urls:
tag = HTML.script("", type="text/javascript", src=url, **attrs)
tags.append(tag)
return literal("\n").join(tags)
示例2: source_js_url
# 需要導入模塊: from webhelpers2.html import HTML [as 別名]
# 或者: from webhelpers2.html.HTML import script [as 別名]
def source_js_url(self, url, **kwargs):
if not kwargs.pop('relative_path', False):
url = abs_static_url(url)
else:
url = static_url(url)
script_tag = HTML.script(type='text/javascript', src=url, **kwargs)
self.data['x-script-tags'].append(script_tag)
return u''
示例3: js_obfuscate
# 需要導入模塊: from webhelpers2.html import HTML [as 別名]
# 或者: from webhelpers2.html.HTML import script [as 別名]
def js_obfuscate(content):
"""Obfuscate data in a Javascript tag.
Example::
>>> js_obfuscate("<input type='hidden' name='check' value='valid' />")
literal(u'<script type="text/javascript">\\n//<![CDATA[\\neval(unescape(\\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%69%6e%70%75%74%20%74%79%70%65%3d%27%68%69%64%64%65%6e%27%20%6e%61%6d%65%3d%27%63%68%65%63%6b%27%20%76%61%6c%75%65%3d%27%76%61%6c%69%64%27%20%2f%3e%27%29%3b\\'))\\n//]]>\\n</script>')
"""
doc_write = "document.write('%s');" % content
obfuscated = ''.join(['%%%x' % ord(x) for x in doc_write])
complete = "eval(unescape('%s'))" % obfuscated
cdata = HTML.cdata("\n", complete, "\n//")
return HTML.script("\n//", cdata, "\n", type="text/javascript")
示例4: mail_to
# 需要導入模塊: from webhelpers2.html import HTML [as 別名]
# 或者: from webhelpers2.html.HTML import script [as 別名]
def mail_to(email_address, name=None, cc=None, bcc=None, subject=None,
body=None, replace_at=None, replace_dot=None, encode=None, **html_attrs):
"""Create a link tag for starting an email to the specified
``email_address``.
This ``email_address`` is also used as the name of the link unless
``name`` is specified. Additional HTML options, such as class or
id, can be passed in the ``html_attrs`` hash.
You can also make it difficult for spiders to harvest email address
by obfuscating them.
Examples::
>>> mail_to("[email protected]", "My email", encode = "javascript")
literal(u'<script type="text/javascript">\\n//<![CDATA[\\neval(unescape(\\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b\\'))\\n//]]>\\n</script>')
>>> mail_to("[email protected]", "My email", encode = "hex")
literal(u'<a href="mailto:%6d%[email protected]%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>')
You can also specify the cc address, bcc address, subject, and body
parts of the message header to create a complex e-mail using the
corresponding ``cc``, ``bcc``, ``subject``, and ``body`` keyword
arguments. Each of these options are URI escaped and then appended
to the ``email_address`` before being output. **Be aware that
javascript keywords will not be escaped and may break this feature
when encoding with javascript.**
Examples::
>>> mail_to("[email protected]", "My email", cc="[email protected]", bcc="[email protected]", subject="This is an example email", body= "This is the body of the message.")
literal(u'<a href="mailto:[email protected]?cc=ccaddress%40domain.com&bcc=bccaddress%40domain.com&subject=This%20is%20an%20example%20email&body=This%20is%20the%20body%20of%20the%20message.">My email</a>')
"""
extras = []
for item in ('cc', cc), ('bcc', bcc), ('subject', subject), ('body', body):
option = item[1]
if option:
if not isinstance(option, literal):
item = (item[0], escape(option))
extras.append(item)
options_query = urllib.urlencode(extras).replace("+", "%20")
protocol = 'mailto:'
email_address_obfuscated = email_address
if replace_at:
email_address_obfuscated = email_address_obfuscated.replace('@',
replace_at)
if replace_dot:
email_address_obfuscated = email_address_obfuscated.replace('.',
replace_dot)
if encode == 'hex':
email_address_obfuscated = HTML.literal(''.join(
['&#%d;' % ord(x) for x in email_address_obfuscated]))
protocol = HTML.literal(''.join(['&#%d;' % ord(x) for x in protocol]))
word_re = re.compile('\w')
encoded_parts = []
for x in email_address:
if word_re.match(x):
encoded_parts.append('%%%x' % ord(x))
else:
encoded_parts.append(x)
email_address = HTML.literal(''.join(encoded_parts))
url = HTML.literal(protocol + email_address)
if options_query:
url += HTML.literal('?') + options_query
html_attrs['href'] = url
tag = HTML.a(name or email_address_obfuscated, **html_attrs)
if encode == 'javascript':
tmp = "document.write('%s');" % tag
string = ''.join(['%%%x' % ord(x) for x in tmp])
return HTML.script(
HTML.literal("\n//<![CDATA[\neval(unescape('%s'))\n//]]>\n" % string),
type="text/javascript")
else:
return tag