本文整理汇总了Python中mistune.escape函数的典型用法代码示例。如果您正苦于以下问题:Python escape函数的具体用法?Python escape怎么用?Python escape使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了escape函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: image
def image(self, src, title, text):
"""Rendering a image with title and text
:param src: source link of the image with optional dimensions qualifier in format ' =<width>x[height]'.
:param title: title text of the image.
:param text: alt text of the image.
"""
if src.startswith('javascript:'):
src = ''
dimensions = ''
if ' ' in src:
src, ext = src.split(' ', 1)
size = re.search(r"=(?P<width>\d+)x(?P<height>\d*)", ext)
if size:
dimensions = ' width="%d"' % int(size.group('width'))
if size.group('height'):
dimensions += ' height="%d"' % int(size.group('height'))
text = mistune.escape(text, quote=True)
if title:
title = ' title="%s"' % mistune.escape(title, quote=True)
else:
title = ''
html = '<img src="%s" alt="%s"%s%s />' % (src, text, title, dimensions)
if dimensions:
html = '<a href="%s">%s</a>' % (src, html)
return html
示例2: block_html
def block_html(self, html):
if self.texoid and html.startswith('<latex'):
attr = html[6:html.index('>')]
latex = html[html.index('>') + 1:html.rindex('<')]
latex = self.parser.unescape(latex)
result = self.texoid.get_result(latex)
if not result:
return '<pre>%s</pre>' % mistune.escape(latex, smart_amp=False)
elif 'error' not in result:
img = ('''<img src="%(svg)s" onerror="this.src='%(png)s';this.onerror=null"'''
'width="%(width)s" height="%(height)s"%(tail)s>') % {
'svg': result['svg'], 'png': result['png'],
'width': result['meta']['width'], 'height': result['meta']['height'],
'tail': ' /' if self.options.get('use_xhtml') else ''
}
style = ['max-width: 100%',
'height: %s' % result['meta']['height'],
'max-height: %s' % result['meta']['height'],
'width: %s' % result['meta']['height']]
if 'inline' in attr:
tag = 'span'
else:
tag = 'div'
style += ['text-align: center']
return '<%s style="%s">%s</%s>' % (tag, ';'.join(style), img, tag)
else:
return '<pre>%s</pre>' % mistune.escape(result['error'], smart_amp=False)
return super(AwesomeRenderer, self).block_html(html)
示例3: block_code
def block_code(self, code, lang=""):
code = code.rstrip('\n')
if not lang:
code = mistune.escape(code, smart_amp=False)
return '<pre><code>%s\n</code></pre>\n' % code
code = mistune.escape(code, quote=True, smart_amp=False)
return '<pre><code class="hljs %s">%s\n</code></pre>\n' % (lang, code)
示例4: block_code
def block_code(self, code, lang):
html = ""
if lang:
html = '\n<div class="highlight"><pre><code class="%s">%s</code></pre></div>\n' % \
(lang, mistune.escape(code))
return html
html = '\n<div class="highlight"><pre><code>%s</code></pre></div>\n' % mistune.escape(code)
return html
示例5: parse_image_link
def parse_image_link(self, m):
link = mistune.escape(m.group(0).strip(), quote=True)
title = mistune.escape(m.group('image_name').strip(), quote=True)
self.tokens.append({
'type': 'image_link',
'src': link,
'title': title,
'text': title
})
示例6: block_code
def block_code(self, code, lang=None) -> str:
if not lang:
return '\n<pre><code>{:s}</code></pre>\n'.format(mistune.escape(code))
try:
lexer = get_lexer_by_name(lang, stripall=False)
except ClassNotFound:
return '\n<pre><code>{:s}</code></pre>\n'.format(mistune.escape(code))
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)
示例7: image
def image(self, src, title, text):
if src.startswith('javascript:'):
src = ''
text = mistune.escape(text, quote=True)
if title:
title = mistune.escape(title, quote=True)
html = '<img class="img-responsive center-block" src="%s" alt="%s" title="%s"' % (src, text, title)
else:
html = '<img class="img-responsive center-block" src="%s" alt="%s"' % (src, text)
if self.options.get('use_xhtml'):
return '%s />' % html
return '%s>' % html
示例8: block_code
def block_code(text, lang, inlinestyles=False, linenos=False):
if not lang:
text = text.strip()
return u"<pre><code>%s</code></pre>\n" % mistune.escape(text)
try:
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter(noclasses=inlinestyles, linenos=linenos)
code = highlight(text, lexer, formatter)
if linenos:
return '<div class="highlight-wrapper">%s</div>\n' % code
return code
except:
return '<pre class="%s"><code>%s</code></pre>\n' % (lang, mistune.escape(text))
示例9: inline_html
def inline_html(self, html):
"""Rendering span level pure html content.
:param html: text content of the html snippet.
"""
# use beautiful soup to parse and read element name, attributes
soup = BeautifulSoup(html, 'lxml')
# only expect one element here
# NOTE: using xml parser had inconsistent results; using lxml
# wraps contents inside html body tags, so access content there
element = soup.html.body.contents[0]
text_content = element.string or ''
if element.name in ['i', 'em']:
return '<emph rend="italic">%s</emph>' % text_content
if element.name in ['b', 'strong']:
return '<emph rend="bold">%s</emph>' % text_content
if element.name == 'a':
# convert name anchor to <anchor xml:id="###"/>
# **preliminary** (anchor not valid in all contexts)
if element.get('name', None) or element.get('id', None):
el_id = element.get('id', None) or element.get('name', None)
return '<anchor xml:id="%s">%s</anchor>' % \
(el_id, text_content)
if self.options.get('escape'):
return mistune.escape(html)
return html
示例10: image
def image(self, src, title, text):
"""Rendering a image with title and text.
:param src: source link of the image.
:param title: title text of the image.
:param text: alt text of the image.
"""
if src.startswith('javascript:'):
src = ''
text = mistune.escape(text, quote=True)
# markdown doesn't include mimetype, but it's required in TEI
# infer mimetype based on image suffix in the url
image_suffix = (src.rsplit('.', 1)[-1])
mimetype = "image/*"
if image_suffix in ['gif', 'png', 'jpeg']:
mimetype = 'image/%s' % image_suffix
elif image_suffix == 'jpg':
mimetype = 'image/jpeg'
tag = '<media mimetype="%s" url="%s">' % (mimetype, src)
if title or text:
desc_parts = ['<desc>']
if title:
desc_parts.append('<head>%s</head>' % title)
if text:
desc_parts.append('<p>%s</p>' % text)
desc_parts.append('</desc>')
tag += ''.join(desc_parts)
tag += '</media>'
return tag
示例11: codespan
def codespan(self, text):
"""Rendering inline `code` text.
:param text: text content for inline code.
"""
text = mistune.escape(text.rstrip(), smart_amp=False)
return '<code>%s</code>' % text
示例12: block_code
def block_code(self, code, lang=None):
code = code.rstrip('\n')
code = mistune.escape(code, smart_amp=False)
res = '<pre class="cn literal-block" id="cn%s">%s\n</pre>\n' % (
self.cn, code)
self.cn += 1
return res
示例13: block_code
def block_code(self, code, lang=None):
if not lang:
return '\n<pre><code>{code}</code></pre>\n'.format(code=mistune.escape(code))
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter(noclasses=True)
text = highlight(code, lexer, formatter)
return text.replace('class="highlight" ', '')
示例14: block_code
def block_code(self, code, lang):
if not lang:
return '\n<pre><code>%s</code></pre>\n' % \
mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)
示例15: embed
def embed(self, link, title, text):
if link.startswith('javascript:'):
link = ''
if not title:
return '<a href="%s" class="embed">%s</a>' % (link, text)
title = mistune.escape(title, quote=True)
return '<a href="%s" title="%s" class="embed">%s</a>' % (link, title, text)