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

Use canonical URLs when ogp_site_url is not configured #56

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ Just add `sphinxext.opengraph` to your extensions list in your `conf.py`

```python
extensions = [
"sphinxext.opengraph",
"sphinxext.opengraph",
]
```
## Options
These values are placed in the conf.py of your sphinx project.

Users hosting documentation on Read The Docs *do not* need to set any of the following unless custom configuration is wanted. The extension will automatically retrieve your site url.
Users hosting documentation on Read The Docs *do not* need to set any of the following unless custom configuration is wanted. RTD will automatically set`html_baseurl`.

* `html_baseurl`
* Configure the canonical url in Sphinx, this is required unless `ogp_site_url` is provided. It should be set to the canonical URL the site is being hosted on.


* `ogp_site_url`
* This config option is very important, set it to the URL the site is being hosted on.
* Overrides `html_baseurl` for this extension, not required. If set should be the canonical URL the site is being hosted on.
* `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.
* 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.
* `ogp_image`
Expand All @@ -37,7 +41,7 @@ Users hosting documentation on Read The Docs *do not* need to set any of the fol
* This sets the ogp type attribute, for more information on the types available please take a look at https://ogp.me/#types. By default it is set to `website`, which should be fine for most use cases.
* `ogp_custom_meta_tags`
* This is not required. List of custom html snippets to insert.

## Example Config

### Simple Config
Expand Down Expand Up @@ -70,17 +74,17 @@ Make sure you place the fields at the very start of the document such that Sphin
These are some overrides that can be used, you can actually override any tag and field lists will always take priority.

* `:og_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 OpenGraph 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`. Note the slightly different syntax because this isn't directly an OpenGraph tag.
* `:og:description:`
* Lets you override the description of the page.
* Lets you override the description of the page.
* `:og:title:`
* Lets you override the title of the page.
* Lets you override the title of the page.
* `:og:type:`
* Override the type of the page, for the list of available types take a look at https://ogp.me/#types.
* Override the type of the page, for the list of available types take a look at https://ogp.me/#types.
* `:ogp:image:`
* Set the image for the page.[^1]
* Set the image for the page.[^1]
* `:ogp:image:alt:`
* Sets the alt text. Will be ignored if there is no image set.
* Sets the alt text. Will be ignored if there is no image set.

### Example
Remember that the fields **must** be placed at the very start of the file. You can verify Sphinx has picked up the fields if they aren't shown in the final html file.
Expand Down
29 changes: 12 additions & 17 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

import docutils.nodes as nodes
from sphinx.application import Sphinx
from sphinx.errors import ConfigError
from sphinx.util import logging

from .descriptionparser import get_description
from .titleparser import get_title

import os

logger = logging.getLogger(__name__)


DEFAULT_DESCRIPTION_LENGTH = 200

Expand Down Expand Up @@ -67,26 +71,17 @@ def get_tags(
# type tag
tags["og:type"] = config["ogp_type"]

if os.getenv("READTHEDOCS") and config["ogp_site_url"] is None:
# readthedocs uses html_baseurl for sphinx > 1.8
parse_result = urlparse(config["html_baseurl"])

# url tag
# Get the canonical URL if ogp_site_url is not configured
if config["ogp_site_url"] is None:
# either ogp_site_url or html_baseurl should be configured
if config["html_baseurl"] is None:
raise EnvironmentError("ReadTheDocs did not provide a valid canonical URL!")

# Grab root url from canonical url
config["ogp_site_url"] = urlunparse(
(
parse_result.scheme,
parse_result.netloc,
parse_result.path,
"",
"",
"",
logger.info(
"sphinxext-opengrpah: A URL has not been configured and is required as per the OpenGraph Specification. Can be ignored if deploying in RTD environments."
)
ItayZiv marked this conversation as resolved.
Show resolved Hide resolved
)

# url tag
config["ogp_site_url"] = config["html_baseurl"]

# Get the URL of the specific page
if context["builder"] == "dirhtml":
page_url = urljoin(config["ogp_site_url"], context["pagename"] + "/")
Expand Down
2 changes: 1 addition & 1 deletion tests/roots/test-arbitrary-tags/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-custom-tags/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"

ogp_custom_meta_tags = [
'<meta property="og:ignore_canonical" content="true" />',
Expand Down
2 changes: 1 addition & 1 deletion tests/roots/test-description-length/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_description_length = 50
2 changes: 1 addition & 1 deletion tests/roots/test-double-spacing/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-first-image-no-image/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image = "http://example.org/en/latest/image33.png"
ogp_image_alt = "TEST"
ogp_use_first_image = True
2 changes: 1 addition & 1 deletion tests/roots/test-first-image/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image = "http://example.org/en/latest/image33.png"
ogp_image_alt = "TEST"
ogp_use_first_image = True
2 changes: 1 addition & 1 deletion tests/roots/test-image-rel-paths/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_use_first_image = True
2 changes: 1 addition & 1 deletion tests/roots/test-image/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image = "http://example.org/en/latest/image.png"
2 changes: 1 addition & 1 deletion tests/roots/test-list/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-local-image/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image = "_static/sample.jpg"
2 changes: 1 addition & 1 deletion tests/roots/test-nested-lists/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
exclude_patterns = ["_build"]

html_theme = "basic"

html_baseurl = "http://example.org/en/latest/"
ogp_site_url = "https://example.com/en/stable/"
1 change: 1 addition & 0 deletions tests/roots/test-override-url/index.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion tests/roots/test-overrides-complex/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image_alt = "Example Alt Text"
2 changes: 1 addition & 1 deletion tests/roots/test-overrides-simple/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_image = "http://example.org/en/latest/image.png"
ogp_type = "book"
2 changes: 1 addition & 1 deletion tests/roots/test-quotation-marks/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

smartquotes = False

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
8 changes: 0 additions & 8 deletions tests/roots/test-rtd-default/index.rst

This file was deleted.

6 changes: 0 additions & 6 deletions tests/roots/test-rtd-invalid/conf.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/roots/test-rtd-invalid/index.rst

This file was deleted.

2 changes: 1 addition & 1 deletion tests/roots/test-simple/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-sitename/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_site_name = "Example's Docs!"
2 changes: 1 addition & 1 deletion tests/roots/test-skip-admonitions/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-skip-code-block/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_description_length = 100
2 changes: 1 addition & 1 deletion tests/roots/test-skip-comments/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-skip-raw/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_description_length = 100
2 changes: 1 addition & 1 deletion tests/roots/test-skip-title/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
2 changes: 1 addition & 1 deletion tests/roots/test-type/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

html_theme = "basic"

ogp_site_url = "http://example.org/en/latest/"
html_baseurl = "http://example.org/en/latest/"
ogp_type = "article"
41 changes: 8 additions & 33 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def test_dirhtml_url(og_meta_tags):
assert get_tag_content(og_meta_tags, "url") == "http://example.org/en/latest/index/"


@pytest.mark.sphinx("html", testroot="override-url")
def test_override_url(og_meta_tags):
assert (
get_tag_content(og_meta_tags, "url")
== "https://example.com/en/stable/index.html"
)


@pytest.mark.sphinx("html", testroot="image")
def test_image(og_meta_tags):
assert (
Expand Down Expand Up @@ -215,36 +223,3 @@ def test_arbitrary_tags(og_meta_tags):
== "http://example.org/en/latest/video.mp4"
)
assert get_tag_content(og_meta_tags, "video:type") == "video/mp4"


# use same as simple, as configuration is identical to overriden
@pytest.mark.sphinx("html", testroot="simple")
def test_rtd_override(app: Sphinx, monkeypatch):
monkeypatch.setenv("READTHEDOCS", "True")
app.config.html_baseurl = "https://failure.com/en/latest/"

app.build()
tags = conftest._og_meta_tags(app)

assert get_tag_content(tags, "url") == "http://example.org/en/latest/index.html"


@pytest.mark.sphinx("html", testroot="rtd-default")
def test_rtd_valid(app: Sphinx, monkeypatch):
monkeypatch.setenv("READTHEDOCS", "True")
app.config.html_baseurl = "https://failure.com/en/latest/"

app.build()
tags = conftest._og_meta_tags(app)

assert get_tag_content(tags, "url") == "https://failure.com/en/latest/index.html"


# use rtd-default, as we are not changing configuration, but RTD variables
@pytest.mark.sphinx("html", testroot="rtd-invalid")
def test_rtd_invalid(app: Sphinx, monkeypatch):
monkeypatch.setenv("READTHEDOCS", "True")
app.config.html_baseurl = None

with pytest.raises(Exception):
app.build()