So, I spent about 3 hours working on getting Open Graph tags to work in the theme that I created. I've got mixed feeling about it because I started off by adding the tags to the base html template using an if statement and it seemed a little clunky.
{% if OPEN_GRAPH %}
<meta property="og:title" content="{{ SITENAME }}"/>
<meta property="og:type" content="website"/>
<meta property="og:description" content="{{ SITE_DESCRIPTION }}"/>
<meta property="og:url" content="{% SITE_URL %}"/>
{% endif OPEN_GRAPH %}
This worked for the site in general but doesn't translate to all the articles and this caused me to go into a some what deep dive into how Pelican and Jinja parses blocks and if statements. I started off with putting all the tags into the base and then doing individual changes to tags that need to be changed for.
BAH!!!
So, took a step back and looked into what plugins were already available. And there is was! Pelican-open_graph! I was home free! Or so I thought, until I tried to use it. The plugin has three problems.
It doesn't account for when someone does not have a modified date on their articles.
Luckily, someone had put a solution up. The change was forked about 7 months ago today and it looks like whoever created the open graph plugin has taken a break to work on other things.
It's a year old and hasn't made the changes for pelican regarding summaries of articles.
So, I had to do some debugging to find out that the code needed to be changed in the following way1.
ogtags.append(('og:description', instance.metadata.get('og_description',
instance.metadata.get('summary',
instance.summary))))
It also assumed that every article has some kind of tag.
I've decided to just punt this problem and add the tags to the 6 articles that don't have them, but than I took a second look at the problem because the perfectionist in me doesn't like to leave problems unsolved and I didn't want to track down what the particular articles were.
So, I just wrapped the code in a try block to make sure that I got this particular exception.
try:
for tag in instance.tags:
ogtags.append(('article:tag', tag.name))
except AttributeError:
ogtags.append(('article:tag', 'untagged'))
Results
Ultimately, I got to learn a little bit more about using pelican and knocked off a major milestone on getting this blog to where I'd like it to be.
-
I've got to branch and push this fix. I don't know how many other people are using pelican or this plugin but if I can save them or future me some time... ↩