From e7c9fa4d53c48108289c33b2275d70f92880bd17 Mon Sep 17 00:00:00 2001 From: j4nk Date: Sun, 9 Jul 2023 19:27:12 -0400 Subject: [PATCH] United all md4tj files into one file --- md4tj-blog.el | 111 ------------------------------- md4tj-rss.el | 58 ---------------- md4tj-util.el | 36 ---------- md4tj.el | 177 ++++++++++++++++++++++++++++++++++++++++++++++++- test_blog.html | 4 +- test_file.html | 2 +- 6 files changed, 177 insertions(+), 211 deletions(-) delete mode 100644 md4tj-blog.el delete mode 100644 md4tj-rss.el delete mode 100644 md4tj-util.el diff --git a/md4tj-blog.el b/md4tj-blog.el deleted file mode 100644 index 774e8b3..0000000 --- a/md4tj-blog.el +++ /dev/null @@ -1,111 +0,0 @@ -;;; md4tj --- Summary - - -;;; Commentary: - -;;; Code: - -(require 'cl-lib) -;; (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") - -;; 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))))) - -(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) - (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 () - "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)))) - -(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 ".html")) (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 (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) - "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-blog-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-blog-all-blogs-list-elt-to-html (elt) - "Convert abl list elt ELT to html." - (concat "
" "\n" - "

" (nth 0 elt) "

" "\n" - "
" (format-time-string "%Y %B %d %H:%M" (string-to-number (nth 1 elt))) "
" "\n" - "
")) - -(defun md4tj-blog-all-blogs-list-to-html (abl) - "Convert all blogs list ABL to html." - (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) -;;; md4tj-blog.el ends here diff --git a/md4tj-rss.el b/md4tj-rss.el deleted file mode 100644 index fdcb18c..0000000 --- a/md4tj-rss.el +++ /dev/null @@ -1,58 +0,0 @@ -;; md4tj-rss --- Summary - -;;; Commentary: - -;;; Code: -(require 'cl-lib) - -(defun md4tj-rss-needs-rss () - "Scans current buffer, returning t if needing rss." - (save-excursion - (goto-char (point-min)) - (if (search-forward-regexp "^@@RSSENABLE" nil t) t nil))) - -(defun md4tj-rss-begin-tag (tag &optional attrs) - "Return the RSS text for TAG with ATTRS." - (concat "<" tag (mapconcat (lambda (attr) (concat " " (nth 0 attr) "=" "\"" (nth 1 attr) "\"")) attrs "") ">")) - -(defun md4tj-rss-end-tag (tag) - "Return the RSS text for ending TAG." - (concat "")) - -(defun md4tj-rss-begin () - "Return the RSS text to begin RSS document." - (md4tj-rss-begin-tag "rss" (list (list "version" "2.0")))) - -(defun md4tj-rss-is-valid-channel-statement (toks) - "Return t if the car of TOKS is a valid channel statement." - ;; Note: user isn't allowed to set docs, generator, or lastBuildDate tags - (member (car toks) (list "title" "link" "description" "language" "copyright" "managingEditor" "webMaster" "pubDate" "category" "ttl" "rating"))) - -(defun md4tj-rss-get-channel-statements () - "Get all channel statements from current buffer." - (cl-remove-if-not #'md4tj-rss-is-valid-channel-statement (cl-map 'listp (lambda (toks) (cons (string-replace "@@RSSCHANNEL" "" (car toks)) (cdr toks))) (cl-map 'listp #'split-string (cl-remove-if-not (lambda (line) (string-match "^@@RSSCHANNEL.*" line)) (split-string (buffer-string) "\n")))))) - -(defun md4tj-rss-channel-statement-toks-to-rss (toks) - "Generate RSS syntax for RSS channel statement tokenized to TOKS." - (concat (md4tj-rss-begin-tag (car toks)) (mapconcat #'identity (cdr toks) " ") (md4tj-rss-end-tag (car toks)))) - -(defun md4tj-rss-channel () - "Return the RSS text for channel in current buffer." - (concat (md4tj-rss-begin-tag "channel") - (mapconcat #'md4tj-rss-channel-statement-toks-to-rss (md4tj-rss-get-channel-statements) "") - (md4tj-rss-begin-tag "docs") "https://www.rssboard.org/rss-specification" (md4tj-rss-end-tag "docs") - (md4tj-rss-begin-tag "generator") "md4tj-rss.el" (md4tj-rss-end-tag "generator") - (md4tj-rss-begin-tag "lastBuildDate") (format-time-string "%a, %d %b %Y %H:%M:%S GMT") (md4tj-rss-end-tag "lastBuildDate") - (md4tj-rss-end-tag "channel"))) - -(defun md4tj-rss () - "Return the RSS text for the current buffer." - (save-excursion - (when (md4tj-rss-needs-rss) - (concat - (md4tj-rss-begin) - (md4tj-rss-channel) - (md4tj-rss-end-tag "rss"))))) - -(provide 'md4tj-rss) -;;; md4tj-rss.el ends here diff --git a/md4tj-util.el b/md4tj-util.el deleted file mode 100644 index f54c0d7..0000000 --- a/md4tj-util.el +++ /dev/null @@ -1,36 +0,0 @@ -;;; 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 - "<" "<" - (string-replace ">" ">" 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 diff --git a/md4tj.el b/md4tj.el index 465212a..026c9c5 100644 --- a/md4tj.el +++ b/md4tj.el @@ -5,11 +5,37 @@ ;;; Code: (require 'cl-lib) - -(load-file "./md4tj-util.el") -(load-file "./md4tj-blog.el") (require 'subr-x) +;; Change this line to wherever dash.el is on the building system +;; Can't use (require) because load-path is set in .emacs +(load-file "~/.emacs.d/elpa/dash-20230415.2324/dash.el") +;; Basic utilities for subsequent stuff +(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 + "<" "<" + (string-replace ">" ">" 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))) +;; End basic utilities + +;; Normal md4tj stuff (defun md4tj-begin-tag (tag &optional attrs) "Return beginning html tag for TAG with optional ATTRS." (concat "<" tag @@ -242,5 +268,150 @@ (forward-line)) acc))) +;; End normal md4tj stuff + +;; md4tj blog stuff +(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) + (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 () + "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)))) + +(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 ".html")) (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 (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) + "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-blog-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-blog-all-blogs-list-elt-to-html (elt) + "Convert abl list elt ELT to html." + (concat "
" "\n" + "

" (nth 0 elt) "

" "\n" + "
" (format-time-string "%Y %B %d %H:%M" (string-to-number (nth 1 elt))) "
" "\n" + "
")) + +(defun md4tj-blog-all-blogs-list-to-html (abl) + "Convert all blogs list ABL to html." + (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)))) + +;; End md4tj blog stuff + +;; md4tj rss stuff +(defun md4tj-rss-needs-rss () + "Scans current buffer, returning t if needing rss." + (save-excursion + (goto-char (point-min)) + (if (search-forward-regexp "^@@RSSENABLE" nil t) t nil))) + +(defun md4tj-rss-begin-tag (tag &optional attrs) + "Return the RSS text for TAG with ATTRS." + (concat "<" tag (mapconcat (lambda (attr) (concat " " (nth 0 attr) "=" "\"" (nth 1 attr) "\"")) attrs "") ">")) + +(defun md4tj-rss-end-tag (tag) + "Return the RSS text for ending TAG." + (concat "")) + +(defun md4tj-rss-begin () + "Return the RSS text to begin RSS document." + (md4tj-rss-begin-tag "rss" (list (list "version" "2.0")))) + +(defun md4tj-rss-is-valid-channel-statement (toks) + "Return t if the car of TOKS is a valid channel statement." + ;; Note: user isn't allowed to set docs, generator, or lastBuildDate tags + (member (car toks) (list "title" "link" "description" "language" "copyright" "managingEditor" "webMaster" "pubDate" "category" "ttl" "rating"))) + +(defun md4tj-rss-get-channel-statements () + "Get all channel statements from current buffer." + (cl-remove-if-not #'md4tj-rss-is-valid-channel-statement (cl-map 'listp (lambda (toks) (cons (string-replace "@@RSSCHANNEL" "" (car toks)) (cdr toks))) (cl-map 'listp #'split-string (cl-remove-if-not (lambda (line) (string-match "^@@RSSCHANNEL.*" line)) (split-string (buffer-string) "\n")))))) + +(defun md4tj-rss-channel-statement-toks-to-rss (toks) + "Generate RSS syntax for RSS channel statement tokenized to TOKS." + (concat (md4tj-rss-begin-tag (car toks)) (mapconcat #'identity (cdr toks) " ") (md4tj-rss-end-tag (car toks)))) + +(defun md4tj-rss-channel () + "Return the RSS text for channel in current buffer." + (concat (md4tj-rss-begin-tag "channel") + (mapconcat #'md4tj-rss-channel-statement-toks-to-rss (md4tj-rss-get-channel-statements) "") + (md4tj-rss-begin-tag "docs") "https://www.rssboard.org/rss-specification" (md4tj-rss-end-tag "docs") + (md4tj-rss-begin-tag "generator") "md4tj-rss.el" (md4tj-rss-end-tag "generator") + (md4tj-rss-begin-tag "lastBuildDate") (format-time-string "%a, %d %b %Y %H:%M:%S GMT") (md4tj-rss-end-tag "lastBuildDate") + (md4tj-rss-end-tag "channel"))) + +(defun md4tj-rss () + "Return the RSS text for the current buffer." + (save-excursion + (when (md4tj-rss-needs-rss) + (concat + (md4tj-rss-begin) + (md4tj-rss-channel) + (md4tj-rss-end-tag "rss"))))) +;; End md4tj rss stuff + (provide 'md4tj) ;;; md4tj.el ends here diff --git a/test_blog.html b/test_blog.html index 3e90c1d..aaab687 100644 --- a/test_blog.html +++ b/test_blog.html @@ -24,11 +24,11 @@
-

Blog Post 1

+

Blog Post 2

2023 March 02 19:44
-

Blog Post 2

+

Blog Post 1

2023 March 02 19:44
diff --git a/test_file.html b/test_file.html index 3c066c6..d156cf5 100644 --- a/test_file.html +++ b/test_file.html @@ -75,7 +75,7 @@ int main() {

This is a div that has monospace text.

-

Last updated: Sun Jul 9 18:12:44 2023

+

Last updated: Sun Jul 9 19:26:42 2023