wpilibsuite / sphinxext-opengraph

Sphinx extension to generate unique OpenGraph metadata
https://sphinxext-opengraph.readthedocs.io
Other
74 stars 27 forks source link

Allow dirhtml builder without `ogp_site_url` #84

Closed rkdarst closed 1 year ago

rkdarst commented 1 year ago
hugovk commented 1 year ago

Please can you give an example of how exactly it fails with dirhtml?

(See also https://github.com/wpilibsuite/sphinxext-opengraph/issues/77 and https://github.com/wpilibsuite/sphinxext-opengraph/pull/78)

rkdarst commented 1 year ago

I saw those, looking again it seems the problem was introduced in #78.

It errors with this:

Extension error (sphinxext.opengraph):
Handler <function html_page_context at 0x7f98118190d0> for event 'html-page-context' threw an exception (exception: 'NoneType' object has no attribute 'replace')

Or a full traceback:

Traceback (most recent call last):
  File "/home/xxx/venv/lib/python3.9/site-packages/sphinx/events.py", line 94, in emit
    results.append(listener.handler(self.app, *args))
  File "/home/yyy/sphinxext-opengraph/sphinxext/opengraph/__init__.py", line 192, in html_page_context
    context["metatags"] += get_tags(app, context, doctree, app.config)
  File "/home/yyy/sphinxext-opengraph/sphinxext/opengraph/__init__.py", line 176, in get_tags
    [make_tag(p, c) for p, c in tags.items()]
  File "/home/yyy/sphinxext-opengraph/sphinxext/opengraph/__init__.py", line 176, in <listcomp>
    [make_tag(p, c) for p, c in tags.items()]
  File "/home/yyy/sphinxext-opengraph/sphinxext/opengraph/__init__.py", line 34, in make_tag
    content = content.replace('"', "&quot;")
AttributeError: 'NoneType' object has no attribute 'replace'

The root problem was a call make_tag(property='og:url', content=None) and then content is not a string so you can't content.replace(...). That None comes from the line I changed - before #78, it would always be a string for dirhtml. And due to a quirk in how it works now when it's not configured with an absolute url, if ogp_site_url was None, the function gets passed None instead of '' which would make a (little bit) more sense as a default value.

Other possible solutions:

TheTripleV commented 1 year ago

I think

set the default value of ogp_site_url to ''

is fine to restore previous behavior.

rkdarst commented 1 year ago

Updated and tested on my own site. Found and fixed one unintended consequence, but not sure if there could be others - doesn't seem so.

hugovk commented 1 year ago

The root problem was a call make_tag(property='og:url', content=None) and then content is not a string so you can't content.replace(...). That None comes from the line I changed - before #78, it would always be a string for dirhtml. And due to a quirk in how it works now when it's not configured with an absolute url, if ogp_site_url was None, the function gets passed None instead of '' which would make a (little bit) more sense as a default value.

Ah right, I had ogp_site_url set when testing #78. Can repro when removing it, and it works again with this PR. 👍

Maybe add some test cases to this PR to help avoid regressions in the future?

rkdarst commented 1 year ago

thanks for this and the other one!

rkdarst commented 1 year ago

So, I see that I forgot to git add the no-configuration test root. Would you like me to add this and change the test to use it? - I must have thought that was even more simple and less-configured than the readthedocs one

TheTripleV commented 1 year ago

I think it's ok. The rtd default one has no rtd or opengraph specific configuration.