md4tj/md4tj-blog.el

112 lines
3.9 KiB
EmacsLisp
Raw Normal View History

2023-03-25 19:42:47 -04:00
;;; md4tj --- Summary
2023-03-26 08:12:55 -04:00
2023-03-25 19:42:47 -04:00
;;; Commentary:
;;; Code:
(require 'cl-lib)
2023-03-26 22:19:07 -04:00
;; (load "./md4tj-util.el")
;; Remember to add (setq flycheck-emacs-lisp-load-path 'inherit) to .emacs for this to work with flycheck
(load "~/.emacs.d/elpa/dash-20230415.2324/dash.el")
2023-03-25 19:42:47 -04:00
2023-03-26 08:12:55 -04:00
;; NOT needed anymore
;; Presence of blog indicated by @@BLOGINSERT line
;; (defun md4tj-blog-enabled ()
;; "Determine if the current buffer corresponds to a blog."
;; (save-excursion
;; (goto-char (point-min))
;; (not (not (search-forward-regexp "^@@BLOGENABLE" (point-max) t)))))
2023-03-25 19:42:47 -04:00
(defun md4tj-blog-base-url ()
"Find base URL of blog."
(save-excursion
(goto-char (point-min))
(if (search-forward-regexp "^@@BLOGBASEURL" (point-max) t)
(replace-regexp-in-string "^@@BLOGBASEURL[ \\\t]+" "" (md4tj-util-getline))
nil)))
(defun md4tj-blog-base-dir ()
"Find base directory of blog."
(save-excursion
(goto-char (point-min))
(if (search-forward-regexp "^@@BLOGBASEDIR" (point-max) t)
2023-03-26 08:12:55 -04:00
(let ((tmp (replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline))))
(if (string-match "/$" tmp) tmp (concat tmp "/"))))))
2023-03-25 19:42:47 -04:00
2023-03-26 08:12:55 -04:00
;; (defun md4tj-blog-names ()
;; "Find all blogs on blog page."
;; (cl-map #'listp (lambda (l) (nth 1 l)) (cl-map #'listp 'split-string (cl-remove-if-not (lambda (s) (string-match "^@@BLOGPOST" s)) (split-string (buffer-string) "\n")))))
2023-03-25 19:42:47 -04:00
(defun md4tj-blog-names ()
2023-03-26 08:12:55 -04:00
"Get all blogs names from buffer."
(cl-map 'listp (lambda (s) (string-replace (md4tj-blog-base-dir) "" s)) (cl-map 'listp (lambda (s) (replace-regexp-in-string "\\.md4tj$" "" s)) (md4tj-blog-files))))
2023-03-25 19:42:47 -04:00
(defun md4tj-blog-links ()
"Get all links on the blog page."
(let ((base-url (md4tj-blog-base-url)))
2023-03-26 08:12:55 -04:00
(cl-map #'listp (lambda (s) (concat base-url s ".html")) (md4tj-blog-names))))
2023-03-25 19:42:47 -04:00
(defun md4tj-blog-file (name)
"Get full filename from NAME."
(concat (md4tj-blog-base-dir) name ".md4tj"))
(defun md4tj-blog-files ()
"Get all blog .md4tj files."
2023-03-26 08:12:55 -04:00
(cl-map 'listp (lambda (s) (concat (md4tj-blog-base-dir) s)) (cl-remove-if-not (lambda (s) (string-match ".*\\.md4tj$" s)) (directory-files (md4tj-blog-base-dir)))))
;(cl-map #'listp 'md4tj-blog-file (md4tj-blog-names)))
2023-03-25 19:42:47 -04:00
(defun md4tj-blog-title (f)
"Get blog title from file F."
(with-temp-buffer
(insert-file-contents f)
(goto-char (point-min))
(if (search-forward-regexp "^@@BLOGPOSTTITLE" (point-max) t)
(replace-regexp-in-string "^@@BLOGPOSTTITLE[ \t]+" "" (md4tj-util-getline))
"")))
(defun md4tj-blog-titles ()
"Get all blog titles in current file."
(cl-map #'listp 'md4tj-blog-title (md4tj-blog-files)))
(defun md4tj-blog-time (f)
"Get blog time (unix time) from file F."
(with-temp-buffer
(insert-file-contents f)
(goto-char (point-min))
(if (search-forward-regexp "^@@BLOGPOSTDATE" (point-max) t)
(replace-regexp-in-string "^@@BLOGPOSTDATE[ \t]+" "" (md4tj-util-getline))
"")))
(defun md4tj-blog-times ()
"Get all blog times in current file."
(cl-map #'listp 'md4tj-blog-time (md4tj-blog-files)))
;; triple of
;; Title
;; Time
;; Link
2023-03-26 08:12:55 -04:00
(defun md4tj-blog-all-blogs-list ()
2023-03-25 19:42:47 -04:00
"Get all blogs in current buffer."
(-sort (lambda (l1 l2) (< (string-to-number (nth 1 l1)) (string-to-number (nth 1 l2)))) (md4tj-util-zip (list (md4tj-blog-titles) (md4tj-blog-times) (md4tj-blog-links)))))
2023-03-26 08:12:55 -04:00
(defun md4tj-blog-all-blogs-list-elt-to-html (elt)
2023-03-25 19:42:47 -04:00
"Convert abl list elt ELT to html."
(concat "<div class=blogpost>" "\n"
"<a href=\"" (nth 2 elt) "\"><h4>" (nth 0 elt) "</h4></a>" "\n"
"<h5>" (format-time-string "%Y %B %d %H:%M" (string-to-number (nth 1 elt))) "</h5>" "\n"
"</div>"))
2023-03-26 08:12:55 -04:00
(defun md4tj-blog-all-blogs-list-to-html (abl)
2023-03-25 19:42:47 -04:00
"Convert all blogs list ABL to html."
2023-03-26 08:12:55 -04:00
(mapconcat 'md4tj-blog-all-blogs-list-elt-to-html (md4tj-blog-all-blogs-list) "\n"))
2023-03-25 19:42:47 -04:00
2023-03-26 08:12:55 -04:00
(defun md4tj-blog-html ()
"Return blog html."
(save-excursion
(goto-char (point-min))
(md4tj-blog-all-blogs-list-to-html (md4tj-blog-all-blogs-list))))
2023-03-25 19:42:47 -04:00
(provide 'md4tj-blog)
;;; md4tj-blog.el ends here