"""MyST Markdown parser for sphinx."""from__future__importannotationsfromdocutilsimportnodesfromdocutils.parsers.rstimportParserasRstParserfromsphinx.parsersimportParserasSphinxParserfromsphinx.utilimportloggingfrommyst_parser.config.mainimport(MdParserConfig,TopmatterReadError,merge_file_level,read_topmatter,)frommyst_parser.mdit_to_docutils.sphinx_importSphinxRendererfrommyst_parser.mdit_to_docutils.transformsimport(CollectFootnotes,ResolveAnchorIds,SortFootnotes,)frommyst_parser.parsers.mditimportcreate_md_parserfrommyst_parser.warnings_importcreate_warningSPHINX_LOGGER=logging.getLogger(__name__)
[文档]classMystParser(SphinxParser):"""Sphinx parser for Markedly Structured Text (MyST)."""supported:tuple[str,...]=("md","markdown","myst")"""Aliases this parser supports."""settings_spec=RstParser.settings_spec"""Runtime settings specification. Defines runtime settings and associated command-line options, as used by `docutils.frontend.OptionParser`. This is a concatenation of tuples of: - Option group title (string or `None` which implies no group, just a list of single options). - Description (string or `None`). - A sequence of option tuples """config_section="myst parser"config_section_dependencies=("parsers",)translate_section_name=None
[文档]defparse(self,inputstring:str,document:nodes.document)->None:"""Parse source text. :param inputstring: The source string to parse :param document: The root docutils node to add AST elements to """# get the global configconfig:MdParserConfig=document.settings.env.myst_config# update the global config with the file-level configtry:topmatter=read_topmatter(inputstring)exceptTopmatterReadError:pass# this will be reported during the renderelse:iftopmatter:warning=lambdawtype,msg:create_warning(# noqa: E731document,msg,wtype,line=1,append_to=document)config=merge_file_level(config,topmatter,warning)parser=create_md_parser(config,SphinxRenderer)parser.options["document"]=documentparser.render(inputstring)