From aed8c20a9488f1ed4f4e316068cbafb07e8676ff Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Fri, 19 Jul 2019 19:37:54 -0400 Subject: Implement a blogroll --- haunt/jakob/utils/tags.scm | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 haunt/jakob/utils/tags.scm (limited to 'haunt/jakob/utils') diff --git a/haunt/jakob/utils/tags.scm b/haunt/jakob/utils/tags.scm new file mode 100644 index 0000000..87bec60 --- /dev/null +++ b/haunt/jakob/utils/tags.scm @@ -0,0 +1,57 @@ +;;; Copyright © 2019 Jakob L. Kreuze +;;; +;;; This program is free software; you can redistribute it and/or +;;; modify it under the terms of the GNU General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see +;;; . + +;;; Commentary: +;;; +;;; Common procedures for tag-based navigation. +;;; +;;; Code: + +(define-module (jakob utils tags) + #:use-module (srfi srfi-1) + #:export (group-by-tag + count-tags + tag-uri)) + +(define (group-by-tag items accessor) + "Return lists of the form (tag items) for each tag used in ITEMS. ACCESSOR is +a procedure that takes a single item as an argument and returns its tags." + (let ((table (make-hash-table))) + (for-each (lambda (item) + (let ((tags (accessor item))) + (for-each (lambda (tag) + (let ((current (hash-ref table tag))) + (if current + (hash-set! table tag (cons item current)) + (hash-set! table tag (list item))))) + tags))) + items) + (hash-fold alist-cons '() table))) + +(define (count-tags items accessor) + "Return lists of the form (tag-name count) summarizing tag usage across ENTRIES, +ordered such that tags with greater usage are at the beginning of the list, and +tags with less usage are at the end of the list. ACCESSOR is a procedure that +takes a single item as an argument and returns its tags." + (sort (map (lambda (tag) + (list (car tag) (length (cdr tag)))) + (group-by-tag items accessor)) + (lambda (a b) (> (cadr a) (cadr b))))) + +(define* (tag-uri prefix tag #:optional (extension ".html")) + "Return a URI relative to the site's root for a page listing entries in PREFIX +that are tagged with TAG." + (string-append prefix "/tag/" tag extension)) -- cgit v1.3