Skip to content

Commit

Permalink
Escape quotations in Open Graph description when smart quotes are dis…
Browse files Browse the repository at this point in the history
…abled. (#49)

* Escape quotation marks when smart quotes are disabled

* Add tests

* Reformat with Black
  • Loading branch information
ItayZiv authored Dec 4, 2021
1 parent 54ea5eb commit 8569b1c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sphinxext/opengraph/descriptionparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@ def get_description(

mcv = DescriptionParser(description_length, known_titles, document)
doctree.walkabout(mcv)
return mcv.description
# Parse quotation so they won't break html tags if smart quotes are disabled
return mcv.description.replace('"', """)
10 changes: 10 additions & 0 deletions tests/roots/test-quotation-marks/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extensions = ["sphinxext.opengraph"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

smartquotes = False

ogp_site_url = "http://example.org/"
1 change: 1 addition & 0 deletions tests/roots/test-quotation-marks/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"This text should appear in escaped quotation marks" This text should still appear as well "while this is once again in quotations"
11 changes: 11 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ def test_skip_code_block(og_meta_tags):
)


@pytest.mark.sphinx("html", testroot="quotation-marks")
def test_quotation_marks(og_meta_tags):
# 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
description = get_tag_content(og_meta_tags, "description")

assert (
description
== '"This text should appear in escaped quotation marks" This text should still appear as well "while this is once again in quotations"'
)


# use same as simple, as configuration is identical to overriden
@pytest.mark.sphinx("html", testroot="simple")
def test_rtd_override(app: Sphinx, monkeypatch):
Expand Down

0 comments on commit 8569b1c

Please sign in to comment.