diff --git a/.gitignore b/.gitignore index df83cd7..21c2e83 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ _tmp/ *~ .cask/ *.elc -build/ \ No newline at end of file +build/ +orgs/categories/ +orgs/tags/ diff --git a/build.el b/build.el index 718bf02..6be8cc7 100755 --- a/build.el +++ b/build.el @@ -51,24 +51,27 @@ (seq-reduce ;; all-tags is in (tags . tags->files) form (lambda (all-tags file) - (with-temp-buffer - (insert-file-contents file) - (let ((tags (mapcar #'car (org-get-buffer-tags)))) - (seq-reduce - (lambda (result tag) - (let ((tag-list (car result)) - (tag->file (cdr result))) - (cons - ;; Tag list - (if (member tag tag-list) tag-list (sort (cons tag tag-list) 'string<)) - ;; tag->file - (cons (cons tag - ;; Current value of the the given tag (all the files - ;; that contain that tag) - (append (list file) (cdr (assoc tag tag->file)))) - tag->file)))) - tags - all-tags)))) + (if (get-file-global-props file "PAGE") + ;; Ignore pages + all-tags + (with-temp-buffer + (insert-file-contents file) + (let ((tags (mapcar #'car (org-get-buffer-tags)))) + (seq-reduce + (lambda (result tag) + (let ((tag-list (car result)) + (tag->file (cdr result))) + (cons + ;; Tag list + (if (member tag tag-list) tag-list (sort (cons tag tag-list) 'string<)) + ;; tag->file + (cons (cons tag + ;; Current value of the the given tag (all the files + ;; that contain that tag) + (append (list file) (cdr (assoc tag tag->file)))) + tag->file)))) + tags + all-tags))))) (all-org-files) '())) @@ -149,7 +152,7 @@ Not pages." (mapconcat (lambda (cat) (let ((count (length (cdr (assoc cat (cdr categories)))))) - (format " - [[//categories/%s.html][%s(%s)]]" cat cat count))) + (format " - [[./%s.html][%s(%s)]]" cat cat count))) (car categories) "\n"))) @@ -160,7 +163,7 @@ Not pages." (mapconcat (lambda (tag) (let ((count (length (cdr (assoc tag (cdr tags)))))) - (format " - [[//tags/%s.html][%s(%s)]]" tag tag count))) + (format " - [[./%s.html][%s(%s)]]" tag tag count))) (car tags) "\n"))) @@ -179,6 +182,61 @@ Not pages." (latest-org-list)) +(defun pair-file-with-date (file) + "Return a pair for the given FILE with date as car and file as cdr." + (cons + (->epoch (get-file-global-props file "DATE")) + file)) + + +(defun create-tag-pages (project-dir) + "Create all the tag files in the PROJECT-DIR." + (let ((tags (get-all-tags))) + (mapcar + (lambda (tag) + (let ((out (format "%s/orgs/tags/%s.org" project-dir tag)) + (files (cdr (assoc tag (cdr tags))))) + (copy-template + (format "%s/templates/links_template.org" project-dir) + out + (mapconcat + (lambda (file-pair) + (format "- [[file:..%s][%s]]" + (replace-regexp-in-string (regexp-quote (from-root "/orgs")) "" (cdr file-pair) nil 'literal) + (get-file-global-props (cdr file-pair) "TITLE"))) + + (sort + (mapcar #'pair-file-with-date files) + (lambda (x y) (< (car x) (car y)))) + "\n") + tag))) + (car tags)))) + + +(defun create-category-pages (project-dir) + "Create all the category files in the PROJECT-DIR." + (let ((tags (get-all-categories))) + (mapcar + (lambda (tag) + (let ((out (format "%s/orgs/categories/%s.org" project-dir tag)) + (files (cdr (assoc tag (cdr tags))))) + (copy-template + (format "%s/templates/links_template.org" project-dir) + out + (mapconcat + (lambda (file-pair) + (format "- [[file:..%s][%s]]" + (replace-regexp-in-string (regexp-quote (from-root "/orgs")) "" (cdr file-pair) nil 'literal) + (get-file-global-props (cdr file-pair) "TITLE"))) + + (sort + (mapcar #'pair-file-with-date files) + (lambda (x y) (< (car x) (car y)))) + "\n") + tag))) + (car tags)))) + + (defun main () "The entry point to the build script." @@ -217,6 +275,9 @@ Not pages." (from-root "/orgs/tags/index.org") (tags-org-list)) + (create-tag-pages project-root) + (create-category-pages project-root) + (setq org-html-preamble #'preamble-fn) diff --git a/lisp/utils.el b/lisp/utils.el new file mode 100644 index 0000000..13f8928 --- /dev/null +++ b/lisp/utils.el @@ -0,0 +1,74 @@ +;;; utils.el --- The utility collections +;;; Version: 0.1.0 +;;; Package-Version 0.1.0 +;;; Commentary: +;;; Code: + +(defmacro comment (&rest body) + "Ignore the given BODY." + nil) + + +(defun prod-p () + "Return non-nil if we're in development mode." + (when (getenv "LXHOME_PROD") + t)) + + +(defun get-buffer-global-props (prop) + "Get a plists of global org properties PROP of current buffer." + (car + (org-element-map + (org-element-parse-buffer) + 'keyword + (lambda (el) + (when (string-match prop (org-element-property :key el)) + (org-element-property :value el)))))) + + +(defun get-file-global-props (file prop) + "Return the value of the given global PROP in the given org FILE." + (with-temp-buffer + (insert-file-contents file) + (message (get-buffer-global-props prop)))) + + +(comment + (setq org-directory "~/src/lxhome/orgs/") + (let ((f "./orgs/index.org")) + (get-file-global-props f "CATEGORY"))) + + +(defun ->epoch (date-str) + "Convert the given DATE-STR to epoch seconds." + ;; Just because it's easier to deal with date in bash rather than elisp + (string-to-number + (shell-command-to-string (concat (format "date -d %s" date-str) " +%s")))) + + +(defun replace-in-buffer (str replacement) + "Replace the given STR with its REPLACEMENT in current buffer." + (with-current-buffer (current-buffer) + (goto-char (point-min)) + (while (search-forward str nil t) + (replace-match replacement)))) + + +(defun copy-template (src dest content &optional title) + "Replace the placeholder in SRC with CONTENT and write it to DEST." + (with-temp-file dest + (insert-file-contents src) + (when title + (replace-in-buffer "<<