From 7469f2b32c54d4b2d7f0d64cd407c87d573995e4 Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Tue, 22 Nov 2022 22:24:18 +0200 Subject: [PATCH] Take default og:site_name from sphinx `project` config value (#83) --- README.md | 2 +- sphinxext/opengraph/__init__.py | 10 ++++++++-- tests/roots/test-sitename-from-project/conf.py | 9 +++++++++ tests/roots/test-sitename-from-project/index.rst | 1 + tests/test_options.py | 5 +++++ 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/roots/test-sitename-from-project/conf.py create mode 100644 tests/roots/test-sitename-from-project/index.rst diff --git a/README.md b/README.md index bf327e0..215f9e0 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Users hosting documentation on Read The Docs *do not* need to set any of the fol * `ogp_description_length` * Configure the amount of characters taken from a page. The default of 200 is probably good for most people. If something other than a number is used, it defaults back to 200. * `ogp_site_name` - * This is not required. Name of the site. This is displayed above the title. + * This is not required. Name of the site. This is displayed above the title. Defaults to the Sphinx [`project`](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-project) config value. Set to `False` to unset and use no default. * `ogp_image` * This is not required. Link to image to show. Note that all relative paths are converted to be relative to the root of the html output as defined by `ogp_site_url`. * `ogp_image_alt` diff --git a/sphinxext/opengraph/__init__.py b/sphinxext/opengraph/__init__.py index c02ffd6..6287169 100644 --- a/sphinxext/opengraph/__init__.py +++ b/sphinxext/opengraph/__init__.py @@ -104,8 +104,14 @@ def get_tags( ) tags["og:url"] = page_url - # site name tag - site_name = config["ogp_site_name"] + # site name tag, False disables, default to project if ogp_site_name not + # set. + if config["ogp_site_name"] is False: + site_name = None + elif config["ogp_site_name"] is None: + site_name = config["project"] + else: + site_name = config["ogp_site_name"] if site_name: tags["og:site_name"] = site_name diff --git a/tests/roots/test-sitename-from-project/conf.py b/tests/roots/test-sitename-from-project/conf.py new file mode 100644 index 0000000..dc7805b --- /dev/null +++ b/tests/roots/test-sitename-from-project/conf.py @@ -0,0 +1,9 @@ +extensions = ["sphinxext.opengraph"] + +project = "Project name" +master_doc = "index" +exclude_patterns = ["_build"] + +html_theme = "basic" + +ogp_site_url = "http://example.org/en/latest/" diff --git a/tests/roots/test-sitename-from-project/index.rst b/tests/roots/test-sitename-from-project/index.rst new file mode 100644 index 0000000..a33871d --- /dev/null +++ b/tests/roots/test-sitename-from-project/index.rst @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci varius natoque penatibus et magnis dis parturient mauris. \ No newline at end of file diff --git a/tests/test_options.py b/tests/test_options.py index c61f9a1..1d60125 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -107,6 +107,11 @@ def test_site_name(og_meta_tags): assert get_tag_content(og_meta_tags, "site_name") == "Example's Docs!" +@pytest.mark.sphinx("html", testroot="sitename-from-project") +def test_site_name_project(og_meta_tags): + assert get_tag_content(og_meta_tags, "site_name") == "Project name" + + @pytest.mark.sphinx("html", testroot="first-image") def test_first_image(og_meta_tags): assert (