From c8bb99b30e770668675128ddd3fb67e87c9376e1 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Mon, 26 Aug 2019 01:57:22 +0530 Subject: Use grouping functions to handle multiple tags and languages. * ennu.el (ennu-many-to-many-group-by): New function. (ennu-publish): Use grouping functions seq-group-by and ennu-many-to-many-group-by instead of seq-filter to handle multiple tags and languages. --- ennu.el | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/ennu.el b/ennu.el index 76a2dbe..7169b9a 100644 --- a/ennu.el +++ b/ennu.el @@ -365,6 +365,22 @@ value of the last form in BODY." (unwind-protect ,@body (delete-directory ,temporary-directory t)))) +(defun ennu-many-to-many-group-by (function sequence) + "Apply FUNCTION to each element of SEQUENCE. +Separate the elements of SEQUENCE into an alist using the results +as keys. Keys are compared using `equal'." + (seq-reduce + (lambda (result element) + (seq-do + (lambda (key) + (map-put result key + (cons element (map-elt result key nil 'equal)) + 'equal)) + (funcall function element)) + result) + (seq-reverse sequence) + nil)) + (defun ennu-publish () (interactive) (let ((make-backup-files nil) @@ -375,9 +391,7 @@ value of the last form in BODY." (seq-do (apply-partially 'ennu--do-operation temporary-directory) (append - (let* ((posts (ennu-posts (ennu-setting :posts-directory))) - (tags (seq-uniq (seq-mapcat 'ennu-post-tags posts))) - (tongues (seq-uniq (seq-map 'ennu-post-tongue posts)))) + (let ((posts (ennu-posts (ennu-setting :posts-directory)))) (append ;; Publish posts (seq-map 'ennu-publish-post posts) @@ -388,22 +402,18 @@ value of the last form in BODY." (seq-take posts (ennu-setting :atom-feed-number-of-posts)))) ;; Publish indices (seq-map - (lambda (tongue) - (ennu-publish-index - "index" tongue blog-title posts-per-page - (seq-filter (lambda (post) - (string= tongue (ennu-post-tongue post))) - posts))) - tongues) - (seq-map - (lambda (tag) - (let ((posts (seq-filter (lambda (post) - (member tag (ennu-post-tags post))) - posts))) - (ennu-publish-index - (ennu--expand-relative tag (ennu-setting :tag-directory)) - (ennu-post-tongue (first posts)) tag "" posts-per-page posts))) - tags) + (pcase-lambda (`(,tongue . ,posts)) + (ennu-publish-index "index" tongue blog-title posts-per-page posts)) + (seq-group-by 'ennu-post-tongue posts)) + (seq-mapcat + (pcase-lambda (`(,tag . ,posts)) + (seq-map + (pcase-lambda (`(,tongue . ,posts)) + (ennu-publish-index + (ennu--expand-relative tag (ennu-setting :tag-directory)) + tongue tag posts-per-page posts)) + (seq-group-by 'ennu-post-tongue posts))) + (ennu-many-to-many-group-by 'ennu-post-tags posts)) ;; Publish links (seq-map 'ennu-publish-link (seq-uniq (seq-mapcat 'ennu-post-links posts))) -- cgit v1.2.3