From 0a2a49cfb899077566cec514095049a6e41a6cb7 Mon Sep 17 00:00:00 2001 From: TheTripleV Date: Thu, 9 Jul 2020 13:40:07 -0400 Subject: [PATCH] Add support for custom tags --- README.md | 7 +++++++ sphinxext/opengraph.py | 5 +++++ tests/roots/test-custom-tags/conf.py | 12 ++++++++++++ tests/roots/test-custom-tags/index.rst | 1 + tests/test_options.py | 4 ++++ 5 files changed, 29 insertions(+) create mode 100644 tests/roots/test-custom-tags/conf.py create mode 100644 tests/roots/test-custom-tags/index.rst diff --git a/README.md b/README.md index 8f46a22..9308242 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ These values are placed in the conf.py of your sphinx project. * This is not required. Link to image to show. * `ogp_type` * 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_tags` + * This is not required. List of custom tags and values to insert into all documents. ## Example Config @@ -45,4 +47,9 @@ ogp_site_url = "http://example.org/" ogp_image = "http://example.org/image.png" ogp_description_length = 300 ogp_type = "article" + +ogp_custom_tags = [ + ("og:ignore_canonical", "true"), +] + ``` diff --git a/sphinxext/opengraph.py b/sphinxext/opengraph.py index 3cec812..e068988 100644 --- a/sphinxext/opengraph.py +++ b/sphinxext/opengraph.py @@ -184,6 +184,10 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str, if image_url: tags += make_tag("og:image", image_url) + # custom tags + for custom_tag in config['ogp_custom_tags']: + tags += make_tag(*custom_tag) + return tags @@ -198,6 +202,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("ogp_image", None, "html") app.add_config_value("ogp_type", "website", "html") app.add_config_value("ogp_site_name", None, "html") + app.add_config_value("ogp_custom_tags", [], "html") app.connect('html-page-context', html_page_context) diff --git a/tests/roots/test-custom-tags/conf.py b/tests/roots/test-custom-tags/conf.py new file mode 100644 index 0000000..91413dd --- /dev/null +++ b/tests/roots/test-custom-tags/conf.py @@ -0,0 +1,12 @@ +extensions = ["sphinxext.opengraph"] + +master_doc = "index" +exclude_patterns = ["_build"] + +html_theme = "basic" + +ogp_site_url = "http://example.org/" + +ogp_custom_tags = [ + ("og:ignore_canonical", "true"), +] \ No newline at end of file diff --git a/tests/roots/test-custom-tags/index.rst b/tests/roots/test-custom-tags/index.rst new file mode 100644 index 0000000..a33871d --- /dev/null +++ b/tests/roots/test-custom-tags/index.rst @@ -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. \ No newline at end of file diff --git a/tests/test_options.py b/tests/test_options.py index 9dc6e50..d4ae643 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -86,3 +86,7 @@ def test_nested_list_punctuation(og_meta_tags): def test_skip_comments(og_meta_tags): assert get_tag_content(og_meta_tags, "description") == "This is text." + +@pytest.mark.sphinx("html", testroot="custom-tags") +def test_custom_tags(og_meta_tags): + assert get_tag_content(og_meta_tags, "ignore_canonical") == "true" \ No newline at end of file