Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :ogp_disable: metadata field #86

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ Make sure you place the fields at the very start of the document such that Sphin
These are some overrides that can be used on individual pages, you can actually override any tag and field lists will always take priority.

* `:ogp_description_length:`
* Configure the amount of characters to grab for the description of the page. If the value isn't a number it will fall back to `ogp_description_length`. Note the slightly different syntax because this isn't directly an Open Graph tag.
* Configure the amount of characters to grab for the description of the page. If the value isn't a number it will fall back to `ogp_description_length`.[^1]
* `:ogp_disable:`
* Disables generation of Open Graph tags on the page.[^1]
* `:og:description:`
* Lets you override the description of the page.
* `:description:` or `.. meta::\n :description:`
Expand All @@ -86,7 +88,7 @@ These are some overrides that can be used on individual pages, you can actually
* `:og:type:`
* Override the type of the page, for the list of available types take a look at https://ogp.me/#types.
* `:og:image:`
* Set the image for the page.[^1]
* Set the image for the page.[^2]
* `:og:image:alt:`
* Sets the alt text. Will be ignored if there is no image set.

Expand All @@ -102,7 +104,7 @@ Page contents
=============
```

### Arbitrary Tags[^1]
### Arbitrary Tags[^2]
Additionally, you can use field lists to add any arbitrary Open Graph tag not supported by the extension. The syntax for arbitrary tags is the same with `:og:tag: content`. For example:

```rst
Expand All @@ -112,4 +114,5 @@ Page contents
=============
```

[^1]: Note: Relative file paths for images, videos and audio are currently **not** supported when using field lists. Please use an absolute path instead.
[^1]: Note the slightly different syntax because this isn't directly an Open Graph tag.
[^2]: Note: Relative file paths for images, videos and audio are currently **not** supported when using field lists. Please use an absolute path instead.
4 changes: 4 additions & 0 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def get_tags(
fields = context["meta"]
if fields is None:
fields = {}

if "ogp_disable" in fields:
return ""

tags = {}
meta_tags = {} # For non-og meta tags

Expand Down
11 changes: 11 additions & 0 deletions tests/roots/test-overrides-disable/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extensions = ["sphinxext.opengraph"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
ogp_image = "http://example.org/en/latest/image.png"
ogp_type = "book"
5 changes: 5 additions & 0 deletions tests/roots/test-overrides-disable/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:ogp_disable:

Lorem Ipsum
===========
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ def test_overrides_complex(og_meta_tags):
assert get_tag_content(og_meta_tags, "image:alt") == "Overridden Alt Text"


@pytest.mark.sphinx("html", testroot="overrides-disable")
def test_overrides_disable(og_meta_tags):
assert len(og_meta_tags) == 0


@pytest.mark.sphinx("html", testroot="arbitrary-tags")
def test_arbitrary_tags(og_meta_tags):
assert (
Expand Down