Skip to content

Commit 2212715

Browse files
authored
Implement RTD Support (#30)
1 parent cdeb4ed commit 2212715

File tree

10 files changed

+112
-11
lines changed

10 files changed

+112
-11
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extensions = [
1919
## Options
2020
These values are placed in the conf.py of your sphinx project.
2121

22+
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.
23+
2224
* `ogp_site_url`
2325
* This config option is very important, set it to the URL the site is being hosted on.
2426
* `ogp_description_length`

docs/source/conf.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
#
1313
import os
1414
import sys
15-
sys.path.insert(0, os.path.abspath('../..'))
15+
16+
sys.path.insert(0, os.path.abspath("../.."))
1617

1718

1819
# -- Project information -----------------------------------------------------
1920

20-
project = 'sphinxext-opengraph'
21-
copyright = '2020, FIRST'
22-
author = 'WPILib'
21+
project = "sphinxext-opengraph"
22+
copyright = "2020, FIRST"
23+
author = "WPILib"
2324

2425
# The full version, including alpha/beta/rc tags
25-
release = '1.0'
26+
release = "1.0"
2627

2728

2829
# -- General configuration ---------------------------------------------------
@@ -36,7 +37,7 @@
3637
]
3738

3839
# Add any paths that contain templates here, relative to this directory.
39-
templates_path = ['_templates']
40+
templates_path = ["_templates"]
4041

4142
# List of patterns, relative to source directory, that match files and
4243
# directories to ignore when looking for source files.
@@ -49,4 +50,4 @@
4950
# The theme to use for HTML and HTML Help pages. See the documentation for
5051
# a list of builtin themes.
5152
#
52-
html_theme = 'furo'
53+
html_theme = "furo"

output.txt

60.9 KB
Binary file not shown.

sphinxext/opengraph/__init__.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from typing import Any, Dict
2-
from urllib.parse import urljoin
3-
from pathlib import Path
2+
from urllib.parse import urljoin, urlparse, urlunparse
3+
from pathlib import Path, PosixPath
44

55
import docutils.nodes as nodes
66
from sphinx.application import Sphinx
7+
from sphinx.util import logger
78

89
from .descriptionparser import get_description
910
from .titleparser import get_title
1011

12+
import os
13+
14+
1115
DEFAULT_DESCRIPTION_LENGTH = 200
1216

1317
# A selection from https://www.iana.org/assignments/media-types/media-types.xhtml#image
@@ -30,7 +34,10 @@ def make_tag(property: str, content: str) -> str:
3034

3135

3236
def get_tags(
33-
context: Dict[str, Any], doctree: nodes.document, config: Dict[str, Any]
37+
app: Sphinx,
38+
context: Dict[str, Any],
39+
doctree: nodes.document,
40+
config: Dict[str, Any],
3441
) -> str:
3542

3643
# Set length of description
@@ -53,6 +60,24 @@ def get_tags(
5360

5461
# type tag
5562
tags += make_tag("og:type", config["ogp_type"])
63+
if os.getenv("READTHEDOCS") and config["ogp_site_url"] is None:
64+
# readthedocs uses html_baseurl for sphinx > 1.8
65+
parse_result = urlparse(config["html_baseurl"])
66+
67+
if config["html_baseurl"] is None:
68+
raise EnvironmentError("ReadTheDocs did not provide a valid canonical URL!")
69+
70+
# Grab root url from canonical url
71+
config["ogp_site_url"] = urlunparse(
72+
(
73+
parse_result.scheme,
74+
parse_result.netloc,
75+
parse_result.path,
76+
"",
77+
"",
78+
"",
79+
)
80+
)
5681

5782
# url tag
5883
# Get the URL of the specific page
@@ -109,7 +134,7 @@ def html_page_context(
109134
doctree: nodes.document,
110135
) -> None:
111136
if doctree:
112-
context["metatags"] += get_tags(context, doctree, app.config)
137+
context["metatags"] += get_tags(app, context, doctree, app.config)
113138

114139

115140
def setup(app: Sphinx) -> Dict[str, Any]:

tests/conftest.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from bs4 import BeautifulSoup
33
from sphinx.testing.path import path
44

5+
from sphinx.application import Sphinx
6+
7+
58
pytest_plugins = "sphinx.testing.fixtures"
69

710

@@ -21,6 +24,12 @@ def _meta_tags(content):
2124
return BeautifulSoup(c, "html.parser").find_all("meta")
2225

2326

27+
def _og_meta_tags(content):
28+
return [
29+
tag for tag in _meta_tags(content) if tag.get("property", "").startswith("og:")
30+
]
31+
32+
2433
@pytest.fixture()
2534
def meta_tags(content):
2635
return _meta_tags(content)

tests/roots/test-rtd-default/conf.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* Item 1
2+
* Item 2
3+
4+
* Nested Item 1
5+
* Nested Item 2
6+
7+
* Item 3
8+
* Item 4

tests/roots/test-rtd-invalid/conf.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* Item 1
2+
* Item 2
3+
4+
* Nested Item 1
5+
* Nested Item 2
6+
7+
* Item 3
8+
* Item 4

tests/test_options.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import pytest
2+
from sphinx.application import Sphinx
3+
import conftest
4+
import os
25

36

47
def get_tag(tags, tag_type):
@@ -122,3 +125,36 @@ def test_skip_raw(og_meta_tags):
122125
description
123126
== "This text should be included. This text should also be included."
124127
)
128+
129+
130+
# use same as simple, as configuration is identical to overriden
131+
@pytest.mark.sphinx("html", testroot="simple")
132+
def test_rtd_override(app: Sphinx, monkeypatch):
133+
monkeypatch.setenv("READTHEDOCS", "True")
134+
app.config.html_baseurl = "https://failure.com"
135+
136+
app.build()
137+
tags = conftest._og_meta_tags(app)
138+
139+
assert get_tag_content(tags, "url") == "http://example.org/index.html"
140+
141+
142+
@pytest.mark.sphinx("html", testroot="rtd-default")
143+
def test_rtd_valid(app: Sphinx, monkeypatch):
144+
monkeypatch.setenv("READTHEDOCS", "True")
145+
app.config.html_baseurl = "https://failure.com"
146+
147+
app.build()
148+
tags = conftest._og_meta_tags(app)
149+
150+
assert get_tag_content(tags, "url") == "https://failure.com/index.html"
151+
152+
153+
# use rtd-default, as we are not changing configuration, but RTD variables
154+
@pytest.mark.sphinx("html", testroot="rtd-invalid")
155+
def test_rtd_invalid(app: Sphinx, monkeypatch):
156+
monkeypatch.setenv("READTHEDOCS", "True")
157+
app.config.html_baseurl = None
158+
159+
with pytest.raises(Exception):
160+
app.build()

0 commit comments

Comments
 (0)