From 1fb1d6e4bbed635029d992fad1b86d647d190052 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 13 Feb 2021 18:47:03 +0000 Subject: [PATCH] Finish up the tag and category files --- .gitignore | 4 +- build.el | 101 ++++++++++++++++++++++++++++------- lisp/utils.el | 74 +++++++++++++++++++++++++ orgs/index.org | 1 - orgs/sitemap.inc | 6 ++- templates/blog.html | 3 +- templates/categories.org | 7 +++ templates/index.org | 16 ++++++ templates/links_template.org | 7 +++ templates/page-preamble.html | 1 + templates/post-preamble.html | 2 + templates/tags.org | 8 +++ 12 files changed, 205 insertions(+), 25 deletions(-) create mode 100644 lisp/utils.el create mode 100644 templates/categories.org create mode 100644 templates/index.org create mode 100644 templates/links_template.org create mode 100644 templates/page-preamble.html create mode 100644 templates/post-preamble.html create mode 100644 templates/tags.org 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 "<<>>" title)) + (replace-in-buffer "<<<links>>>" content))) + + +(defun get-file-tags (file) + "Returna list of tags for the given FILE." + (with-temp-buffer + (insert-file-contents file) + (mapcar #'car (org-get-buffer-tags)))) + + +(provide 'lisp/utils) +;;; utils.el ends here diff --git a/orgs/index.org b/orgs/index.org index da4c66a..a2364d4 100644 --- a/orgs/index.org +++ b/orgs/index.org @@ -15,4 +15,3 @@ ads ad asd asd as dasd asd - [[http://localhost:3003/essays/serene-blah.html][Serene blah]] - [[http://localhost:3003/essays/javascript-blah.html][javascript blah]] - - [[http://localhost:3003/essays/theindex.html][Index]] diff --git a/orgs/sitemap.inc b/orgs/sitemap.inc index 05006af..279bfd6 100644 --- a/orgs/sitemap.inc +++ b/orgs/sitemap.inc @@ -1,6 +1,10 @@ #+TITLE: lxsameer's nest -- [[file:theindex.org][Index]] +- [[file:categories/nil.org][<<<title>>>]] +- [[file:categories/SLA.org][SLA]] +- [[file:tags/ASDK.org][ASDK]] +- [[file:tags/BD.org][BD]] +- [[file:tags/ABD.org][ABD]] - [[file:essays/javascript-blah.org][javascript blah]] - [[file:tags/index.org][index]] - [[file:categories/index.org][index]] diff --git a/templates/blog.html b/templates/blog.html index b7b2768..9028c79 100644 --- a/templates/blog.html +++ b/templates/blog.html @@ -9,7 +9,7 @@ <header> <nav style="text-align: center;"> - <a href="/">HOME</a> | <a href="#">GPG</a> | <a href="/categories/">Categories</a> | <a href="/tags/">Tags</a> + <a href="/">HOME</a> | <a href="/categories/">Categories</a> | <a href="/tags/">Tags</a> | <a href="#">Code of Honor</a> | <a href="#">RSS</a> </nav> <hr/> </header> @@ -27,7 +27,6 @@ <span>Built with <a href="https://www.gnu.org/software/emacs/">Emacs Lisp</a> and <a href="https://orgmode.org/">org-mode</a>.</span><br/> <span><a href="https://devheroes.codes/lxsameer/lxhome">https://devheroes.codes/lxsameer/lxhome</a></span> </footer> - </div> </body> </html> diff --git a/templates/categories.org b/templates/categories.org new file mode 100644 index 0000000..2b879e4 --- /dev/null +++ b/templates/categories.org @@ -0,0 +1,7 @@ +#+SETUPFILE: ../../config.org +#+OPTIONS: toc:nil +#+EXPORT_FILE_NAME: index.html +#+PAGE: true +#+TITLE: Categories + +<<<links>>> diff --git a/templates/index.org b/templates/index.org new file mode 100644 index 0000000..9ea1e94 --- /dev/null +++ b/templates/index.org @@ -0,0 +1,16 @@ +#+SETUPFILE: ../config.org +#+OPTIONS: toc:nil +#+EXPORT_FILE_NAME: index.html +#+TAGS: ABD(a) BD(c) ASDK(k) +#+DATE: 2021-02-11 +#+CATEGORY: blah +#+TITLE: blah +#+PAGE: true + +* Welcome :ABD:BD: + :PROPERTIES: + :BLAH: s + :END: + blha bbasd asds asd a asd =asdasd= somer `asdasd` sadasd *asdasD* asdasd /asdasd/ asdasd. + ads ad asd asd as dasd asd +<<<links>>> diff --git a/templates/links_template.org b/templates/links_template.org new file mode 100644 index 0000000..0715acd --- /dev/null +++ b/templates/links_template.org @@ -0,0 +1,7 @@ +#+SETUPFILE: ../../config.org +#+OPTIONS: toc:nil +#+TITLE: <<<title>>> +#+PAGE: true + + +<<<links>>> diff --git a/templates/page-preamble.html b/templates/page-preamble.html new file mode 100644 index 0000000..6213adc --- /dev/null +++ b/templates/page-preamble.html @@ -0,0 +1 @@ +<h1>{{title}}</h1> diff --git a/templates/post-preamble.html b/templates/post-preamble.html new file mode 100644 index 0000000..aadb50d --- /dev/null +++ b/templates/post-preamble.html @@ -0,0 +1,2 @@ +<h1 class="title">{{title}}</h1> +<span class="tags">{{{tags}}}</span> - <time>{{date}}</time> diff --git a/templates/tags.org b/templates/tags.org new file mode 100644 index 0000000..1362940 --- /dev/null +++ b/templates/tags.org @@ -0,0 +1,8 @@ +#+SETUPFILE: ../../config.org +#+OPTIONS: toc:nil +#+EXPORT_FILE_NAME: index.html +#+PAGE: true +#+TITLE: Tags + + +<<<links>>>