commit 1c94343bb2032a019249aea431a54e301b66c3c6
parent b4bb1631b7f9536216cfae9f3f58af2bbfec6c12
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date: Fri, 15 Apr 2022 22:07:04 -0700
Implement support for twitter cards
Diffstat:
5 files changed, 76 insertions(+), 15 deletions(-)
diff --git a/src/content.lisp b/src/content.lisp
@@ -10,7 +10,7 @@
(defun render-posts (posts pages &key stream title root-uri header links
stylesheet year month archive-date-list
- enable-rss)
+ enable-rss twitter-card)
(render-template* *posts-template* stream
:posts posts
:pages pages
@@ -22,11 +22,13 @@
:year year
:month month
:archive-date-list archive-date-list
- :enable-rss enable-rss))
+ :enable-rss enable-rss
+ :twitter-card twitter-card))
(defun render-page (page path pages &key stream title root-uri header links
stylesheet year month
- archive-date-list enable-rss)
+ archive-date-list enable-rss
+ twitter-card)
(render-template* *page-template* stream
:page page
:path path
@@ -39,7 +41,8 @@
:year year
:month month
:archive-date-list archive-date-list
- :enable-rss enable-rss))
+ :enable-rss enable-rss
+ :twitter-card twitter-card))
(defun render-rss (posts &key stream title link description image-url language
copyright managing-editor webmaster category)
@@ -57,7 +60,7 @@
:build-date (miniblog.format:rfc-822-format (now)) ))
(defun make-generator (&key title root-uri header links stylesheet)
- (lambda (posts pages &key year month archive-date-list enable-rss)
+ (lambda (posts pages &key year month archive-date-list enable-rss twitter-card)
(render-posts posts pages
:title title
:root-uri root-uri
@@ -67,10 +70,11 @@
:year year
:month month
:archive-date-list archive-date-list
- :enable-rss enable-rss)))
+ :enable-rss enable-rss
+ :twitter-card twitter-card)))
(defun make-page-generator (&key title root-uri header links stylesheet)
- (lambda (page path pages &key archive-date-list enable-rss)
+ (lambda (page path pages &key archive-date-list enable-rss twitter-card)
(render-page page path pages
:title title
:root-uri root-uri
@@ -78,6 +82,7 @@
:links links
:stylesheet stylesheet
:enable-rss enable-rss
+ :twitter-card twitter-card
:archive-date-list archive-date-list)))
(defun make-rss-generator (&key title link description image-url language
@@ -168,7 +173,7 @@
(mapcar #'get-year-month-pair-for-entry entries)
:test #'year-month-pairs-equal-p)))
-(defun gen-all-pages (pages &key prefix all-pages archive-date-list generator enable-rss)
+(defun gen-all-pages (pages &key prefix all-pages archive-date-list generator enable-rss twitter-card)
"Generate all pages in the tree rooted at PAGES. PREFIX may be supplied if this is a subtree, in which case ALL-PAGES should also be supplied so that the page nav can render correctly."
(labels ((with-paths (parent-path entries)
(mapcar #'(lambda (entry)
@@ -185,6 +190,7 @@
(or all-pages pages)
:generator generator
:archive-date-list archive-date-list
+ :twitter-card twitter-card
:enable-rss enable-rss))
(next-page-entry (list :page path next-page-content))
(queue-rest (cdr queue))
@@ -193,7 +199,7 @@
(append queue-rest queue-new))))))
(gen-pages-recur nil (with-paths prefix (getf pages :children)))))
-(defun gen-all (entries pages &key generator page-generator rss-generator)
+(defun gen-all (entries pages &key generator page-generator rss-generator twitter-card)
"Generate a list of all monthly pages and hierarchical pages as well as the index and RSS feed"
(labels
((gen-all-years (entries pages archive-date-list generator enable-rss)
@@ -204,6 +210,7 @@
(gen-month rest-entries
(car year-month) (cdr year-month)
pages
+ :twitter-card twitter-card
:enable-rss enable-rss
:archive-date-list archive-date-list
:generator generator)))
@@ -216,12 +223,14 @@
(append (list
(list :rss nil rss-content)
(list :index nil (gen-index entries pages
+ :twitter-card twitter-card
:enable-rss rss-content
:archive-date-list archive-date-list
:generator generator)))
(gen-all-pages pages
:archive-date-list archive-date-list
:generator page-generator
+ :twitter-card twitter-card
:enable-rss rss-content)
(gen-all-years entries pages
archive-date-list generator rss-content)))))
@@ -237,7 +246,7 @@
(funcall (or generator (make-rss-generator))
index-entries)))
-(defun gen-index (entries pages &key generator archive-date-list enable-rss)
+(defun gen-index (entries pages &key generator archive-date-list enable-rss twitter-card)
"Generate the index (front page) with the latest ten posts"
(if (not entries)
(funcall (or generator (make-generator)) nil)
@@ -246,10 +255,11 @@
(index-entries (top-ten entries)))
(funcall (or generator (make-generator))
index-entries pages
+ :twitter-card twitter-card
:enable-rss enable-rss
:archive-date-list archive-date-list))))
-(defun gen-month (entries year month pages &key generator archive-date-list enable-rss)
+(defun gen-month (entries year month pages &key generator archive-date-list enable-rss twitter-card)
"Generate a page with all posts for the given year and month"
(let ((archive-date-list (or archive-date-list
(get-archive-date-list entries))))
@@ -265,21 +275,24 @@
month-entries pages
:year year
:month month
+ :twitter-card twitter-card
:enable-rss enable-rss
:archive-date-list archive-date-list))
nil)
rest-entries))))
-(defun gen-post (entry pages &key generator archive-date-list enable-rss)
+(defun gen-post (entry pages &key generator archive-date-list enable-rss twitter-card)
"Generate a single post page"
(funcall (or generator (make-generator))
(list entry) pages
+ :twitter-card twitter-card
:enable-rss enable-rss
:archive-date-list archive-date-list))
-(defun gen-page (entry path pages &key generator archive-date-list enable-rss)
+(defun gen-page (entry path pages &key generator archive-date-list enable-rss twitter-card)
"Generate a single page"
(funcall (or generator (make-page-generator))
entry path pages
+ :twitter-card twitter-card
:enable-rss enable-rss
:archive-date-list archive-date-list))
diff --git a/src/miniblog.lisp b/src/miniblog.lisp
@@ -30,6 +30,14 @@
(defvar *blog-managing-editor* nil)
(defvar *blog-webmaster* nil)
+;;; The following are configurations for social media cards et al; currently
+;;; simple Twitter summary cards are supported. *TWITTER-CARD* can be a simple
+;;; truthy value in which case a basic card is generated or it can be a plist
+;;; with keys defining the card metadata. Currently supported keys:
+;;;
+;;; :IMAGE - URL to the image to populate the twitter:image tag
+(defvar *twitter-card* nil)
+
(defparameter +command-line-spec+
'((("add" #\a) :type boolean :optional t :documentation "Add new post")
(("edit" #\e) :type integer :optional t :documentation "Edit a post by ID")
@@ -168,9 +176,11 @@
(regenerate-file (list :index nil
(miniblog.content:gen-index entries pages
:enable-rss rss-content
+ :twitter-card *twitter-card*
:generator *generator*)))
(regenerate-file (car (miniblog.content:gen-month entries year month pages
:enable-rss rss-content
+ :twitter-card *twitter-card*
:generator *generator*)))))
(defun regenerate-page-and-parent-and-children (parent-path page-id pages)
@@ -184,6 +194,7 @@
parent-path
pages
:enable-rss rss-content
+ :twitter-card *twitter-card*
:generator *page-generator*
:archive-date-list archive-date-list)))
(loop for page in (getf subtree-root :children)
@@ -193,6 +204,7 @@
(miniblog.content:gen-page
page path pages
:enable-rss rss-content
+ :twitter-card *twitter-card*
:generator *page-generator*
:archive-date-list archive-date-list)))))
(let ((this-page (find-if #'(lambda (entry) (= page-id (getf entry :id))) (getf subtree-root :children))))
@@ -200,6 +212,7 @@
:prefix parent-path
:all-pages pages
:enable-rss rss-content
+ :twitter-card *twitter-card*
:generator *page-generator*
:archive-date-list archive-date-list)))))
@@ -208,7 +221,8 @@
(miniblog.data:get-pages)
:generator *generator*
:page-generator *page-generator*
- :rss-generator *rss-generator*)))
+ :rss-generator *rss-generator*
+ :twitter-card *twitter-card*)))
(mapcar #'regenerate-file all)))
(defun entries-in-month (year month)
diff --git a/src/packages.lisp b/src/packages.lisp
@@ -64,4 +64,4 @@
*generator* *page-generator* *rss-generator*
*blog-description* *blog-link* *blog-image-url* *blog-language*
*blog-category* *blog-copyright* *blog-managing-editor*
- *blog-webmaster*))
+ *blog-webmaster* *twitter-card*))
diff --git a/templates/page.dtl b/templates/page.dtl
@@ -1,4 +1,15 @@
{% extends "template.dtl" %}
+{% block html-head %}
+ {% super %}
+ {% if twitter-card %}
+ <meta name="twitter:card" content="summary">
+ {% if twitter-card.image %}
+ <meta name="twitter:image" content="{{ twitter-card.image }}">
+ {% endif %}
+ <meta name="twitter:title" content="{{ page.title }}">
+ <meta name="twitter:description" content="{{ page.content|markdown|strip-html|truncatechars:200 }}">
+ {% endif %}
+{% endblock %}
{% block main %}
{% if page.content %}
<h2>{{ page.title }}</h2>
diff --git a/templates/posts.dtl b/templates/posts.dtl
@@ -1,4 +1,27 @@
{% extends "template.dtl" %}
+{% block html-head %}
+ {% super %}
+ {% if twitter-card %}
+ <meta name="twitter:card" content="summary">
+ {% if twitter-card.image %}
+ <meta name="twitter:image" content="{{ twitter-card.image }}">
+ {% endif %}
+ {% if posts %}
+ {% comment %}
+ Djula doesn't offer a clean way to just grab the CAR from posts so this
+ is a placeholder.
+ {% endcomment %}
+ {% for post in posts %}
+ {% if forloop.first %}
+ <meta name="twitter:title" content="{{ post.title }}">
+ <meta name="twitter:description" content="{{ post.content|markdown|strip-html|truncatechars:200 }}">
+ {% endif %}
+ {% endfor %}
+ {% else %}
+ <meta name="twitter:title" content="{{ title }}">
+ {% endif %}
+ {% endif %}
+{% endblock %}
{% block main %}
{% if posts %}
{% for post in posts %}