|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | from pathlib import Path |
5 | | -from typing import Any, Dict |
| 5 | +from typing import TYPE_CHECKING |
6 | 6 | from urllib.parse import urljoin, urlparse, urlsplit, urlunparse |
7 | 7 |
|
8 | 8 | import docutils.nodes as nodes |
9 | | -from sphinx.application import Sphinx |
10 | 9 |
|
11 | 10 | from sphinxext.opengraph.descriptionparser import get_description |
12 | 11 | from sphinxext.opengraph.metaparser import get_meta_description |
13 | 12 | from sphinxext.opengraph.titleparser import get_title |
14 | 13 |
|
| 14 | +if TYPE_CHECKING: |
| 15 | + from typing import Any |
| 16 | + |
| 17 | + from sphinx.application import Sphinx |
| 18 | + |
15 | 19 | try: |
16 | | - import matplotlib |
17 | | -except ImportError: |
18 | | - print("matplotlib is not installed, social cards will not be generated") |
19 | | - create_social_card = None |
20 | | - DEFAULT_SOCIAL_CONFIG = {} |
21 | | -else: |
22 | 20 | from sphinxext.opengraph.socialcards import ( |
23 | 21 | DEFAULT_SOCIAL_CONFIG, |
24 | 22 | create_social_card, |
25 | 23 | ) |
| 24 | +except ImportError: |
| 25 | + print("matplotlib is not installed, social cards will not be generated") |
| 26 | + create_social_card = None |
| 27 | + DEFAULT_SOCIAL_CONFIG = {} |
26 | 28 |
|
27 | 29 | __version__ = "0.9.1" |
28 | 30 | version_info = (0, 9, 1) |
@@ -54,9 +56,9 @@ def make_tag(property: str, content: str, type_: str = "property") -> str: |
54 | 56 |
|
55 | 57 | def get_tags( |
56 | 58 | app: Sphinx, |
57 | | - context: Dict[str, Any], |
| 59 | + context: dict[str, Any], |
58 | 60 | doctree: nodes.document, |
59 | | - config: Dict[str, Any], |
| 61 | + config: dict[str, Any], |
60 | 62 | ) -> str: |
61 | 63 | # Get field lists for per-page overrides |
62 | 64 | fields = context["meta"] |
@@ -268,14 +270,14 @@ def html_page_context( |
268 | 270 | app: Sphinx, |
269 | 271 | pagename: str, |
270 | 272 | templatename: str, |
271 | | - context: Dict[str, Any], |
| 273 | + context: dict[str, Any], |
272 | 274 | doctree: nodes.document, |
273 | 275 | ) -> None: |
274 | 276 | if doctree: |
275 | 277 | context["metatags"] += get_tags(app, context, doctree, app.config) |
276 | 278 |
|
277 | 279 |
|
278 | | -def setup(app: Sphinx) -> Dict[str, Any]: |
| 280 | +def setup(app: Sphinx) -> dict[str, Any]: |
279 | 281 | # ogp_site_url="" allows relative by default, even though it's not |
280 | 282 | # officially supported by OGP. |
281 | 283 | app.add_config_value("ogp_site_url", "", "html") |
|
0 commit comments