From 8673f8d393c4db69989085dc78018daeb975e3e7 Mon Sep 17 00:00:00 2001 From: tcmetzger Date: Sun, 19 Jul 2020 20:01:49 -0700 Subject: [PATCH 1/4] Add support for og:image:alt --- README.md | 3 +++ sphinxext/opengraph.py | 10 ++++++++++ tests/roots/test-image/conf.py | 1 + tests/test_options.py | 5 +++++ 4 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 6fee5ae..58f8fde 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ These values are placed in the conf.py of your sphinx project. * This is not required. Name of the site. This is displayed above the title. * `ogp_image` * This is not required. Link to image to show. +* `ogp_image_alt` + * This is not required. Alt text for image. Defaults to using `ogp_site_name` as alt text, if available. Set to `False` if + you want to turn off alt text completely. * `ogp_type` * This sets the ogp type attribute, for more information on the types available please take a look at https://ogp.me/#types. By default it is set to `website`, which should be fine for most use cases. * `ogp_custom_meta_tags` diff --git a/sphinxext/opengraph.py b/sphinxext/opengraph.py index 8ceb571..f1ba45f 100644 --- a/sphinxext/opengraph.py +++ b/sphinxext/opengraph.py @@ -184,6 +184,15 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str, if image_url: tags += make_tag("og:image", image_url) + # Add image alt text (either provided by config or from site_name) + ogp_image_alt = config["ogp_image_alt"] + if isinstance(ogp_image_alt, str): + tags += make_tag("og:image:alt", config["ogp_image_alt"]) + elif config["ogp_image_alt"] and site_name: + tags += make_tag("og:image:alt", site_name) + elif config["ogp_image_alt"] and htp.text: + tags += make_tag("og:image:alt", htp.text) + # custom tags tags += '\n'.join(config['ogp_custom_meta_tags']) @@ -199,6 +208,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("ogp_site_url", None, "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", True, "html") app.add_config_value("ogp_type", "website", "html") app.add_config_value("ogp_site_name", None, "html") app.add_config_value("ogp_custom_meta_tags", [], "html") diff --git a/tests/roots/test-image/conf.py b/tests/roots/test-image/conf.py index 369d5a0..bc1c7d6 100644 --- a/tests/roots/test-image/conf.py +++ b/tests/roots/test-image/conf.py @@ -5,5 +5,6 @@ html_theme = "basic" +ogp_site_name = "Example's Docs!" ogp_site_url = "http://example.org/" ogp_image = "http://example.org/image.png" diff --git a/tests/test_options.py b/tests/test_options.py index 4705dac..8cda53a 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -31,6 +31,11 @@ def test_image(og_meta_tags): assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png" +@pytest.mark.sphinx("html", testroot="image") +def test_image_alt(og_meta_tags): + assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!" + + @pytest.mark.sphinx("html", testroot="type") def test_type(og_meta_tags): assert get_tag_content(og_meta_tags, "type") == "article" From 8ff1a021708cc6fe404388b5395ca5e26178f608 Mon Sep 17 00:00:00 2001 From: tcmetzger Date: Sun, 19 Jul 2020 20:07:34 -0700 Subject: [PATCH 2/4] Include document's title as alt source --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58f8fde..1de6ff5 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ These values are placed in the conf.py of your sphinx project. * `ogp_image` * This is not required. Link to image to show. * `ogp_image_alt` - * This is not required. Alt text for image. Defaults to using `ogp_site_name` as alt text, if available. Set to `False` if - you want to turn off alt text completely. + * This is not required. Alt text for image. Defaults to using `ogp_site_name` or the document's title as alt text, + if available. Set to `False` if you want to turn off alt text completely. * `ogp_type` * This sets the ogp type attribute, for more information on the types available please take a look at https://ogp.me/#types. By default it is set to `website`, which should be fine for most use cases. * `ogp_custom_meta_tags` From c731ae87a967c5ee5a5ba1e71a6bedc581da18b4 Mon Sep 17 00:00:00 2001 From: Timo Cornelius Metzger Date: Mon, 20 Jul 2020 09:10:53 -0700 Subject: [PATCH 3/4] Update sphinxext/opengraph.py Co-authored-by: Vasista Vovveti --- sphinxext/opengraph.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sphinxext/opengraph.py b/sphinxext/opengraph.py index f1ba45f..188c1c4 100644 --- a/sphinxext/opengraph.py +++ b/sphinxext/opengraph.py @@ -187,10 +187,10 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str, # Add image alt text (either provided by config or from site_name) ogp_image_alt = config["ogp_image_alt"] if isinstance(ogp_image_alt, str): - tags += make_tag("og:image:alt", config["ogp_image_alt"]) - elif config["ogp_image_alt"] and site_name: + tags += make_tag("og:image:alt", ogp_image_alt) + elif ogp_image_alt and site_name: tags += make_tag("og:image:alt", site_name) - elif config["ogp_image_alt"] and htp.text: + elif ogp_image_alt and htp.text: tags += make_tag("og:image:alt", htp.text) # custom tags @@ -219,4 +219,3 @@ def setup(app: Sphinx) -> Dict[str, Any]: "parallel_read_safe": True, "parallel_write_safe": True, } - From 835c10f3143103cef40973747c7f096208ff91f4 Mon Sep 17 00:00:00 2001 From: tcmetzger Date: Mon, 20 Jul 2020 13:03:29 -0700 Subject: [PATCH 4/4] Remove line break for consistency --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 1de6ff5..542ebe1 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,7 @@ These values are placed in the conf.py of your sphinx project. * `ogp_image` * This is not required. Link to image to show. * `ogp_image_alt` - * This is not required. Alt text for image. Defaults to using `ogp_site_name` or the document's title as alt text, - if available. Set to `False` if you want to turn off alt text completely. + * This is not required. Alt text for image. Defaults to using `ogp_site_name` or the document's title as alt text, if available. Set to `False` if you want to turn off alt text completely. * `ogp_type` * This sets the ogp type attribute, for more information on the types available please take a look at https://ogp.me/#types. By default it is set to `website`, which should be fine for most use cases. * `ogp_custom_meta_tags`