2424import yaml
2525from tqdm import tqdm
2626
27- from .autodoc import autodoc , find_object_in_package , remove_example_tags , resolve_links_in_text
27+ from .autodoc import autodoc , find_object_in_package , get_source_path , remove_example_tags , resolve_links_in_text
2828from .convert_md_to_mdx import convert_md_to_mdx
2929from .convert_rst_to_mdx import convert_rst_to_mdx , find_indent , is_empty_line
3030from .convert_to_notebook import generate_notebooks_from_file
@@ -87,6 +87,7 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
8787 is_inside_codeblock = False
8888 lines = content .split ("\n " )
8989 new_lines = []
90+ source_files = None
9091 if return_anchors :
9192 anchors = []
9293 errors = []
@@ -125,6 +126,8 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
125126 errors .extend (doc [2 ])
126127 doc = doc [0 ]
127128 new_lines .append (doc )
129+
130+ source_files = get_source_path (object_name , package )
128131 else :
129132 new_lines .append (lines [idx ])
130133 if lines [idx ].startswith ("```" ):
@@ -136,7 +139,7 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
136139 new_content = "\n " .join (new_lines )
137140 new_content = remove_example_tags (new_content )
138141
139- return (new_content , anchors , errors ) if return_anchors else new_content
142+ return (new_content , anchors , source_files , errors ) if return_anchors else new_content
140143
141144
142145def build_mdx_files (package , doc_folder , output_dir , page_info ):
@@ -153,6 +156,7 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
153156 output_dir = Path (output_dir )
154157 os .makedirs (output_dir , exist_ok = True )
155158 anchor_mapping = {}
159+ source_files_mapping = {}
156160
157161 if "package_name" not in page_info :
158162 page_info ["package_name" ] = package .__name__
@@ -171,9 +175,11 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
171175 content = reader .read ()
172176 content = convert_md_to_mdx (content , page_info )
173177 content = resolve_open_in_colab (content , page_info )
174- content , new_anchors , errors = resolve_autodoc (
178+ content , new_anchors , source_files , errors = resolve_autodoc (
175179 content , package , return_anchors = True , page_info = page_info
176180 )
181+ if source_files is not None :
182+ source_files_mapping [source_files ] = str (file )
177183 with open (dest_file , "w" , encoding = "utf-8" ) as writer :
178184 writer .write (content )
179185 # Make sure we clean up for next page.
@@ -186,9 +192,11 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
186192 content = reader .read ()
187193 content = convert_rst_to_mdx (content , page_info )
188194 content = resolve_open_in_colab (content , page_info )
189- content , new_anchors , errors = resolve_autodoc (
195+ content , new_anchors , source_files , errors = resolve_autodoc (
190196 content , package , return_anchors = True , page_info = page_info
191197 )
198+ if source_files is not None :
199+ source_files_mapping [source_files ] = str (file )
192200 with open (dest_file , "w" , encoding = "utf-8" ) as writer :
193201 writer .write (content )
194202 # Make sure we clean up for next page.
@@ -213,7 +221,7 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
213221 "The deployment of the documentation will fail because of the following errors:\n " + "\n " .join (all_errors )
214222 )
215223
216- return anchor_mapping
224+ return anchor_mapping , source_files_mapping
217225
218226
219227def resolve_links (doc_folder , package , mapping , page_info ):
@@ -351,6 +359,7 @@ def build_doc(
351359 language = "en" ,
352360 notebook_dir = None ,
353361 is_python_module = False ,
362+ watch_mode = False ,
354363):
355364 """
356365 Build the documentation of a package.
@@ -368,6 +377,9 @@ def build_doc(
368377 If provided, where to save the notebooks generated from the doc file with an [[open-in-colab]] marker.
369378 is_python_module (`bool`, *optional*, defaults to `False`):
370379 Whether the docs being built are for python module. (For example, HF Course is not a python module).
380+ watch_mode (`bool`, *optional*, default to `False`):
381+ If `True`, disables the toc tree check and sphinx objects.inv builds since they are not needed
382+ when this mode is active.
371383 """
372384 page_info = {"version" : version , "language" : language , "package_name" : package_name }
373385 if clean and Path (output_dir ).exists ():
@@ -376,11 +388,13 @@ def build_doc(
376388 read_doc_config (doc_folder )
377389
378390 package = importlib .import_module (package_name ) if is_python_module else None
379- anchors_mapping = build_mdx_files (package , doc_folder , output_dir , page_info )
380- sphinx_refs = check_toc_integrity (doc_folder , output_dir )
381- sphinx_refs .extend (convert_anchors_mapping_to_sphinx_format (anchors_mapping , package ))
391+ anchors_mapping , source_files_mapping = build_mdx_files (package , doc_folder , output_dir , page_info )
392+ if not watch_mode :
393+ sphinx_refs = check_toc_integrity (doc_folder , output_dir )
394+ sphinx_refs .extend (convert_anchors_mapping_to_sphinx_format (anchors_mapping , package ))
382395 if is_python_module :
383- build_sphinx_objects_ref (sphinx_refs , output_dir , page_info )
396+ if not watch_mode :
397+ build_sphinx_objects_ref (sphinx_refs , output_dir , page_info )
384398 resolve_links (output_dir , package , anchors_mapping , page_info )
385399 generate_frontmatter (output_dir )
386400
@@ -390,6 +404,8 @@ def build_doc(
390404 os .remove (nb_file )
391405 build_notebooks (doc_folder , notebook_dir , package = package , mapping = anchors_mapping , page_info = page_info )
392406
407+ return source_files_mapping
408+
393409
394410def check_toc_integrity (doc_folder , output_dir ):
395411 """
0 commit comments