本文整理汇总了Python中CommonMark.dumpAST方法的典型用法代码示例。如果您正苦于以下问题:Python CommonMark.dumpAST方法的具体用法?Python CommonMark.dumpAST怎么用?Python CommonMark.dumpAST使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CommonMark
的用法示例。
在下文中一共展示了CommonMark.dumpAST方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import CommonMark [as 别名]
# 或者: from CommonMark import dumpAST [as 别名]
def main():
parser = argparse.ArgumentParser(
description="Process Markdown according to "
"the CommonMark specification.")
if sys.version_info < (3, 0):
reload(sys) # noqa
sys.setdefaultencoding('utf-8')
parser.add_argument(
'infile',
nargs="?",
type=argparse.FileType('r'),
default=sys.stdin,
help="Input Markdown file to parse, defaults to STDIN")
parser.add_argument(
'-o',
nargs="?",
type=argparse.FileType('w'),
default=sys.stdout,
help="Output HTML/JSON file, defaults to STDOUT")
parser.add_argument('-a', action="store_true", help="Print formatted AST")
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
args = parser.parse_args()
parser = CommonMark.Parser()
f = args.infile
o = args.o
lines = []
for line in f:
lines.append(line)
data = "".join(lines)
ast = parser.parse(data)
if not args.a and not args.aj:
renderer = CommonMark.HtmlRenderer()
o.write(renderer.render(ast))
exit()
if args.a:
# print ast
CommonMark.dumpAST(ast)
exit()
# o.write(ast.to_JSON())
o.write(CommonMark.dumpJSON(ast))
exit()
示例2: exit
# 需要导入模块: import CommonMark [as 别名]
# 或者: from CommonMark import dumpAST [as 别名]
help="Input Markdown file to parse, defaults to STDIN")
parser.add_argument(
'-o',
nargs="?",
type=argparse.FileType('w'),
default=sys.stdout,
help="Output HTML/JSON file, defaults to STDOUT")
parser.add_argument('-a', action="store_true", help="Print formatted AST")
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
args = parser.parse_args()
parser = CommonMark.Parser()
f = args.infile
o = args.o
lines = []
for line in f:
lines.append(line)
data = "".join(lines)
ast = parser.parse(data)
if not args.a and not args.aj:
renderer = CommonMark.HtmlRenderer()
o.write(renderer.render(ast))
exit()
if args.a:
# print ast
CommonMark.dumpAST(ast)
exit()
# o.write(ast.to_JSON())
o.write(CommonMark.ASTtoJSON(ast))
exit()
示例3: test_dumpAST_orderedlist
# 需要导入模块: import CommonMark [as 别名]
# 或者: from CommonMark import dumpAST [as 别名]
def test_dumpAST_orderedlist(self):
md = '1.'
ast = Parser().parse(md)
CommonMark.dumpAST(ast)
示例4: _parse_node
# 需要导入模块: import CommonMark [as 别名]
# 或者: from CommonMark import dumpAST [as 别名]
def _parse_node(self, node):
children = node.children + node.inline_content
if node.t in ("ATXHeader", "SetextHeader"):
obj = Style(name="Heading {0}".format(node.level))
elif node.t == "Paragraph":
obj = Paragraph()
elif node.t == "Str":
obj = Text(text=node.c)
elif node.t in ("Softbreak", "Hardbreak"):
obj = Text(text=" ")
elif node.t == "Emph":
obj = Italic()
elif node.t == "Strong":
obj = Bold()
elif node.t == "Image":
caption = node.label[0].c if len(node.label) else ""
obj = Image(location=node.destination, caption=caption)
elif node.t == "HtmlBlock":
# Special case. Parse the HTML into instructions
instructions = self.html_parser.parse(node.string_content)
if not instructions:
obj = IgnoredOperation()
elif len(instructions) == 1:
# Only contains one instruction, carry on as normal
return instructions[0]
else:
# Lots of instructions. Return a group
return Group(instructions)
elif node.t == "List":
obj = BulletList() if node.list_data["type"] == "Bullet" else NumberedList()
elif node.t == "ListItem":
obj = ListElement()
elif node.t == "Link":
obj = HyperLink(location=node.destination)
children.extend(node.label)
elif node.t in ("ReferenceDef", "HorizontalRule"):
# ToDo: handle markdown references
obj = IgnoredOperation()
elif node.t == "Code":
# Need an inline code object
obj = InlineCode([Text(text=node.c)])
elif node.t == "IndentedCode":
obj = CodeBlock([Text(text=node.string_content)])
else:
CommonMark.dumpAST(node)
raise ParseException("Cannot process node type {0}".format(node.t))
if isinstance(node.c, list):
children = children + node.c
for child in children:
result = self._parse_node(child)
if result is None:
continue
obj.add_child(result)
return obj
示例5: raw_input
# 需要导入模块: import CommonMark [as 别名]
# 或者: from CommonMark import dumpAST [as 别名]
while True:
s = ""
while True:
inp = raw_input(colors.OKBLUE+"Markdown: "+colors.ENDC)
if not inp == "end" and not inp == "quit":
s += inp+"\n"
elif inp == "end":
s = s[:-1]
break
elif inp =="quit":
print(colors.HEADER+"bye!"+colors.ENDC)
exit(0)
ast = parser.parse(s)
html = renderer.render(ast)
print(colors.WARNING+"="*10+"AST====="+colors.ENDC)
CommonMark.dumpAST(ast)
print(colors.WARNING+"="*10+"HTML===="+colors.ENDC)
print(html)
# some tests?
if args.t:
tests = args.t.split(",")
choice_examples = []
for t in tests:
if not t == "" and len(examples) > int(t):
choice_examples.append(examples[int(t)-1])
examples = choice_examples
# all tests
for i, example in enumerate(examples): # [0,examples[0]]