From 756d9e9c68ba9e06232850011f071122369fac80 Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Tue, 8 Nov 2022 16:40:18 +0200 Subject: [PATCH 1/5] Allow dirhtml builder without `ogp_site_url` - Yes, the readme says that ogp_site_url is very important. And indeed, reports online say that OGP doesn't work with relative URLs. Although at least one place says it does... - Yet, I got confused when I could build it with html (it left everything relative), but not with dirhtml (obscure extension error it took me a while to figure out). (And it only fails on the page named `/index`. - Set the default ogp_site_url to "" by default, instead of None, to make relative by default (previous behavior) instead of raising an exception. - Even if this isn't perfect, it helps a bit with "make this extension do something useful, even if not perfect, if it's installed but not configured". - So overall, even though this isn't a perfect change, and could be considered technically incorrect, I think it's less confusing and makes things more consistent for a possibly better solution later. --- sphinxext/opengraph/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sphinxext/opengraph/__init__.py b/sphinxext/opengraph/__init__.py index a4bc48d..c5565e1 100644 --- a/sphinxext/opengraph/__init__.py +++ b/sphinxext/opengraph/__init__.py @@ -193,7 +193,9 @@ def html_page_context( def setup(app: Sphinx) -> Dict[str, Any]: - app.add_config_value("ogp_site_url", None, "html") + # ogp_site_url="" allows relative by default, even though it's not + # officially supported by OGP. + app.add_config_value("ogp_site_url", "", "html") app.add_config_value("ogp_description_length", DEFAULT_DESCRIPTION_LENGTH, "html") app.add_config_value("ogp_image", None, "html") app.add_config_value("ogp_image_alt", None, "html") From 27dae4b960b8637f6a5483e5e85ef30d27c76701 Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Tue, 8 Nov 2022 23:36:35 +0200 Subject: [PATCH 2/5] Fix ogp_site_url="" default and RTD detection --- sphinxext/opengraph/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinxext/opengraph/__init__.py b/sphinxext/opengraph/__init__.py index c5565e1..c02ffd6 100644 --- a/sphinxext/opengraph/__init__.py +++ b/sphinxext/opengraph/__init__.py @@ -69,7 +69,7 @@ def get_tags( # type tag tags["og:type"] = config["ogp_type"] - if os.getenv("READTHEDOCS") and config["ogp_site_url"] is None: + if os.getenv("READTHEDOCS") and not config["ogp_site_url"]: # readthedocs uses html_baseurl for sphinx > 1.8 parse_result = urlparse(config["html_baseurl"]) From 9b115048cc0f775cd0b3e8385a82a5202f9cc53d Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Wed, 9 Nov 2022 15:04:39 +0200 Subject: [PATCH 3/5] tests/test_options: Add two tests of running with no configuration --- tests/test_options.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_options.py b/tests/test_options.py index b814ca7..2d3bf32 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -279,3 +279,17 @@ def test_rtd_invalid(app: Sphinx, monkeypatch): with pytest.raises(Exception): app.build() + + +# Test no breakage with no configuration +@pytest.mark.sphinx("html", testroot="no-configuration") +def test_no_configuration_html(og_meta_tags): + print(og_meta_tags) + assert get_tag_content(og_meta_tags, "type") == "website" + + +# Test no breakage with no configuration +@pytest.mark.sphinx("dirhtml", testroot="no-configuration") +def test_no_configuration_dirhtml(og_meta_tags): + print(og_meta_tags) + assert get_tag_content(og_meta_tags, "type") == "website" From df0ad60d666dcb03bcf97a1297535676df2662b1 Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Wed, 9 Nov 2022 15:19:01 +0200 Subject: [PATCH 4/5] Remove unneeded print statement Co-authored-by: Hugo van Kemenade --- tests/test_options.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_options.py b/tests/test_options.py index 2d3bf32..17f3693 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -284,12 +284,10 @@ def test_rtd_invalid(app: Sphinx, monkeypatch): # Test no breakage with no configuration @pytest.mark.sphinx("html", testroot="no-configuration") def test_no_configuration_html(og_meta_tags): - print(og_meta_tags) assert get_tag_content(og_meta_tags, "type") == "website" # Test no breakage with no configuration @pytest.mark.sphinx("dirhtml", testroot="no-configuration") def test_no_configuration_dirhtml(og_meta_tags): - print(og_meta_tags) assert get_tag_content(og_meta_tags, "type") == "website" From a2d7545b28d5184f75f7a66d7b8f21bb6782c28a Mon Sep 17 00:00:00 2001 From: Vasista Vovveti Date: Tue, 22 Nov 2022 14:58:10 -0500 Subject: [PATCH 5/5] Update test_options.py --- tests/test_options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_options.py b/tests/test_options.py index 17f3693..c61f9a1 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -282,12 +282,12 @@ def test_rtd_invalid(app: Sphinx, monkeypatch): # Test no breakage with no configuration -@pytest.mark.sphinx("html", testroot="no-configuration") +@pytest.mark.sphinx("html", testroot="rtd-default") def test_no_configuration_html(og_meta_tags): assert get_tag_content(og_meta_tags, "type") == "website" # Test no breakage with no configuration -@pytest.mark.sphinx("dirhtml", testroot="no-configuration") +@pytest.mark.sphinx("dirhtml", testroot="rtd-default") def test_no_configuration_dirhtml(og_meta_tags): assert get_tag_content(og_meta_tags, "type") == "website"