diff --git a/README.md b/README.md index e561ea9..3bae1dd 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 a4bc48d..cf349c2 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 b814ca7..8dcd875 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 (