Added RSS channel tag and subtag generation. Need item tags still

This commit is contained in:
j4nk 2023-01-04 21:14:48 -05:00
parent e22b0a5fe9
commit 981210140a
3 changed files with 82 additions and 3 deletions

58
md4tj-rss.el Normal file
View File

@ -0,0 +1,58 @@
;; 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,4 +1,4 @@
;;; md4tj_parse --- Summary
;;; md4tj --- Summary
;;; Commentary:
@ -250,5 +250,6 @@
(setq acc (concat acc (md4tj-convert-line-to-html line fullstate) "\n"))
(forward-line))
acc)))
(provide 'md4tj_parse)
;;; md4tj_parse.el ends here
(provide 'md4tj)
;;; md4tj.el ends here

20
test_rss.md4tj Normal file
View File

@ -0,0 +1,20 @@
@@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
@@DIV blogpost
### Blog post 1
Blog text 1
@@ENDDIV blogpost
@@DIV blogpost
### Blog post 2
Blog text 2
@@ENDDIV blogpost