File tree 7 files changed +49
-4
lines changed
roots/test-image-rel-paths 7 files changed +49
-4
lines changed Original file line number Diff line number Diff line change 1
1
from typing import Any , Dict
2
2
from urllib .parse import urljoin , urlparse , urlunparse
3
- from pathlib import Path , PosixPath
3
+ from pathlib import Path
4
4
5
5
import docutils .nodes as nodes
6
6
from sphinx .application import Sphinx
7
- from sphinx .util import logger
8
7
9
8
from .descriptionparser import get_description
10
9
from .titleparser import get_title
@@ -111,6 +110,10 @@ def get_tags(
111
110
ogp_image_alt = first_image .get ("alt" , None )
112
111
113
112
if image_url :
113
+ image_url_parsed = urlparse (image_url )
114
+ if not image_url_parsed .scheme :
115
+ # Relative image path detected. Make absolute.
116
+ image_url = urljoin (config ["ogp_site_url" ], image_url_parsed .path )
114
117
tags += make_tag ("og:image" , image_url )
115
118
116
119
# Add image alt text (either provided by config or from site_name)
Original file line number Diff line number Diff line change @@ -19,8 +19,11 @@ def content(app):
19
19
yield app
20
20
21
21
22
- def _meta_tags (content ):
23
- c = (content .outdir / "index.html" ).read_text ()
22
+ def _meta_tags (content , subdir = None ):
23
+ if subdir is None :
24
+ c = (content .outdir / "index.html" ).read_text ()
25
+ else :
26
+ c = (content .outdir / subdir / "index.html" ).read_text ()
24
27
return BeautifulSoup (c , "html.parser" ).find_all ("meta" )
25
28
26
29
@@ -42,5 +45,14 @@ def og_meta_tags(content):
42
45
]
43
46
44
47
48
+ @pytest .fixture ()
49
+ def og_meta_tags_sub (content ):
50
+ return [
51
+ tag
52
+ for tag in _meta_tags (content , "sub" )
53
+ if tag .get ("property" , "" ).startswith ("og:" )
54
+ ]
55
+
56
+
45
57
def pytest_configure (config ):
46
58
config .addinivalue_line ("markers" , "sphinx" )
Original file line number Diff line number Diff line change
1
+ extensions = ["sphinxext.opengraph" ]
2
+
3
+ master_doc = "index"
4
+ exclude_patterns = ["_build" ]
5
+
6
+ html_theme = "basic"
7
+
8
+ ogp_site_name = "Example's Docs!"
9
+ ogp_site_url = "http://example.org/"
10
+ ogp_use_first_image = True
Original file line number Diff line number Diff line change
1
+ .. image :: img/sample.jpg
2
+ :alt: Test image alt text
Original file line number Diff line number Diff line change
1
+ ========
2
+ Sub Page
3
+ ========
4
+
5
+ .. image :: ../img/sample.jpg
6
+ :alt: Test image alt text
Original file line number Diff line number Diff line change @@ -37,6 +37,18 @@ def test_image(og_meta_tags):
37
37
assert get_tag_content (og_meta_tags , "image" ) == "http://example.org/image.png"
38
38
39
39
40
+ @pytest .mark .sphinx ("html" , testroot = "image-rel-paths" )
41
+ def test_image_rel_paths (og_meta_tags , og_meta_tags_sub ):
42
+ assert (
43
+ get_tag_content (og_meta_tags , "image" )
44
+ == "http://example.org/_images/sample.jpg"
45
+ )
46
+ assert (
47
+ get_tag_content (og_meta_tags_sub , "image" )
48
+ == "http://example.org/_images/sample.jpg"
49
+ )
50
+
51
+
40
52
@pytest .mark .sphinx ("html" , testroot = "image" )
41
53
def test_image_alt (og_meta_tags ):
42
54
assert get_tag_content (og_meta_tags , "image:alt" ) == "Example's Docs!"
You can’t perform that action at this time.
0 commit comments