summaryrefslogtreecommitdiff
path: root/sphinx/application.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/application.py')
-rw-r--r--sphinx/application.py70
1 files changed, 54 insertions, 16 deletions
diff --git a/sphinx/application.py b/sphinx/application.py
index 2253ce306..54a2603aa 100644
--- a/sphinx/application.py
+++ b/sphinx/application.py
@@ -916,22 +916,24 @@ class Sphinx:
"""
self.registry.add_post_transform(transform)
- def add_javascript(self, filename: str, **kwargs: str) -> None:
+ def add_javascript(self, filename: str, **kwargs: Any) -> None:
"""An alias of :meth:`add_js_file`."""
warnings.warn('The app.add_javascript() is deprecated. '
'Please use app.add_js_file() instead.',
RemovedInSphinx40Warning, stacklevel=2)
self.add_js_file(filename, **kwargs)
- def add_js_file(self, filename: str, **kwargs: str) -> None:
+ def add_js_file(self, filename: str, priority: int = 500, **kwargs: Any) -> None:
"""Register a JavaScript file to include in the HTML output.
Add *filename* to the list of JavaScript files that the default HTML
- template will include. The filename must be relative to the HTML
- static path , or a full URI with scheme. If the keyword argument
- ``body`` is given, its value will be added between the
- ``<script>`` tags. Extra keyword arguments are included as
- attributes of the ``<script>`` tag.
+ template will include in order of *priority* (ascending). The filename
+ must be relative to the HTML static path , or a full URI with scheme.
+ If the priority of JavaScript file is the same as others, the JavaScript
+ files will be included in order of the registration. If the keyword
+ argument ``body`` is given, its value will be added between the
+ ``<script>`` tags. Extra keyword arguments are included as attributes of
+ the ``<script>`` tag.
Example::
@@ -944,23 +946,43 @@ class Sphinx:
app.add_js_file(None, body="var myVariable = 'foo';")
# => <script>var myVariable = 'foo';</script>
+ .. list-table:: priority range for JavaScript files
+ :widths: 20,80
+
+ * - Priority
+ - Main purpose in Sphinx
+ * - 200
+ - default priority for built-in JavaScript files
+ * - 500
+ - default priority for extensions
+ * - 800
+ - default priority for :confval:`html_js_files`
+
+ A JavaScript file can be added to the specific HTML page when on extension
+ calls this method on :event:`html-page-context` event.
+
.. versionadded:: 0.5
.. versionchanged:: 1.8
Renamed from ``app.add_javascript()``.
And it allows keyword arguments as attributes of script tag.
+
+ .. versionchanged:: 3.5
+ Take priority argument. Allow to add a JavaScript file to the specific page.
"""
- self.registry.add_js_file(filename, **kwargs)
+ self.registry.add_js_file(filename, priority=priority, **kwargs)
if hasattr(self.builder, 'add_js_file'):
- self.builder.add_js_file(filename, **kwargs) # type: ignore
+ self.builder.add_js_file(filename, priority=priority, **kwargs) # type: ignore
- def add_css_file(self, filename: str, **kwargs: str) -> None:
+ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> None:
"""Register a stylesheet to include in the HTML output.
Add *filename* to the list of CSS files that the default HTML template
- will include. The filename must be relative to the HTML static path,
- or a full URI with scheme. The keyword arguments are also accepted for
- attributes of ``<link>`` tag.
+ will include in order of *priority* (ascending). The filename must be
+ relative to the HTML static path, or a full URI with scheme. If the
+ priority of CSS file is the same as others, the CSS files will be
+ included in order of the registration. The keyword arguments are also
+ accepted for attributes of ``<link>`` tag.
Example::
@@ -975,6 +997,19 @@ class Sphinx:
# => <link rel="alternate stylesheet" href="_static/fancy.css"
# type="text/css" title="fancy" />
+ .. list-table:: priority range for CSS files
+ :widths: 20,80
+
+ * - Priority
+ - Main purpose in Sphinx
+ * - 500
+ - default priority for extensions
+ * - 800
+ - default priority for :confval:`html_css_files`
+
+ A CSS file can be added to the specific HTML page when on extension calls
+ this method on :event:`html-page-context` event.
+
.. versionadded:: 1.0
.. versionchanged:: 1.6
@@ -987,11 +1022,14 @@ class Sphinx:
.. versionchanged:: 1.8
Renamed from ``app.add_stylesheet()``.
And it allows keyword arguments as attributes of link tag.
+
+ .. versionchanged:: 3.5
+ Take priority argument. Allow to add a CSS file to the specific page.
"""
logger.debug('[app] adding stylesheet: %r', filename)
- self.registry.add_css_files(filename, **kwargs)
+ self.registry.add_css_files(filename, priority=priority, **kwargs)
if hasattr(self.builder, 'add_css_file'):
- self.builder.add_css_file(filename, **kwargs) # type: ignore
+ self.builder.add_css_file(filename, priority=priority, **kwargs) # type: ignore
def add_stylesheet(self, filename: str, alternate: bool = False, title: str = None
) -> None:
@@ -1000,7 +1038,7 @@ class Sphinx:
'Please use app.add_css_file() instead.',
RemovedInSphinx40Warning, stacklevel=2)
- attributes = {} # type: Dict[str, str]
+ attributes = {} # type: Dict[str, Any]
if alternate:
attributes['rel'] = 'alternate stylesheet'
else: