Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_use_xhtml():
ret = mistune.markdown('foo\n\n----\n\nbar')
assert '<hr>' in ret
ret = mistune.markdown('foo\n\n----\n\nbar', use_xhtml=True)
assert '<hr>' in ret
ret = mistune.markdown('foo \nbar', use_xhtml=True)
assert '<br>' in ret
ret = mistune.markdown('![foo](bar "title")', use_xhtml=True)
assert '<img title="title" alt="foo" src="bar">' in ret
def run():
with open('test/profiler/syntax.md', 'r') as fin:
mistune.markdown(fin.read())
def parse_markdown(self, text, strip_tag=''):
"""Render the given text to Markdown
Optional paramaters:
- strip_paragraph_tags: Allows removal of given tags
"""
html = mistune.markdown(text,
escape=False,
hard_wrap=False,
parse_block_html=False,
parse_inline_html=False,
use_xhtml=False)
if strip_tag:
start_tag = '<{}>'.format(strip_tag)
end_tag = ''.format(strip_tag)
html = html.replace(start_tag, '')
html = html.replace(end_tag, '')
return html
def markdown_preprocess(markdown_content):
"""Preprocess content written in Markdown language."""
readme_rendered = mistune.markdown(markdown_content, escape=False)
soup = BeautifulSoup(readme_rendered, "html.parser")
# Replace anchors with content where relevant and extract otherwise
for link in soup.findAll('a'):
if link.text.startswith('http'):
link.extract()
else:
link.replaceWithChildren()
# Remove all the images
for image in soup.findAll('img'):
image.extract()
# Remove all the code blocks
for code_block in soup.findAll('code'):
code_block.extract()
return soup.text
if f["type"] == "doc":
f["doc"] = number_headings(self.toc, f["doc"])
if "extends" in f:
doc += "\n\nExtends [%s](#%s)" % (f["extends"], to_id(f["extends"]))
if f["name"] in self.subs:
doc += "\n\nExtended by"
doc += ", ".join([" [%s](#%s)" % (s, to_id(s)) for s in self.subs[f["name"]]])
if f["name"] in self.uses:
doc += "\n\nReferenced by"
doc += ", ".join([" [%s.%s](#%s)" % (s[0], s[1], to_id(s[0])) for s in self.uses[f["name"]]])
doc = doc + "\n\n" + f["doc"]
doc = mistune.markdown(doc, renderer=MyRenderer())
if f["type"] == "record":
doc += "<h3>Fields</h3>"
doc += """"""
doc += ""
for i in f["fields"]:
doc += ""
tp = i["type"]
if isinstance(tp, list) and tp[0] == "null":
opt = False
tp = tp[1:]
else:
opt = True
doc += "" % (i["name"], typefmt(tp), opt, mistune.markdown(i["doc"]))
doc += ""
doc += """<table class="table table-striped"><tbody><tr><th>field</th><th>type</th><th>required</th><th>description</th></tr><tr><td><code>%s</code></td><td>%s</td><td>%s</td><td>%s</td></tr></tbody></table>"""
def create(cls, author, raw_comment, parent):
"""
Create a new comment instance. If the parent is submisison
update comment_count field and save it.
If parent is comment post it as child comment
:param author: RedditUser instance
:type author: RedditUser
:param raw_comment: Raw comment text
:type raw_comment: str
:param parent: Comment or Submission that this comment is child of
:type parent: Comment | Submission
:return: New Comment instance
:rtype: Comment
"""
html_comment = mistune.markdown(raw_comment)
# todo: any exceptions possible?
comment = cls(author=author,
author_name=author.user.username,
raw_comment=raw_comment,
html_comment=html_comment)
if isinstance(parent, Submission):
submission = parent
comment.submission = submission
elif isinstance(parent, Comment):
submission = parent.submission
comment.submission = submission
comment.parent = parent
else:
return
submission.comment_count += 1
Set the list in the adequate format for the template
'''
# Compose the list of articles
ARTICLE_TEMPLATE = '* **{title}** {summary}: {link}'
article_list = [ARTICLE_TEMPLATE.format(title=title, summary=summary,
link=link)
for title, summary, link in articles]
data = {
'article_list': '\n'.join(article_list),
'keywords': ', '.join(keywords),
'feed_list': ', '.join(feed_list),
}
text = EMAIL_TEMPLATE.format(**data)
html_content = mistune.markdown(text)
html = jinja2.Template(EMAIL_STYLING).render(content=html_content)
return text, html
def normalize_post_text(text, charmap=None):
# footer
# normalized = re.sub(r'(.+)\n---\n.+', r'\1', text, flags=re.DOTALL)
# quotes
normalized = re.sub(r'^>.+\n?.+\n\n', '', text, flags=re.M)
# derived from https://stackoverflow.com/a/761847
# stripping markdown syntax
html = mistune.markdown(normalized)
normalized = ''.join(BeautifulSoup(html, "html5lib").findAll(text=True)).strip()
# whitespace html entity
normalized = re.sub(r' ', ' ', normalized)
# ^() syntax
normalized = re.sub(r'\^\(([^\^]+)\)', r'\1', normalized)
normalized = re.sub(r'(^|\s|\w+|[/,|])\^(\s|\w+|[/,|])', r'\1\2', normalized)
normalized = re.sub(r'[\u2018\u2019]', "'", normalized.replace("\u2605", "*"))
normalized = re.sub(r'[\u201C\u201D]', '"', normalized)
# consecutive whitespaces
normalized = re.sub(r'\s+', ' ', normalized)
# users and subreddit mentions