Finish up the tag and category files

This commit is contained in:
Sameer Rahmani 2021-02-13 18:47:03 +00:00
parent dc4b171c16
commit 1fb1d6e4bb
12 changed files with 205 additions and 25 deletions

4
.gitignore vendored
View File

@ -5,4 +5,6 @@ _tmp/
*~ *~
.cask/ .cask/
*.elc *.elc
build/ build/
orgs/categories/
orgs/tags/

101
build.el
View File

@ -51,24 +51,27 @@
(seq-reduce (seq-reduce
;; all-tags is in (tags . tags->files) form ;; all-tags is in (tags . tags->files) form
(lambda (all-tags file) (lambda (all-tags file)
(with-temp-buffer (if (get-file-global-props file "PAGE")
(insert-file-contents file) ;; Ignore pages
(let ((tags (mapcar #'car (org-get-buffer-tags)))) all-tags
(seq-reduce (with-temp-buffer
(lambda (result tag) (insert-file-contents file)
(let ((tag-list (car result)) (let ((tags (mapcar #'car (org-get-buffer-tags))))
(tag->file (cdr result))) (seq-reduce
(cons (lambda (result tag)
;; Tag list (let ((tag-list (car result))
(if (member tag tag-list) tag-list (sort (cons tag tag-list) 'string<)) (tag->file (cdr result)))
;; tag->file (cons
(cons (cons tag ;; Tag list
;; Current value of the the given tag (all the files (if (member tag tag-list) tag-list (sort (cons tag tag-list) 'string<))
;; that contain that tag) ;; tag->file
(append (list file) (cdr (assoc tag tag->file)))) (cons (cons tag
tag->file)))) ;; Current value of the the given tag (all the files
tags ;; that contain that tag)
all-tags)))) (append (list file) (cdr (assoc tag tag->file))))
tag->file))))
tags
all-tags)))))
(all-org-files) (all-org-files)
'())) '()))
@ -149,7 +152,7 @@ Not pages."
(mapconcat (mapconcat
(lambda (cat) (lambda (cat)
(let ((count (length (cdr (assoc cat (cdr categories)))))) (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) (car categories)
"\n"))) "\n")))
@ -160,7 +163,7 @@ Not pages."
(mapconcat (mapconcat
(lambda (tag) (lambda (tag)
(let ((count (length (cdr (assoc tag (cdr tags)))))) (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) (car tags)
"\n"))) "\n")))
@ -179,6 +182,61 @@ Not pages."
(latest-org-list)) (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 () (defun main ()
"The entry point to the build script." "The entry point to the build script."
@ -217,6 +275,9 @@ Not pages."
(from-root "/orgs/tags/index.org") (from-root "/orgs/tags/index.org")
(tags-org-list)) (tags-org-list))
(create-tag-pages project-root)
(create-category-pages project-root)
(setq org-html-preamble #'preamble-fn) (setq org-html-preamble #'preamble-fn)

74
lisp/utils.el Normal file
View File

@ -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>>>" 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

View File

@ -15,4 +15,3 @@
ads ad asd asd as dasd asd ads ad asd asd as dasd asd
- [[http://localhost:3003/essays/serene-blah.html][Serene blah]] - [[http://localhost:3003/essays/serene-blah.html][Serene blah]]
- [[http://localhost:3003/essays/javascript-blah.html][javascript blah]] - [[http://localhost:3003/essays/javascript-blah.html][javascript blah]]
- [[http://localhost:3003/essays/theindex.html][Index]]

View File

@ -1,6 +1,10 @@
#+TITLE: lxsameer's nest #+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:essays/javascript-blah.org][javascript blah]]
- [[file:tags/index.org][index]] - [[file:tags/index.org][index]]
- [[file:categories/index.org][index]] - [[file:categories/index.org][index]]

View File

@ -9,7 +9,7 @@
<header> <header>
<nav style="text-align: center;"> <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> </nav>
<hr/> <hr/>
</header> </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>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> <span><a href="https://devheroes.codes/lxsameer/lxhome">https://devheroes.codes/lxsameer/lxhome</a></span>
</footer> </footer>
</div> </div>
</body> </body>
</html> </html>

7
templates/categories.org Normal file
View File

@ -0,0 +1,7 @@
#+SETUPFILE: ../../config.org
#+OPTIONS: toc:nil
#+EXPORT_FILE_NAME: index.html
#+PAGE: true
#+TITLE: Categories
<<<links>>>

16
templates/index.org Normal file
View File

@ -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>>>

View File

@ -0,0 +1,7 @@
#+SETUPFILE: ../../config.org
#+OPTIONS: toc:nil
#+TITLE: <<<title>>>
#+PAGE: true
<<<links>>>

View File

@ -0,0 +1 @@
<h1>{{title}}</h1>

View File

@ -0,0 +1,2 @@
<h1 class="title">{{title}}</h1>
<span class="tags">{{{tags}}}</span> - <time>{{date}}</time>

8
templates/tags.org Normal file
View File

@ -0,0 +1,8 @@
#+SETUPFILE: ../../config.org
#+OPTIONS: toc:nil
#+EXPORT_FILE_NAME: index.html
#+PAGE: true
#+TITLE: Tags
<<<links>>>