Tagged view (experimental)

Table of Contents

This page is an experiment to implement tag based index page. I have pretty much achieved that. But I also want to implement tag based filtering and searching (I might unfortunately need a little of javascript for this).

(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%s" tag
             (mapconcat (lambda (article)
                          (format "- [[./%s][%s]]" (car article) (cadr article)))
                        articles "\n"))))
 (p/group-by-tags (p/tags-table)) "\n")

story

special

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:

Site built at: 2024-05-03 Fri 03:56

Emacs 29.0.50 (Org mode 9.5.5)