diff --git a/md4tj-rss.el b/md4tj-rss.el new file mode 100644 index 0000000..fdcb18c --- /dev/null +++ b/md4tj-rss.el @@ -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 "")) + +(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.el b/md4tj.el index bda3b32..61ed320 100644 --- a/md4tj.el +++ b/md4tj.el @@ -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 diff --git a/test_rss.md4tj b/test_rss.md4tj new file mode 100644 index 0000000..9d09ad6 --- /dev/null +++ b/test_rss.md4tj @@ -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 \ No newline at end of file