Tagged view (experimental)

Table of Contents

Go back to index.

Implementation details

This page is an experiment to implement tag based index page. And I have pretty much achieved that.

(defun p/get-posts-files ()
  "Get the file names of all the posts from content/."
  (remq nil (mapcar
             (lambda (filename)
               (if (not (string-match "draft.*" filename))
                   filename))
             (directory-files
              (if (string= (car (last (string-split (pwd) "/" ) 2)) "content")
                  "." "content/")
              nil directory-files-no-dot-files-regexp nil))))

(defun p/get-tags (filename)
  "Get the tags in FILENAME."
  (condition-case nil
      (save-excursion
        (find-file filename)
        (goto-char (point-min))
        (search-forward "#+tags:")
        (string-split
         (string-trim (thing-at-point 'line t) ".*tags:\s*")
         ":"))
    (error nil)))
(defun p/get-title (filename)
  "Get the title in FILENAME."
  (condition-case nil
      (save-excursion
        (find-file filename)
        (goto-char (point-min))
        (search-forward "#+title:")
        (string-trim (thing-at-point 'line t) ".*title:\s*"))
    (error nil)))
(defun p/tags-table()
  "Get a tags-table for all posts."
  (mapcar
   (lambda (post) `(,post ,(p/get-title post) ,(p/get-tags post))) (p/get-posts-files)))

(defun p/group-by-tags (tags-table)
  "Group TAGS-TABLE by the tags."
  (mapcar (lambda (tag)
            `(,tag ,(delete nil
                            (mapcar (lambda (item)
                                      (if (member tag (flatten-list (last item)))
                                          (take 2 item)))
                                    tags-table))))
          (delete-dups (flatten-tree (mapcar #'last tags-table)))))

(mapconcat
 (lambda (taglist)
   (let ((tag (car taglist))
         (articles (cadr taglist)))
     (format "* %s\n:PROPERTIES:\n:CUSTOM_ID: %s\n:END:\n%s\n" tag tag
             (mapconcat (lambda (article)
                          (format "- [[./%s][%s]]" (car article) (cadr article)))
                        articles "\n"))))
 (p/group-by-tags (p/tags-table)) "\n")

special

story

meta

programming

emacs

philosophy

linux

ram

freesoftware

git

tutorial

unix

Author: tusharhero (tusharhero@sdf.org)

tusharhero's pages is licensed under CC BY-ND 4.0

Date: 2024-08-02 Fri 14:05

Site built at: 2024-08-02 Fri 11:56

Emacs 30.0.50 (Org mode 9.6.15)