United all md4tj files into one file

This commit is contained in:
j4nk 2023-07-09 19:27:12 -04:00
parent 524dba5bc0
commit e7c9fa4d53
6 changed files with 177 additions and 211 deletions

View File

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

View File

@ -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 "</" tag ">"))
(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

View File

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

177
md4tj.el
View File

@ -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
"<" "&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)))
;; 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 "<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-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 "</" tag ">"))
(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

View File

@ -24,11 +24,11 @@
<br>
<div class=blogpost>
<a href="https://example.com/blog/blogpost1.html"><h4>Blog Post 1</h4></a>
<a href="https://example.com/blog/blogpost2.html"><h4>Blog Post 2</h4></a>
<h5>2023 March 02 19:44</h5>
</div>
<div class=blogpost>
<a href="https://example.com/blog/blogpost2.html"><h4>Blog Post 2</h4></a>
<a href="https://example.com/blog/blogpost1.html"><h4>Blog Post 1</h4></a>
<h5>2023 March 02 19:44</h5>
</div>

View File

@ -75,7 +75,7 @@ int main() {
<p>This is a div that has monospace text.</p>
</div>
<p id="lastupdated">Last updated: Sun Jul 9 18:12:44 2023</p>
<p id="lastupdated">Last updated: Sun Jul 9 19:26:42 2023</p>
<br>