From d838dd584b1d53dbcc9647895d3339e317ac1506 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 16 May 2025 16:36:17 +0900 Subject: [PATCH 1/2] skip when building embedded html --- sphinxext/opengraph/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinxext/opengraph/__init__.py b/sphinxext/opengraph/__init__.py index 61ee8a9..04bd080 100644 --- a/sphinxext/opengraph/__init__.py +++ b/sphinxext/opengraph/__init__.py @@ -60,7 +60,7 @@ def html_page_context( context: dict[str, Any], doctree: nodes.document, ) -> None: - if doctree: + if doctree and not app.builder.embedded: context['metatags'] += get_tags( context, doctree, From 2d4fe514695c0ee346ce939916e352166372419a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 4 Aug 2025 14:21:19 +0900 Subject: [PATCH 2/2] add test for ePub --- tests/conftest.py | 10 ++++------ tests/roots/test-simple/conf.py | 4 ++++ tests/test_options.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 315e341..0cb973c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -32,11 +32,11 @@ def content(app): return app -def _meta_tags(content, subdir=None): +def _meta_tags(content, subdir=None, target='index.html'): if subdir is None: - c = (content.outdir / 'index.html').read_text(encoding='utf-8') + c = (content.outdir / target).read_text(encoding='utf-8') else: - c = (content.outdir / subdir / 'index.html').read_text(encoding='utf-8') + c = (content.outdir / subdir / target).read_text(encoding='utf-8') return BeautifulSoup(c, 'html.parser').find_all('meta') @@ -53,9 +53,7 @@ def meta_tags(content): @pytest.fixture def og_meta_tags(content): - return [ - tag for tag in _meta_tags(content) if tag.get('property', '').startswith('og:') - ] + return _og_meta_tags(content) @pytest.fixture diff --git a/tests/roots/test-simple/conf.py b/tests/roots/test-simple/conf.py index 2bd3512..e7b6747 100644 --- a/tests/roots/test-simple/conf.py +++ b/tests/roots/test-simple/conf.py @@ -8,3 +8,7 @@ html_theme = 'basic' ogp_site_url = 'http://example.org/en/latest/' + +# needed for ePub build +epub_copyright = 'sphinxext.opengraph team' +version = '1.0' diff --git a/tests/test_options.py b/tests/test_options.py index c780435..ee15231 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -42,6 +42,16 @@ def test_simple(og_meta_tags): ) +@pytest.mark.sphinx('epub', testroot='simple') +def test_epub(content: Sphinx): + """Ogp tags are disabled for ePub build""" + meta_tags = conftest._meta_tags(content, target='index.xhtml') + og_meta_tags = [ + tag for tag in meta_tags if tag.get('property', '').startswith('og:') + ] + assert len(og_meta_tags) == 0 + + @pytest.mark.sphinx('html', testroot='meta-name-description') def test_meta_name_description(meta_tags): og_description = get_tag_content(meta_tags, 'description')