Skip to content

Commit 952f485

Browse files
authored
Complete Type Hints and Add support for custom tags (#13)
* Complete type hints - Sphinx's included coverage extension was used as reference * Add support for custom tags
1 parent 2f7332d commit 952f485

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ These values are placed in the conf.py of your sphinx project.
2828
* This is not required. Link to image to show.
2929
* `ogp_type`
3030
* 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.
31+
* `ogp_custom_meta_tags`
32+
* This is not required. List of custom html snippets to insert.
3133

3234
## Example Config
3335

@@ -45,4 +47,9 @@ ogp_site_url = "http://example.org/"
4547
ogp_image = "http://example.org/image.png"
4648
ogp_description_length = 300
4749
ogp_type = "article"
50+
51+
ogp_custom_meta_tags = [
52+
'<meta property="og:ignore_canonical" content="true" />',
53+
]
54+
4855
```

sphinxext/opengraph.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from typing import Any, Dict, Iterable, Sequence, Tuple
12
from urllib.parse import urljoin
23
import docutils.nodes as nodes
34
import string
45
from html.parser import HTMLParser
5-
6+
import sphinx
7+
from sphinx.application import Sphinx
68

79
DEFAULT_DESCRIPTION_LENGTH = 200
810

@@ -18,13 +20,13 @@ def __init__(self):
1820
self.text_outside_tags = ""
1921
self.level = 0
2022

21-
def handle_starttag(self, tag, attrs):
23+
def handle_starttag(self, tag, attrs) -> None:
2224
self.level += 1
2325

24-
def handle_endtag(self, tag):
26+
def handle_endtag(self, tag) -> None:
2527
self.level -= 1
2628

27-
def handle_data(self, data):
29+
def handle_data(self, data) -> None:
2830
self.text += data
2931
if self.level == 0:
3032
self.text_outside_tags += data
@@ -34,7 +36,7 @@ class OGMetadataCreatorVisitor(nodes.NodeVisitor):
3436
Finds the title and creates a description from a doctree
3537
"""
3638

37-
def __init__(self, desc_len, known_titles=None, document=None):
39+
def __init__(self, desc_len: int, known_titles: Iterable[str] = None, document: nodes.document = None):
3840

3941
# Hack to prevent requirement for the doctree to be passed in.
4042
# It's only used by doctree.walk(...) to print debug messages.
@@ -135,7 +137,7 @@ def make_tag(property: str, content: str) -> str:
135137
return f'<meta property="{property}" content="{content}" />\n '
136138

137139

138-
def get_tags(context, doctree, config):
140+
def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str, Any]) -> str:
139141

140142
# Set length of description
141143
try:
@@ -182,20 +184,24 @@ def get_tags(context, doctree, config):
182184
if image_url:
183185
tags += make_tag("og:image", image_url)
184186

187+
# custom tags
188+
tags += '\n'.join(config['ogp_custom_meta_tags'])
189+
185190
return tags
186191

187192

188-
def html_page_context(app, pagename, templatename, context, doctree):
193+
def html_page_context(app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: nodes.document) -> None:
189194
if doctree:
190195
context['metatags'] += get_tags(context, doctree, app.config)
191196

192197

193-
def setup(app):
198+
def setup(app: Sphinx) -> Dict[str, Any]:
194199
app.add_config_value("ogp_site_url", None, "html")
195200
app.add_config_value("ogp_description_length", DEFAULT_DESCRIPTION_LENGTH, "html")
196201
app.add_config_value("ogp_image", None, "html")
197202
app.add_config_value("ogp_type", "website", "html")
198203
app.add_config_value("ogp_site_name", None, "html")
204+
app.add_config_value("ogp_custom_meta_tags", [], "html")
199205

200206
app.connect('html-page-context', html_page_context)
201207

tests/roots/test-custom-tags/conf.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
ogp_site_url = "http://example.org/"
9+
10+
ogp_custom_meta_tags = [
11+
'<meta property="og:ignore_canonical" content="true" />',
12+
]
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

tests/test_options.py

+5
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ def test_nested_list_punctuation(og_meta_tags):
8686
def test_skip_comments(og_meta_tags):
8787
assert get_tag_content(og_meta_tags, "description") == "This is text."
8888

89+
90+
@pytest.mark.sphinx("html", testroot="custom-tags")
91+
def test_custom_tags(og_meta_tags):
92+
assert get_tag_content(og_meta_tags, "ignore_canonical") == "true"
93+

0 commit comments

Comments
 (0)