md4tj/md4tj-rss.el

59 lines
2.4 KiB
EmacsLisp
Raw Normal View History

;; 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