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
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)
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)
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
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 })
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)
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
def block_code(self, code, lang=None): """Rendering block level code. ``pre > code``. :param code: text content of the code block. :param lang: language of the given code. """ code = code.rstrip('\n') if not lang: code = escape(code, smart_amp=False) return '<pre><code>%s\n</code></pre>\n' % code code = escape(code, quote=True, smart_amp=False) return '<pre><code class="lang-%s">%s\n</code></pre>\n' % (lang, code)
def block_code(self, code, lang): code = code.replace(' ', ' ') if not lang: return '\n<pre><code class="highlight">%s</code></pre>\n' % \ mistune.escape(code) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(code, lexer, formatter) except: return '\n<pre><code class="highlight">%s</code></pre>\n' % \ mistune.escape(code)
def block_code(self, code, lang): if not lang: return '\n<pre><code>%s</code></pre>\n' % \ mistune.escape(code) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter() text = highlight(code, lexer, formatter) except Exception as e: text = '\n<pre><code>%s</code></pre>\n' % \ mistune.escape(code) return text
def image(self, src, title, text): src = mistune.escape_link(src) text = mistune.escape(text, quote=True) if title: title = mistune.escape(title, quote=True) html = f'<img class="pure-img" src="{src}" alt="{text}" title="{title}" ' else: html = f'<img class="pure-img" src="{src}" alt="{text}" ' if self.options.get("use_xhtml"): return f"{html} />" return f"{html} >"
def block_code(self, code, lang): preCode = '\n<div class="sourceCode"><pre class="sourceCode">' postCode = '</pre></div>\n' if not lang: code = '<code class="sourceCode">{}</code>'.format(mt.escape(code)) return preCode + code + postCode else: preCode = '\n<div class="sourceCode"><pre class="sourceCode">' postCode = '</pre></div>\n' code = '<code class="sourceCode {}">{}</code>'.format( lang, mt.escape(code)) return preCode + code + postCode
def image(self, src, title, text, attribute=None): """Image rendering for assets""" asset_slug, args = self._asset_url_helper(src) if asset_slug is not None: asset = Asset.objects.filter(slug=asset_slug).first() if asset is not None: if asset.type == "video": return self.video(src, title, text) size = args.get('size', None) crop = args.get('crop', None) quality = args.get('quality', 99) asset = ImageAsset.objects.filter(slug=asset.slug).first() if asset is None: return super().image(src, title, text) if size is not None: image_asset = get_thumbnail(asset.asset, size, crop=crop, quality=quality) else: image_asset = asset.asset src = image_asset.url if not src: src = '' if title is None or len(title) == 0: title = asset.title if text is None or len(text) == 0: text = asset.description else: # - Check if Video asset if src.lower().split('.')[-1] in _VIDEO_EXTENSIONS: return self.video(src, title, text, attribute=attribute) if not attribute: attribute = "image-fluid" # - Render image HTML src = mistune.escape_link(src) text = mistune.escape(text, quote=True) if title: title = mistune.escape(title, quote=True) html = '<img src="%s" alt="%s" title="%s"' % (src, text, title) else: html = '<img src="%s" alt="%s"' % (src, text) if attribute: html = '%s class="%s"' % (html, attribute) if self.options.get('use_xhtml'): return '%s />' % html return '%s>' % html
def ref(self, match): try: post = Post.objects.get(pk=match.group(1)) except Post.DoesNotExist: return '<span class="broken-quote">{}</span>'.format( mistune.escape(match.group(0)) ) return '<a href="{}" class="ref" data-post-id="{}">{}</a>'.format( post.get_absolute_url(), match.group(1), mistune.escape(match.group(0)) )
def footnote_item(self, key, text): """Rendering a footnote item. :param key: identity key for the footnote. :param text: text content of the footnote. """ back = ('<a href="#fnref-%s" rev="footnote">↩</a>') % escape(key) text = text.rstrip() if text.endswith('</p>'): text = re.sub(r'<\/p>$', r'%s</p>' % back, text) else: text = '%s<p>%s</p>' % (text, back) html = '<li id="fn-%s">%s</li>\n' % (escape(key), text) return html
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))
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
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. """ src = mistune.escape_link(src) text = mistune.escape(text, quote=True) if title: title = mistune.escape(title, quote=True) html = '<img src="%s" class="img-responsive" alt="%s" title="%s"' % (src, text, title) else: html = '<img src="%s" class="img-responsive" alt="%s"' % (src, text) return '%s />' % html
def block_code(text, lang, inline_styles=False, linenos=False): if not lang: text = text.strip() return u'<pre><code>%s</code></pre>\n' % escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter(noclasses=inline_styles, linenos=linenos) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight">%s</div>\n' % code return code except: return '<pre class="%s"><code>%s</code></pre>\n' % (lang, escape(text))
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
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
def block_code(self, code, language=None): if not language: return "\n<pre><code>{}</code></pre>\n".format( mistune.escape(code)) lexer = get_lexer_by_name(language, stripall=True) formatter = html.HtmlFormatter() return highlight(code, lexer, formatter)
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)
def block_code(self, code, lang): if not lang: return '\n<div class="highlight"><pre><code>%s</code></pre></div>\n' % \ mistune.escape(code) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter(noclasses=True, style="monokai") return highlight(code, lexer, formatter)
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" ', '')
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. """ allow_local_embed = self.options.get('embed_local_images', False) use_xhtml = self.options.get('use_xhtml') if title: title = mistune.escape(title, quote=True) attributes = { 'alt': text, } if title: attributes['title'] = title html = self.__ie.get_embedded_image(src, use_xhtml=use_xhtml, attributes=attributes, allow_local=allow_local_embed) return html
def _clean_poll(self): name_raw = self.data['name'] title_raw = self.data['title'] min_raw = self.data['min'] max_raw = self.data['max'] close_at_raw = self.data['close'] mode_raw = self.data['mode'] poll = { 'name': name_raw[:self._field_name.max_length] } if title_raw: title = mistune.escape(title_raw.strip(), quote=True) poll['title'] = title[:self._field_title.max_length] # May be empty if min_raw: poll['choice_min'] = int(min_raw) if max_raw: poll['choice_max'] = int(max_raw) if close_at_raw: days = int(close_at_raw[:self.close_max_len]) poll['close_at'] = timezone.now() + timezone.timedelta(days=days) if mode_raw: poll['mode'] = PollMode.BY_NAME[mode_raw] self.cleaned_data['poll'] = poll
def block_code(text, lang, inlinestyles=False, linenos=False): if not lang: text = text.strip() return '<pre><code>%s</code></pre>\n' % mistune.escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = BlogHtmlFormatter(noclasses=inlinestyles, linenos=linenos, cssclass='highlight %s' % lang, lang=lang) code = highlight(text, lexer, formatter) return code except Exception: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text))
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
def block_code(self, code, lang=None): if lang and lang != 'cmd' and lang != 'yml' and lang != 'conf' and lang != 'sbtshell' and lang != 'commandline' and lang != 'shell script': lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter() return highlight(code, lexer, formatter) return '<pre><code class="language-plaintext">' + mistune.escape( code) + '</code></pre>'
def codespan(self, text): """Rendering inline `code` text. :param text: text content for inline code. """ text = escape(text.rstrip(), smart_amp=False) return '<code>%s</code>' % text
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 = html.HtmlFormatter() return highlight(code, lexer, formatter)
def block_code(self, text, lang): if not lang: return u'\n<pre><code>%s</code></pre>\n' % \ mistune.escape(text.strip()) lexer = get_lexer_by_name(lang, stripall=True) formatter = CodeHtmlFormatter() return highlight(text, lexer, formatter)
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. """ src = escape_link(src) text = escape(text, quote=True) if title: title = escape(title, quote=True) html = '<img src="%s" alt="%s" title="%s"' % (src, text, title) else: html = '<img src="%s" alt="%s"' % (src, text) if self.options.get('use_xhtml'): return '%s />' % html return '%s>' % html
def link(self, link, title, text): link = mistune.escape_link(link) if not title: return '<a href="%s"%s>%s</a>' % (link, self._link_rel(link), text) title = mistune.escape(title, quote=True) return '<a href="%s" title="%s"%s>%s</a>' % ( link, title, self._link_rel(link), text)
def inline_html(self, html): """Rendering span level pure html content. :param html: text content of the html snippet. """ if self.options.get('escape'): return [escape(html)] return [html]
def block_code(self, code, lang=None): if lang: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter(noclasses=True) return highlight(code, lexer, formatter) return '<div style="background-color: #f8f8f8;"><pre>' + mistune.escape( code) + '</pre></div>'
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
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)
def block_code(self, code, language=None): if language: lexer = get_lexer_by_name(language, stripall=True) formatter = pygments_html.HtmlFormatter() highlighted = highlight(code, lexer, formatter) return highlighted return '<pre><code>' + mistune.escape(code) + '</code></pre>'
def autolink(self, link, is_email=False): text = link = escape(link) if is_email: link = 'mailto:%s' % link ltype = 'email' else: ltype = 'link' return ltype, link, text
def block_code(self, code, lang=None): code = code.rstrip('\n') if not lang: code = mistune.escape(code, smart_amp=False) return '<pre><code>{0}\n</code></pre>\n'.format(code) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter(cssclass='highlight ' + lang) return highlight(code, lexer, formatter)
def block_code(self, code, lang): if not lang: return "\n<pre><code>{}</code></pre>\n".format( mistune.escape(code) ) lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter() return highlight(code, lexer, formatter)
def text(self, text): """Rendering unformatted text. :param text: text content. """ if self.options.get('parse_block_html'): return text return escape(text)
def block_code(self, code, lang): if lang: try: lexer = pygments.lexers.get_lexer_by_name(lang, stripall=True) formatter = pygments.formatters.html.HtmlFormatter() return pygments.highlight(code, lexer, formatter) except pygments.util.ClassNotFound: pass return '\n<pre><code>%s</code></pre>\n' % mistune.escape(code)
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) if title: title = mistune.escape(title, quote=True) html = '<img class="img-responsive" src="%s" alt="%s" title="%s"' % (src, text, title) else: html = '<img class="img-responsive" src="%s" alt="%s"' % (src, text) if self.options.get('use_xhtml'): return '%s />' % html return '%s>' % html
def block_code(self, code, lang): if not lang: return '<pre><code>{0}</code></pre>'.format(escape(code)) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter( linenos='table', style='railscasts', cssclass='b-codeblock') return highlight(code, lexer, formatter)
def autolink(self, link, is_email=False): text = link = escape(link) if is_email: link = 'mailto:%s' % link if not link: link = "#" site = Site.objects.get_current() nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'" return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
def link(self, link, title, text): link = escape_link(link) site = Site.objects.get_current() nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'" if not link: link = "#" if not title: return '<a href="%s" %s>%s</a>' % (link, nofollow, text) title = escape(title, quote=True) return '<a href="%s" title="%s" %s>%s</a>' % (link, title, nofollow, text)
def block_code(self, code, lang): escaped = mistune.escape(re.sub("\n*$", "", code)) replaced = re.sub("<em>", "<em>", escaped) replaced = re.sub("</em>", "</em>", replaced) if lang == "comment": start_tag = "<pre class=\"comment\">" else: start_tag = "<pre>" return start_tag + replaced + "</pre>"
def block_code(self, code, lang): lexer = None if lang: try: lexer = get_lexer_by_name(lang, stripall = True) except: print("failed to load lexer for language '{}'".format(lang)) if not lexer: return '\n<pre class="plaincode"><code>{}</code></pre>\n'.format(mistune.escape(code.strip())) formatter = HtmlFormatter() return '\n<div class="highlight-code">{}</div>\n'.format(highlight(code, lexer, formatter))
def block_code(self, code, lang): if lang: try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: code = lang + "\n" + code lang = None if not lang: return "\n<pre><code>%s</code></pre>\n" % mistune.escape(code) formatter = HtmlFormatter() return highlight(code, lexer, formatter)
def block_code(self, code, lang): ####################### # Interactive mode ####################### if options.interactive: print code yn = raw_input("execute next? (Yes/No/Skip)") if yn.lower() == "n" or yn.lower() == "no": sys.exit() elif yn.lower() == "s" or yn.lower() == "skip": return "Skipped" ################################# # lookahead : hint for file name # kv : replaceable dictionary ################################# output = executor[lang](code=code, lookahead=LOOKAHEAD, kv=KV) # output is dictionary # {'input':input_code, 'output': output_code, 'error':error_if_exist} if type(output) != dict: # This is error of pre-processing logging.error(output) return '<pre><code class="lang-bash">%s</code></pre>\n' % (lang, output) result = "" if output.has_key("input") and (output["input"] != None and output["input"] != ""): added = output["input"] added = added.rstrip("\n") if not lang: added = mistune.escape(added, smart_amp=False) return "<pre><code>%s\n</code></pre>\n" % added added = mistune.escape(added, quote=True, smart_amp=False) result = result + "<b>Input:</b></br>" result = result + '<pre><code class="lang-%s">%s\n</code></pre>\n' % (lang, added) if output.has_key("output") and (output["output"] != None and output["output"] != ""): logging.debug(output["output"]) added = output["output"] added = added.rstrip("\n") if not lang: added = mistune.escape(added, smart_amp=False) return "<pre><code>%s\n</code></pre>\n" % added added = mistune.escape(added, quote=True, smart_amp=False) result = result + "<b>Output:</b></br>" result = result + '<pre><code class="lang-%s">%s\n</code></pre>\n' % (lang, added) if output.has_key("error") and (output["error"] != None and output["error"] != ""): logging.error(output["error"]) added = output["error"] added = added.rstrip("\n") if not lang: added = mistune.escape(added, smart_amp=False) return "<pre><code>%s\n</code></pre>\n" % added added = mistune.escape(added, quote=True, smart_amp=False) result = result + "<b>Error:</b></br>" result = result + '<pre><code class="lang-%s">%s\n</code></pre>\n' % (lang, added) return result
def link(self, link, title, text): """Rendering a given link with content and title. :param link: href link for ``<a>`` tag. :param title: title content for `title` attribute. :param text: text content for description. """ if link.startswith('javascript:'): link = '' attr = '' if title: attr = ' n="%s"' % mistune.escape(title, quote=True) return '<ref target="%s"%s>%s</ref>' % (link, attr, text)
def block_code(self, code, lang): 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( linenos='table', linenospecial=4, lineseparator='<br>', lineanchors='code-line-link', linespans='code-line-span', anchorlinenos='lineno-line' ) return highlight(code, lexer, formatter)