1010import sys
1111from collections import deque
1212from io import StringIO
13+ from pathlib import Path
1314from typing import TYPE_CHECKING , overload
1415
1516from docutils .parsers .rst import roles
3738if TYPE_CHECKING :
3839 import os
3940 from collections .abc import Callable , Collection , Iterable , Sequence , Set
40- from pathlib import Path
4141 from typing import IO , Any , Final , Literal
4242
4343 from docutils import nodes
@@ -1484,6 +1484,9 @@ def add_js_file(
14841484 A JavaScript file can be added to the specific HTML page when an extension
14851485 calls this method on :event:`html-page-context` event.
14861486
1487+ .. seealso::
1488+ :meth:`add_static_dir` for copying static files to the output directory
1489+
14871490 .. versionadded:: 0.5
14881491
14891492 .. versionchanged:: 1.8
@@ -1549,6 +1552,9 @@ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> Non
15491552 A CSS file can be added to the specific HTML page when an extension calls
15501553 this method on :event:`html-page-context` event.
15511554
1555+ .. seealso::
1556+ :meth:`add_static_dir` for copying static files to the output directory
1557+
15521558 .. versionadded:: 1.0
15531559
15541560 .. versionchanged:: 1.6
@@ -1572,6 +1578,42 @@ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> Non
15721578 filename , priority = priority , ** kwargs
15731579 )
15741580
1581+ def add_static_dir (self , path : str | os .PathLike [str ]) -> None :
1582+ """Register a static directory to include in HTML output.
1583+
1584+ The given directory's contents will be copied to the ``_static``
1585+ directory during an HTML build. Files from extension static directories
1586+ are copied after theme static files and before any directories from
1587+ the user-configured ``html_static_path`` setting.
1588+
1589+ Sphinx has built-in support for ``static/`` directories in themes;
1590+ theme developers should only use this method to register further
1591+ directories to be copied.
1592+
1593+ :param path: The path to a directory containing static files.
1594+ This is typically relative to the extension's package
1595+ directory.
1596+
1597+ Example::
1598+
1599+ from pathlib import Path
1600+
1601+ def setup(app):
1602+ # All files in this directory are copied to _static/,
1603+ # preserving the subdirectory structure
1604+ app.add_static_dir(Path(__file__).parent / 'static')
1605+
1606+ # Add JavaScript and CSS files to HTML pages,
1607+ # the paths are relative to _static/
1608+ app.add_js_file('js/my_extension.js')
1609+ app.add_css_file('css/my_extension.css')
1610+
1611+ .. versionadded:: 9.1
1612+ """
1613+ path = Path (path )
1614+ logger .debug ("[app] adding static_dir: '%s'" , path )
1615+ self .registry .add_static_dir (path )
1616+
15751617 def add_latex_package (
15761618 self , packagename : str , options : str | None = None , after_hyperref : bool = False
15771619 ) -> None :
0 commit comments