本文整理匯總了Python中commonmark.Parser方法的典型用法代碼示例。如果您正苦於以下問題:Python commonmark.Parser方法的具體用法?Python commonmark.Parser怎麽用?Python commonmark.Parser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類commonmark
的用法示例。
在下文中一共展示了commonmark.Parser方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def __init__(self):
self.path = os.path.join(settings.BASE_DIR, "NEWS.md")
self.parser = commonmark.Parser()
self.renderer = commonmark.HtmlRenderer()
self.cookie_name = "news_current"
示例2: count_specified_tag
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def count_specified_tag(contents, tag):
"""Count the specified markdown tag in the string contents."""
ast = commonmark.Parser().parse(contents)
tag_count = 0
# iteratively check all of the nodes in the AST of the markdown file
for subnode, enter in ast.walker():
# check to see if the current subnode is an open node of the specified tag
if subnode.t == tag and enter:
tag_count += 1
return tag_count
# pylint: disable=bad-continuation
示例3: get_paragraphs
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def get_paragraphs(contents):
"""Retrieve the paragraphs in the writing in the contents parameter."""
ast = commonmark.Parser().parse(contents)
paragraph_content = constants.markers.Nothing
mode_looking = True
paragraph_list = []
counter = 0
# iterate through the markdown to find paragraphs and add their contents to paragraph_list
for subnode, enter in ast.walker():
if mode_looking:
# check to see if the current subnode is an open paragraph node
if counter == 1 and subnode.t == constants.markdown.Paragraph and enter:
# initialize paragraph_content
paragraph_content = constants.markers.Nothing
# stop search for paragraph nodes, as one has been found
# instead, start adding content to paragraph_content
mode_looking = False
else:
# check to see if the current subnode is a closing paragraph node
if counter == 2 and subnode.t == constants.markdown.Paragraph and not enter:
# add the content of the paragraph to paragraph_list
paragraph_list.append(paragraph_content.strip())
# stop saving paragraph contents, as the paragraph had ended
# start a search for a new paragraph
mode_looking = True
# if the subnode literal has contents,
# or it is a softbreak, add them to paragraph_content
if subnode.t == constants.markdown.Softbreak:
paragraph_content += constants.markers.Newline
elif subnode.literal is not None:
paragraph_content += subnode.literal
# track the how deep into the tree the search currently is
if subnode.is_container():
if enter:
counter += 1
else:
counter -= 1
# return the list of the collected paragraphs
return paragraph_list
示例4: render_markdown_instead_of_escaping
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def render_markdown_instead_of_escaping(parser, token):
class Node(template.Node):
def __init__(self, variable_name):
self.variable = template.Variable(variable_name)
def render(self, context):
md = self.variable.resolve(context)
if not context.autoescape:
# Auto-escaping is off, so we're in the text portion
# of a notification email. Return the raw markdown.
return md
else:
# Auto-escaping is on, so we're in the HTML portion
# of a notification email. Rather than returning the
# raw Markdown, which will look funny because e.g.
# line breaks will be ignored when it is placed within
# HTML, render the Markdown to HTML. Turn on safe mode
# since the content can't be trusted.
import commonmark
return commonmark.HtmlRenderer({ "safe": True })\
.render(commonmark.Parser().parse(md))
try:
tag_name, variable_name = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError(
"%r tag requires a single argument naming a variable" % token.contents.split()[0]
)
return Node(variable_name)
示例5: render_text
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def render_text(text, autocompletes=None, comment=None, unwrap_p=False):
# Render comment text into HTML.
import re
# Put @-mentions in bold.
if autocompletes:
text, _ = match_autocompletes(text, autocompletes,
lambda text : "**" + text + "**")
# Rewrite attachment:### URLs.
if comment is not None:
def get_attachment_url(attachment_id):
try:
return Attachment.objects.get(id=attachment_id.group(1)).get_absolute_url()
except:
return "about:blank"
text = re.sub("(?<=\()attachment:(\d+)(?=\))", get_attachment_url, text)
# Render to HTML as if CommonMark.
import commonmark
parsed = commonmark.Parser().parse(text)
text = commonmark.HtmlRenderer({ "safe": True }).render(parsed)
if unwrap_p:
# If it's a single paragraph, unwrap it.
text = re.sub(r"^<p>(.*)</p>$", r"\1", text)
return text
示例6: parse
# 需要導入模塊: import commonmark [as 別名]
# 或者: from commonmark import Parser [as 別名]
def parse(self, inputstring, document):
self.document = document
self.current_node = document
self.config = self.default_config.copy()
try:
new_cfg = self.document.settings.env.config.recommonmark_config
self.config.update(new_cfg)
except AttributeError:
pass
self.setup_parse(inputstring, document)
self.setup_sections()
parser = Parser()
ast = parser.parse(inputstring + '\n')
self.convert_ast(ast)
self.finish_parse()