Blog without RSS is finished

This commit is contained in:
j4nk 2023-03-26 08:12:55 -04:00
parent e2f904f038
commit b8ec3553f5
3 changed files with 45 additions and 27 deletions

View File

@ -1,5 +1,6 @@
;;; md4tj --- Summary ;;; md4tj --- Summary
;;; Commentary: ;;; Commentary:
;;; Code: ;;; Code:
@ -7,11 +8,13 @@
(require 'cl-lib) (require 'cl-lib)
;(load "./md4tj-util.el") ;(load "./md4tj-util.el")
(defun md4tj-blog-enabled () ;; NOT needed anymore
"Determine if the current buffer corresponds to a blog." ;; Presence of blog indicated by @@BLOGINSERT line
(save-excursion ;; (defun md4tj-blog-enabled ()
(goto-char (point-min)) ;; "Determine if the current buffer corresponds to a blog."
(not (not (search-forward-regexp "^@@BLOGENABLE" (point-max) t))))) ;; (save-excursion
;; (goto-char (point-min))
;; (not (not (search-forward-regexp "^@@BLOGENABLE" (point-max) t)))))
(defun md4tj-blog-base-url () (defun md4tj-blog-base-url ()
"Find base URL of blog." "Find base URL of blog."
@ -26,17 +29,20 @@
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(if (search-forward-regexp "^@@BLOGBASEDIR" (point-max) t) (if (search-forward-regexp "^@@BLOGBASEDIR" (point-max) t)
(replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline)) (let ((tmp (replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline))))
""))) (if (string-match "/$" tmp) tmp (concat tmp "/"))))))
;; (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")))))
(defun md4tj-blog-names () (defun md4tj-blog-names ()
"Find all blogs on blog page." "Get all blogs names from buffer."
(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"))))) (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))))
(defun md4tj-blog-links () (defun md4tj-blog-links ()
"Get all links on the blog page." "Get all links on the blog page."
(let ((base-url (md4tj-blog-base-url))) (let ((base-url (md4tj-blog-base-url)))
(cl-map #'listp (lambda (s) (concat base-url s)) (md4tj-blog-names)))) (cl-map #'listp (lambda (s) (concat base-url s ".html")) (md4tj-blog-names))))
(defun md4tj-blog-file (name) (defun md4tj-blog-file (name)
"Get full filename from NAME." "Get full filename from NAME."
@ -44,7 +50,8 @@
(defun md4tj-blog-files () (defun md4tj-blog-files ()
"Get all blog .md4tj files." "Get all blog .md4tj files."
(cl-map #'listp 'md4tj-blog-file (md4tj-blog-names))) (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)))
(defun md4tj-blog-title (f) (defun md4tj-blog-title (f)
"Get blog title from file F." "Get blog title from file F."
@ -77,24 +84,26 @@
;; Title ;; Title
;; Time ;; Time
;; Link ;; Link
(defun md4tj-all-blogs-list () (defun md4tj-blog-all-blogs-list ()
"Get all blogs in current buffer." "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))))) (-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)))))
(defun md4tj-all-blogs-list-elt-to-html (elt) (defun md4tj-blog-all-blogs-list-elt-to-html (elt)
"Convert abl list elt ELT to html." "Convert abl list elt ELT to html."
(concat "<div class=blogpost>" "\n" (concat "<div class=blogpost>" "\n"
"<a href=\"" (nth 2 elt) "\"><h4>" (nth 0 elt) "</h4></a>" "\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" "<h5>" (format-time-string "%Y %B %d %H:%M" (string-to-number (nth 1 elt))) "</h5>" "\n"
"</div>")) "</div>"))
(defun md4tj-all-blogs-list-to-html (abl) (defun md4tj-blog-all-blogs-list-to-html (abl)
"Convert all blogs list ABL to html." "Convert all blogs list ABL to html."
(mapconcat 'md4tj-all-blogs-list-elt-to-html (md4tj-all-blogs-list) "\n")) (mapconcat 'md4tj-blog-all-blogs-list-elt-to-html (md4tj-blog-all-blogs-list) "\n"))
(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))))
(provide 'md4tj-blog) (provide 'md4tj-blog)
;;; md4tj-blog.el ends here ;;; md4tj-blog.el ends here

View File

@ -5,7 +5,9 @@
;;; Code: ;;; Code:
(require 'cl-lib) (require 'cl-lib)
;(load "md4tj-util.el")
(load-file "./md4tj-util.el")
(load-file "./md4tj-blog.el")
(defun md4tj-begin-tag (tag &optional attrs) (defun md4tj-begin-tag (tag &optional attrs)
"Return beginning html tag for TAG with optional ATTRS." "Return beginning html tag for TAG with optional ATTRS."
@ -65,8 +67,8 @@
"`\\(.*\\)`" "`\\(.*\\)`"
"<code>\\1</code>" line))))))))) "<code>\\1</code>" line)))))))))
(defun md4tj-convert-line-to-html (line state) (defun md4tj-convert-line-to-html (line state inbuf)
"Process LINE with STATE and return html." "Process LINE with STATE and return html, INBUF provided."
(let ((cleanline (md4tj-util-clean-multiline line))) (let ((cleanline (md4tj-util-clean-multiline line)))
;; If this is a signal to include another file ;; If this is a signal to include another file
(cond ((string-match "^@@INCLUDE" line) (md4tj-parse-to-string (nth 1 (split-string line)))) (cond ((string-match "^@@INCLUDE" line) (md4tj-parse-to-string (nth 1 (split-string line))))
@ -76,6 +78,7 @@
(md4tj-end-tag "p"))) (md4tj-end-tag "p")))
((string-match "^@@DIV" line) (md4tj-begin-tag "div" (list (list "class" (nth 1 (split-string line)))))) ((string-match "^@@DIV" line) (md4tj-begin-tag "div" (list (list "class" (nth 1 (split-string line))))))
((string-match "^@@ENDDIV" line) (md4tj-end-tag "div")) ((string-match "^@@ENDDIV" line) (md4tj-end-tag "div"))
((string-match "^@@BLOGINSERT" line) (with-current-buffer inbuf (md4tj-blog-html)))
((and (string-match "^$$.*$$" line) (eq (nth 1 state) 'normal)) ;; LaTeX formula ((and (string-match "^$$.*$$" line) (eq (nth 1 state) 'normal)) ;; LaTeX formula
(shell-command (concat "./pnglatex" (shell-command (concat "./pnglatex"
" -d 300" " -d 300"
@ -212,13 +215,17 @@
(setq line (with-current-buffer inbuf (md4tj-util-getline))) (setq line (with-current-buffer inbuf (md4tj-util-getline)))
(setq fullstate (md4tj-next-state line (nth 1 fullstate))) (setq fullstate (md4tj-next-state line (nth 1 fullstate)))
;; Insert next line(s) into output file ;; Insert next line(s) into output file
(insert (concat (md4tj-convert-line-to-html line fullstate) "\n")) (let ((linehtml (md4tj-convert-line-to-html line fullstate inbuf)))
(insert (concat linehtml (if (string-empty-p linehtml) "" "\n"))))
;; Advance input file by a line ;; Advance input file by a line
(with-current-buffer inbuf (forward-line))) (with-current-buffer inbuf (forward-line)))
(insert (md4tj-finalize (nth 1 fullstate))) (insert (md4tj-finalize (nth 1 fullstate)))
;; Write outbuf to outfile ;; Write outbuf to outfile
(write-region nil nil outfile nil))) (write-region nil nil outfile nil)))
;; NOTE: Cannot md4tj-parse-to-string a file with a blog
;; This is fine, as included files are mostly meant to be stuff like
;; navbars, etc.
(defun md4tj-parse-to-string (mdfile) (defun md4tj-parse-to-string (mdfile)
"Parse MDFILE, return conversion to HTML as string." "Parse MDFILE, return conversion to HTML as string."
(with-temp-buffer (with-temp-buffer
@ -230,7 +237,7 @@
(while (< (point) (point-max)) (while (< (point) (point-max))
(setq line (md4tj-util-getline)) (setq line (md4tj-util-getline))
(setq fullstate (md4tj-next-state line (nth 1 fullstate))) (setq fullstate (md4tj-next-state line (nth 1 fullstate)))
(setq acc (concat acc (md4tj-convert-line-to-html line fullstate) "\n")) (setq acc (concat acc (md4tj-convert-line-to-html line fullstate nil) "\n"))
(forward-line)) (forward-line))
acc))) acc)))

View File

@ -8,10 +8,12 @@
@@RSSCHANNELdocs https://www.rssboard.org/rss-specification @@RSSCHANNELdocs https://www.rssboard.org/rss-specification
@@RSSCHANNELmanagingEditor editor@example.com @@RSSCHANNELmanagingEditor editor@example.com
@@RSSCHANNELwebMaster webmaster@example.com @@RSSCHANNELwebMaster webmaster@example.com
@@TITLE Test blog
@@BLOGENABLE # Blogs
@@BLOGBASEURL ./blog/
@@BLOGBASEURL https://example.com/blog/
@@BLOGBASEDIR ./blog/ @@BLOGBASEDIR ./blog/
@@BLOGINSERT
@@BLOGPOST blogpost1 ### Non blog text!!
@@BLOGPOST blogpost2