Closed rkdarst closed 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)
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('"', """)
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:
ogp_site_url
is not set (I think minimally working with no configuration is still useful, it does provide some useful description and titles)make_tag
to support content=None
ogp_site_url
to ''
.I think
set the default value of ogp_site_url to ''
is fine to restore previous behavior.
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.
The root problem was a call
make_tag(property='og:url', content=None)
and thencontent
is not a string so you can'tcontent.replace(...)
. ThatNone
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, ifogp_site_url
wasNone
, the function gets passedNone
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?
thanks for this and the other one!
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
I think it's ok. The rtd default one has no rtd or opengraph specific configuration.
/index
./index
work with dirhtml without any configuration, sinceurljoin
works withNone
as a first argument to leave the URLs as relative. This seemed like the the most consistent and least invasive change.