Skip to content

Commit 457e75d

Browse files
authored
Add :ogp_disable: metadata field (sphinx-doc#86)
* feat: handle `ogp_disable` field * docs: update readme * test: add tests * docs: swap footnotes * docs: clarify readme wording
1 parent 651d0ac commit 457e75d

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ Make sure you place the fields at the very start of the document such that Sphin
7979
These are some overrides that can be used on individual pages, you can actually override any tag and field lists will always take priority.
8080

8181
* `:ogp_description_length:`
82-
* 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.
82+
* 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]
83+
* `:ogp_disable:`
84+
* Disables generation of Open Graph tags on the page.[^1]
8385
* `:og:description:`
8486
* Lets you override the description of the page.
8587
* `:description:` or `.. meta::\n :description:`
@@ -89,7 +91,7 @@ These are some overrides that can be used on individual pages, you can actually
8991
* `:og:type:`
9092
* Override the type of the page, for the list of available types take a look at https://ogp.me/#types.
9193
* `:og:image:`
92-
* Set the image for the page.[^1]
94+
* Set the image for the page.[^2]
9395
* `:og:image:alt:`
9496
* Sets the alt text. Will be ignored if there is no image set.
9597

@@ -105,7 +107,7 @@ Page contents
105107
=============
106108
```
107109

108-
### Arbitrary Tags[^1]
110+
### Arbitrary Tags[^2]
109111
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:
110112

111113
```rst
@@ -115,4 +117,5 @@ Page contents
115117
=============
116118
```
117119

118-
[^1]: Note: Relative file paths for images, videos and audio are currently **not** supported when using field lists. Please use an absolute path instead.
120+
[^1]: Note the slightly different syntax because this isn't directly an Open Graph tag.
121+
[^2]: Note: Relative file paths for images, videos and audio are currently **not** supported when using field lists. Please use an absolute path instead.

sphinxext/opengraph/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def get_tags(
5656
fields = context["meta"]
5757
if fields is None:
5858
fields = {}
59+
60+
if "ogp_disable" in fields:
61+
return ""
62+
5963
tags = {}
6064
meta_tags = {} # For non-og meta tags
6165

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
ogp_site_name = "Example's Docs!"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "http://example.org/en/latest/image.png"
11+
ogp_type = "book"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:ogp_disable:
2+
3+
Lorem Ipsum
4+
===========
5+
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.

tests/test_options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ def test_overrides_complex(og_meta_tags):
270270
assert get_tag_content(og_meta_tags, "image:alt") == "Overridden Alt Text"
271271

272272

273+
@pytest.mark.sphinx("html", testroot="overrides-disable")
274+
def test_overrides_disable(og_meta_tags):
275+
assert len(og_meta_tags) == 0
276+
277+
273278
@pytest.mark.sphinx("html", testroot="arbitrary-tags")
274279
def test_arbitrary_tags(og_meta_tags):
275280
assert (

0 commit comments

Comments
 (0)