2525from sphinx .util import logging as sphinx_logging
2626from sphinx .util .docutils import SphinxTranslator
2727
28- from myst_nb ._compat import findall
28+ from myst_nb ._compat import findall , get_env_app
2929from myst_nb .core .config import NbParserConfig
3030from myst_nb .core .execute import ExecutionResult , create_client
3131from myst_nb .core .loggers import DEFAULT_LOG_TYPE , SphinxDocLogger
@@ -70,17 +70,20 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
7070 :param inputstring: The source string to parse
7171 :param document: The root docutils node to add AST elements to
7272 """
73- assert self .env is not None , "env not set"
74- document_path = self .env .doc2path (self .env .docname )
73+ env = getattr (document .settings , "env" , None )
74+ if env is None :
75+ env = self .env
76+ assert env is not None , "env not set"
77+ document_path = env .doc2path (env .docname )
7578
7679 # get a logger for this document
7780 logger = SphinxDocLogger (document )
7881
7982 # get markdown parsing configuration
80- md_config : MdParserConfig = self . env .myst_config
83+ md_config : MdParserConfig = env .myst_config
8184
8285 # get notebook rendering configuration
83- nb_config : NbParserConfig = self . env .mystnb_config
86+ nb_config : NbParserConfig = env .mystnb_config
8487
8588 # create a reader for the notebook
8689 nb_reader = create_nb_reader (document_path , md_config , nb_config , inputstring )
@@ -159,13 +162,11 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
159162
160163 # save final execution data
161164 if nb_client .exec_metadata :
162- NbMetadataCollector .set_exec_data (
163- self .env , self .env .docname , nb_client .exec_metadata
164- )
165+ NbMetadataCollector .set_exec_data (env , env .docname , nb_client .exec_metadata )
165166 if nb_client .exec_metadata ["traceback" ]:
166167 # store error traceback in outdir and log its path
167- reports_file = Path (self . env . app .outdir ).joinpath (
168- "reports" , * (self . env .docname + ".err.log" ).split ("/" )
168+ reports_file = Path (get_env_app ( env ) .outdir ).joinpath (
169+ "reports" , * (env .docname + ".err.log" ).split ("/" )
169170 )
170171 reports_file .parent .mkdir (parents = True , exist_ok = True )
171172 reports_file .write_text (
@@ -177,7 +178,7 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
177178 )
178179
179180 # write final (updated) notebook to output folder (utf8 is standard encoding)
180- path = self . env .docname .split ("/" )
181+ path = env .docname .split ("/" )
181182 ipynb_path = path [:- 1 ] + [path [- 1 ] + ".ipynb" ]
182183 content = nbformat .writes (notebook ).encode ("utf-8" )
183184 nb_renderer .write_file (ipynb_path , content , overwrite = True )
@@ -193,16 +194,14 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
193194 overwrite = True ,
194195 )
195196 NbMetadataCollector .set_doc_data (
196- self . env , self . env .docname , "glue" , list (nb_client .glue_data .keys ())
197+ env , env .docname , "glue" , list (nb_client .glue_data .keys ())
197198 )
198199
199200 # move some document metadata to environment metadata,
200201 # so that we can later read it from the environment,
201202 # rather than having to load the whole doctree
202203 for key , (uri , kwargs ) in document .attributes .pop ("nb_js_files" , {}).items ():
203- NbMetadataCollector .add_js_file (
204- self .env , self .env .docname , key , uri , kwargs
205- )
204+ NbMetadataCollector .add_js_file (env , env .docname , key , uri , kwargs )
206205
207206 # remove temporary state
208207 document .attributes .pop ("nb_renderer" )
@@ -325,7 +324,7 @@ def run(self, **kwargs: Any) -> None:
325324 """Run the transform."""
326325 # get priority list for this builder
327326 # TODO allow for per-notebook/cell priority dicts?
328- bname = self .app .builder .name
327+ bname = get_env_app ( self .env ) .builder .name
329328 priority_list = get_mime_priority (
330329 bname , self .config ["nb_mime_priority_overrides" ]
331330 )
0 commit comments