Skip to content

Commit 8569b1c

Browse files
authored
Escape quotations in Open Graph description when smart quotes are disabled. (#49)
* Escape quotation marks when smart quotes are disabled * Add tests * Reformat with Black
1 parent 54ea5eb commit 8569b1c

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

sphinxext/opengraph/descriptionparser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,5 @@ def get_description(
124124

125125
mcv = DescriptionParser(description_length, known_titles, document)
126126
doctree.walkabout(mcv)
127-
return mcv.description
127+
# Parse quotation so they won't break html tags if smart quotes are disabled
128+
return mcv.description.replace('"', """)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
smartquotes = False
9+
10+
ogp_site_url = "http://example.org/"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"This text should appear in escaped quotation marks" This text should still appear as well "while this is once again in quotations"

tests/test_options.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ def test_skip_code_block(og_meta_tags):
149149
)
150150

151151

152+
@pytest.mark.sphinx("html", testroot="quotation-marks")
153+
def test_quotation_marks(og_meta_tags):
154+
# If smart quotes are disabled and the quotes aren't properly escaped, bs4 will fail to parse the tag and the content will be a empty string
155+
description = get_tag_content(og_meta_tags, "description")
156+
157+
assert (
158+
description
159+
== '"This text should appear in escaped quotation marks" This text should still appear as well "while this is once again in quotations"'
160+
)
161+
162+
152163
# use same as simple, as configuration is identical to overriden
153164
@pytest.mark.sphinx("html", testroot="simple")
154165
def test_rtd_override(app: Sphinx, monkeypatch):

0 commit comments

Comments
 (0)