Added blog stuff

This commit is contained in:
j4nk 2023-03-25 19:42:47 -04:00
parent 981210140a
commit e2f904f038
6 changed files with 171 additions and 23 deletions

6
blog/blogpost1.md4tj Normal file
View File

@ -0,0 +1,6 @@
@@TITLE Blog post 1
@@BLOGPOSTTITLE Blog Post 1
@@BLOGPOSTDATE 1677804254
# Blog post 1
blog text 1!

6
blog/blogpost2.md4tj Normal file
View File

@ -0,0 +1,6 @@
@@TITLE Blog post 2
@@BLOGPOSTTITLE Blog Post 2
@@BLOGPOSTDATE 1677804290
# Blog post 2
blog text 2 text!

100
md4tj-blog.el Normal file
View File

@ -0,0 +1,100 @@
;;; md4tj --- Summary
;;; Commentary:
;;; Code:
(require 'cl-lib)
;(load "./md4tj-util.el")
(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)))))
(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)
(replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline))
"")))
(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-links ()
"Get all links on the blog page."
(let ((base-url (md4tj-blog-base-url)))
(cl-map #'listp (lambda (s) (concat base-url s)) (md4tj-blog-names))))
(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."
(cl-map #'listp 'md4tj-blog-file (md4tj-blog-names)))
(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
(defun md4tj-all-blogs-list ()
"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)))))
(defun md4tj-all-blogs-list-elt-to-html (elt)
"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>"))
(defun md4tj-all-blogs-list-to-html (abl)
"Convert all blogs list ABL to html."
(mapconcat 'md4tj-all-blogs-list-elt-to-html (md4tj-all-blogs-list) "\n"))
(provide 'md4tj-blog)
;;; md4tj-blog.el ends here

36
md4tj-util.el Normal file
View File

@ -0,0 +1,36 @@
;;; md4tj-util --- Summary
;; A collection of useful utilities that can be used by anything
;; relating to md4tj
;;; Commentary:
;;; Code:
(require 'cl-lib)
(defun md4tj-util-getline ()
"Get current line from loaded buffer."
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
(defun md4tj-util-clean-multiline (line)
"Clean LINE of markdown syntax for ul's, ol's and code's."
(replace-regexp-in-string
"^```" ""
(replace-regexp-in-string
"^[0-9]+\\. " ""
(replace-regexp-in-string "^- " "" line))))
(defun md4tj-util-escape-chars (line)
"Escape characters in LINE that would be misinterpreted by the browser."
(string-replace
"<" "&lt;"
(string-replace ">" "&gt;" line)))
(defun md4tj-util-zip (ls)
"Turn list of lists LS into list of tuples."
(cl-labels ((zip-help (ll) (if (not (car ll)) nil (cons (cl-map 'listp #'car ll) (zip-help (cl-map 'listp 'cdr ll))))))
(zip-help ls)))
(provide 'md4tj-util)
;;; md4tj-util.el ends here

View File

@ -5,10 +5,7 @@
;;; Code: ;;; Code:
(require 'cl-lib) (require 'cl-lib)
;(load "md4tj-util.el")
(defun getline ()
"Get current line from loaded buffer."
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
(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."
@ -67,24 +64,10 @@
(replace-regexp-in-string (replace-regexp-in-string
"`\\(.*\\)`" "`\\(.*\\)`"
"<code>\\1</code>" line))))))))) "<code>\\1</code>" line)))))))))
(defun md4tj-clean-multiline (line)
"Clean LINE of markdown syntax for ul."
(replace-regexp-in-string
"^```" ""
(replace-regexp-in-string
"^[0-9]+\\. " ""
(replace-regexp-in-string "^- " "" line))))
(defun md4tj-clean-code-for-html (line)
"Escape characters in LINE that would be misinterpreted by the browser."
(string-replace
"<" "&lt;"
(string-replace ">" "&gt;" line)))
(defun md4tj-convert-line-to-html (line state) (defun md4tj-convert-line-to-html (line state)
"Process LINE with STATE and return html." "Process LINE with STATE and return html."
(let ((cleanline (md4tj-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))))
((string-match "^@@LASTUPDATED" line) (concat ((string-match "^@@LASTUPDATED" line) (concat
@ -115,7 +98,7 @@
(mapconcat #'md4tj-state-to-html state "\n") (mapconcat #'md4tj-state-to-html state "\n")
;; Body ;; Body
(cond ((or (eq (nth 1 state) 'code) (eq (nth 1 state) 'begincode)) (md4tj-clean-code-for-html cleanline)) (cond ((or (eq (nth 1 state) 'code) (eq (nth 1 state) 'begincode)) (md4tj-util-escape-chars cleanline))
((string-match "^#+ " cleanline) (md4tj-process-header (md4tj-process-line cleanline))) ((string-match "^#+ " cleanline) (md4tj-process-header (md4tj-process-line cleanline)))
((string= "---" cleanline) "<hr>") ;; horizontal line ((string= "---" cleanline) "<hr>") ;; horizontal line
((= (length cleanline) 0) "<br>") ;; blank line ((= (length cleanline) 0) "<br>") ;; blank line
@ -185,7 +168,7 @@
(let ((meta-list nil)) (let ((meta-list nil))
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward "^@@" nil t) (while (re-search-forward "^@@" nil t)
(setq meta-list (cons (split-string (getline)) meta-list))) (setq meta-list (cons (split-string (md4tj-util-getline)) meta-list)))
meta-list))) meta-list)))
(defun md4tj-list-to-tuple-list (list) (defun md4tj-list-to-tuple-list (list)
@ -226,7 +209,7 @@
(insert (with-current-buffer inbuf (md4tj-head))) (insert (with-current-buffer inbuf (md4tj-head)))
(insert (md4tj-begin-tag "body")) (insert (md4tj-begin-tag "body"))
(while (with-current-buffer inbuf (< (point) (point-max))) (while (with-current-buffer inbuf (< (point) (point-max)))
(setq line (with-current-buffer inbuf (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")) (insert (concat (md4tj-convert-line-to-html line fullstate) "\n"))
@ -245,7 +228,7 @@
(insert-file-contents mdfile) (insert-file-contents mdfile)
(goto-char (point-min)) (goto-char (point-min))
(while (< (point) (point-max)) (while (< (point) (point-max))
(setq line (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) "\n"))
(forward-line)) (forward-line))

17
test_blog.md4tj Normal file
View File

@ -0,0 +1,17 @@
@@RSSENABLE
@@RSSCHANNELtitle RSS Test
@@RSSCHANNELlink http://example.com
@@RSSCHANNELdescription An example blog for testing RSS capability of md4tj.
@@RSSCHANNELlanguage en-US
@@RSSCHANNELpubDate Tue, 10 Jun 2003 04:00:00 GMT
@@RSSCHANNELlastBuildDate Tue, 10 Jun 2003 04:00:00 GMT
@@RSSCHANNELdocs https://www.rssboard.org/rss-specification
@@RSSCHANNELmanagingEditor editor@example.com
@@RSSCHANNELwebMaster webmaster@example.com
@@BLOGENABLE
@@BLOGBASEURL ./blog/
@@BLOGBASEDIR ./blog/
@@BLOGPOST blogpost1
@@BLOGPOST blogpost2