Possibly correct first implementation of automatic RSS feed generation

This commit is contained in:
j4nk 2023-07-10 08:51:18 -04:00
parent e7c9fa4d53
commit 3f56c251cd
6 changed files with 64 additions and 19 deletions

1
feed.xml Normal file
View File

@ -0,0 +1 @@
<rss version="2.0"><channel><title>RSS Test</title><link>http://example.com</link><description>An example blog for testing RSS capability of md4tj.</description><language>en-US</language><pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate><managingEditor>editor@example.com</managingEditor><webMaster>webmaster@example.com</webMaster><docs>https://www.rssboard.org/rss-specification</docs><generator>md4tj-rss.el</generator><lastBuildDate>Mon, 10 Jul 2023 08:50:16 GMT</lastBuildDate></channel><item><title>Blog Post 2</title><author>j4nk</author><link>https://example.com/blog/blogpost2.html</link><guid>https://example.com/blog/blogpost2.html</guid><pubDate>Thu, 02 Mar 2023 19:44:50 EST</pubDate></item><item><title>Blog Post 1</title><author>j4nk</author><link>https://example.com/blog/blogpost1.html</link><guid>https://example.com/blog/blogpost1.html</guid><pubDate>Thu, 02 Mar 2023 19:44:14 EST</pubDate></item></rss>

View File

@ -94,8 +94,8 @@
"`\\(.*\\)`"
"<code>\\1</code>" line)))))))))
(defun md4tj-convert-line-to-html (line state inbuf)
"Process LINE with STATE and return html, INBUF provided."
(defun md4tj-convert-line-to-html (line state inbuf outfile)
"Process LINE with STATE and return html, INBUF provided with OUTFILE."
(let ((cleanline (md4tj-util-clean-multiline line)))
;; If this is a signal to include another file
(cond ((string-match "^@@INCLUDE" line) (md4tj-parse-to-string (nth 1 (split-string line))))
@ -106,6 +106,7 @@
((string-match "^@@DIV" line) (md4tj-begin-tag "div" (list (list "class" (nth 1 (split-string line))))))
((string-match "^@@ENDDIV" line) (md4tj-end-tag "div"))
((string-match "^@@BLOGINSERT" line) (with-current-buffer inbuf (md4tj-blog-html)))
((string-match "^@@RSSINSERT" line) (md4tj-rss-link-string (md4tj-rss-file-from-outfile outfile)))
((and (string-match "^$$.*$$" line) (eq (nth 1 state) 'normal)) ;; LaTeX formula
(shell-command (concat "./pnglatex"
" -d 300"
@ -234,6 +235,8 @@
(set-buffer inbuf)
(insert-file-contents mdfile)
(goto-char (point-min))
;; If we need RSS, go ahead and output the XML file here
(if (md4tj-rss-needs-rss) (md4tj-rss-to-file mdfile (md4tj-rss-file-from-outfile outfile)))
(set-buffer outbuf)
(insert (md4tj-begin))
(insert (with-current-buffer inbuf (md4tj-head)))
@ -242,7 +245,7 @@
(setq line (with-current-buffer inbuf (md4tj-util-getline)))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
;; Insert next line(s) into output file
(let ((linehtml (md4tj-convert-line-to-html line fullstate inbuf)))
(let ((linehtml (md4tj-convert-line-to-html line fullstate inbuf outfile)))
(insert (concat linehtml (if (string-empty-p linehtml) "" "\n"))))
;; Advance input file by a line
(with-current-buffer inbuf (forward-line)))
@ -264,7 +267,7 @@
(while (< (point) (point-max))
(setq line (md4tj-util-getline))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
(setq acc (concat acc (md4tj-convert-line-to-html line fullstate nil) "\n"))
(setq acc (concat acc (md4tj-convert-line-to-html line fullstate nil nil) "\n"))
(forward-line))
acc)))
@ -369,9 +372,9 @@
(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-begin-tag (tag &optional attrs self-close)
"Return the RSS text for TAG with ATTRS, SELF-CLOSE if necessary."
(concat "<" tag (mapconcat (lambda (attr) (concat " " (nth 0 attr) "=" "\"" (nth 1 attr) "\"")) attrs "") (if self-close "/" "") ">"))
(defun md4tj-rss-end-tag (tag)
"Return the RSS text for ending TAG."
@ -394,23 +397,67 @@
"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."
(defun md4tj-rss-channel (rsslink)
"Return the RSS text for channel in current buffer with RSSLINK."
(concat (md4tj-rss-begin-tag "channel")
;;(md4tj-rss-begin-tag "atom:link" (list (list "href" rsslink) (list "rel" "self") (list "type" "application/rss+xml")) t)
(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."
;; Elt is triple of
;; Title
;; Time
;; Link
(defun md4tj-rss-item (elt)
"Return RSS for ELT."
(concat (md4tj-rss-begin-tag "item")
(md4tj-rss-begin-tag "title") (nth 0 elt) (md4tj-rss-end-tag "title")
;; NOTE: change "j4nk" to author name
(md4tj-rss-begin-tag "author") "j4nk" (md4tj-rss-end-tag "author")
(md4tj-rss-begin-tag "link") (nth 2 elt) (md4tj-rss-end-tag "link")
(md4tj-rss-begin-tag "guid") (nth 2 elt) (md4tj-rss-end-tag "guid")
(md4tj-rss-begin-tag "pubDate") (format-time-string "%a, %d %b %Y %H:%M:%S %Z" (string-to-number (nth 1 elt))) (md4tj-rss-end-tag "pubDate")
(md4tj-rss-end-tag "item")))
(defun md4tj-rss-items ()
"Return all RSS for all elements."
(mapconcat 'md4tj-rss-item (md4tj-blog-all-blogs-list) ""))
(defun md4tj-rss (rsslink)
"Return the RSS text for the current buffer at RSSLINK."
(save-excursion
(when (md4tj-rss-needs-rss)
(concat
(md4tj-rss-begin)
(md4tj-rss-channel)
(md4tj-rss-channel rsslink)
(md4tj-rss-items)
(md4tj-rss-end-tag "rss")))))
(defun md4tj-rss-to-file (infile outfile)
"Write INFILE as RSS to OUTFILE."
(with-temp-buffer
(insert-file-contents infile)
(let ((outbuf (generate-new-buffer " outbuf" t))
(rss-str (md4tj-rss (concat (md4tj-blog-base-url) "/feed.xml"))))
(set-buffer outbuf)
(insert rss-str)
(write-region nil nil outfile nil))))
(defun md4tj-rss-link-string (rsslink)
"Return the html of RSS link to RSSLINK as string."
(concat (md4tj-begin-tag "div" (list (list "id" "rssicon"))) (md4tj-begin-tag "a" (list (list "href" rsslink))) (md4tj-begin-tag "img" (list (list "src" "rss.png") (list "alt" "RSS icon") (list "style" "width:20px;height:20px;"))) (md4tj-end-tag "img") (md4tj-end-tag "a") (md4tj-end-tag "div")))
(defun md4tj-rss-file-from-outfile (outfile)
"Return the filename that the RSS XML should be output to given OUTFILE."
(concat (file-name-directory outfile) "/feed.xml"))
;; End md4tj rss stuff
(provide 'md4tj)

BIN
rss.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

@ -4,6 +4,7 @@
<title>Test blog</title>
@ -18,11 +19,8 @@
</head>
<body>
<br>
<h1>Blogs</h1>
<br>
<a href=".//feed.xml"><img src="rss.png" alt="RSS icon" style="width:20px;height:20px;"></img></a>
<div class=blogpost>
<a href="https://example.com/blog/blogpost2.html"><h4>Blog Post 2</h4></a>
<h5>2023 March 02 19:44</h5>

View File

@ -9,9 +9,8 @@
@@RSSCHANNELmanagingEditor editor@example.com
@@RSSCHANNELwebMaster webmaster@example.com
@@TITLE Test blog
# Blogs
@@RSSINSERT
@@BLOGBASEURL https://example.com/blog/
@@BLOGBASEDIR ./blog/
@@BLOGINSERT

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 19:26:42 2023</p>
<p id="lastupdated">Last updated: Mon Jul 10 08:35:47 2023</p>
<br>