sunpy/ablog

Docutils 0.18 returns an error when searching for author metadata

choldgraf opened this issue · 0 comments

Describe the bug

On a page that has page metadata, but not the metadata needed for ABlog, there seems to be a broken logic check for whether metadata fields exist and we get a list index error:

File "/home/choldgraf/github/forks/python/ebp/pydata-sphinx-theme/.nox/docs-live/lib/python3.9/site-packages/ablog/post.py", line 186, in apply
    metadata["author"] = list(docinfo.traverse(nodes.author))[0].astext()

I think this is because of the check here:

ablog/ablog/post.py

Lines 185 to 191 in 9545dc1

if docinfo.traverse(nodes.author):
metadata["author"] = list(docinfo.traverse(nodes.author))[0].astext()
# These two fields are special-cased in docutils
if docinfo.traverse(nodes.date):
metadata["date"] = list(docinfo.traverse(nodes.date))[0].astext()
if "blogpost" not in metadata and self.env.docname not in self.config.matched_blog_posts:
return None

I think for some reason traverse(nodes.author) will register as True even if the resulting list will be empty.

I suggest instead wrapping it in a list, which fixed it for me:

if list(docinfo.traverse(nodes.author)):

To Reproduce

  • Install docutils 0.18 and latest ablog
  • Put metadata on a non-ABlog page
  • Build the site and I think this should pop up

System Details

  • docutils version: 0.18
  • ablog version: latest release