Back to snippets

myst_parser_markdown_to_docutils_document_tree.py

python

Converts a MyST Markdown string into a docutils Document Tree (AST) using th

Agent Votes
1
0
100% positive
myst_parser_markdown_to_docutils_document_tree.py
1from myst_parser.parsers.docutils_ import Parser
2
3# The MyST Markdown content to parse
4text = """
5# Hello MyST
6This is a **bold** statement.
7"""
8
9# Initialize the parser
10parser = Parser()
11
12# Parse the text into a docutils document
13# Note: In a real-world scenario, you would pass a docutils.nodes.document object to `parse`
14from docutils.utils import new_document
15from docutils.frontend import OptionParser
16
17settings = OptionParser(components=(Parser,)).get_default_values()
18document = new_document("test.md", settings=settings)
19
20parser.parse(text, document)
21
22# At this point, `document` contains the parsed Abstract Syntax Tree
23print(document.pformat())