Skip to content

Commit

Permalink
Add :ogp_disable: metadata field (#86)
Browse files Browse the repository at this point in the history
* feat: handle `ogp_disable` field

* docs: update readme

* test: add tests

* docs: swap footnotes

* docs: clarify readme wording
  • Loading branch information
shiftinv authored Sep 8, 2023
1 parent 651d0ac commit 457e75d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 @@ -89,7 +91,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 @@ -105,7 +107,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 @@ -115,4 +117,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 @@ -56,6 +56,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 @@ -270,6 +270,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

0 comments on commit 457e75d

Please sign in to comment.