Skip to content

Commit

Permalink
Take default og:site_name from sphinx project config value (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdarst committed Nov 22, 2022
1 parent fc41303 commit 7469f2b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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`
Expand Down
10 changes: 8 additions & 2 deletions sphinxext/opengraph/__init__.py
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions 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/"
1 change: 1 addition & 0 deletions 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.
5 changes: 5 additions & 0 deletions tests/test_options.py
Expand Up @@ -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 (
Expand Down

0 comments on commit 7469f2b

Please sign in to comment.