]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/commitdiff
Create an RSS feed with the full content of posts.
authorFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Wed, 12 Mar 2025 07:00:24 +0000 (15:00 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Wed, 12 Mar 2025 07:00:24 +0000 (15:00 +0800)
maak-website.el
source/index.org

index c93f1ccd7cab5039a0eda29b9678dc2d24db7e5a..0230ab83b6c674e9f870812da527bb3e382fd326 100644 (file)
@@ -1,12 +1,13 @@
 (require 'cl-lib)
 (require 'org)
 (require 'ox-publish)
-(add-to-list 'load-path "~/.emacs.d/elpa/ox-rss-20230408.231")
-(add-to-list 'load-path "~/.emacs.d/elpa/mustache-20230713.514")
-(add-to-list 'load-path "~/.emacs.d/elpa/s-20220902.1511")
-(add-to-list 'load-path "~/.emacs.d/elpa/dash-20240510.1327")
-(add-to-list 'load-path "~/.emacs.d/elpa/f-20240308.906 " )
-(add-to-list 'load-path "~/.emacs.d/elpa/htmlize-20240527.1456")
+(let ((default-directory "~/.emacs.d/elpa/"))
+  (normal-top-level-add-subdirs-to-load-path))
+(require 'mustache)
+(require 's)
+(require 'dash)
+(require 'f)
+(require 'htmlize)
 (require 'ox-rss)
 (load "~/.emacs.d/lisp/mustache-html.el")
 
@@ -26,6 +27,7 @@
 
 (setq org-export-time-stamp-file nil)
 (setq org-rss-use-entry-url-as-guid nil)
+(setq make-backup-files nil)
 
 (defun my-org-get-all-filetags ()
   "Get list of filetags from all org-files in my-blog-posts-folder."
@@ -147,18 +149,10 @@ Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the fir
                (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
                (preview (my-blog-get-preview abspath))
               )
-          ;; insert a horizontal line before every post, kill the first one
-          ;; before saving
           (insert (concat "* [[file:" relpath "][" title "]]\n"))
-          ;; add properties for `ox-rss.el' here
-         (let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
-               (rss-pubdate (format-time-string (cdr org-time-stamp-formats) (org-publish-find-date file project-plist))))
-           (org-mode)
-           (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
-           (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
-           (org-set-property "RSS_PERMALINK" rss-permalink)
-           (org-set-property "PUBDATE" rss-pubdate)
-           (org-set-property "RSS_TITLE" title))
+         (org-mode)
+         (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
+         (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
           ;; insert the date, preview, & read more link
          (insert "#+ATTR_HTML: :class card-header\n")
           (insert (concat "Published: " date "\n\n"))
@@ -166,16 +160,12 @@ Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the fir
          (insert "<section class=\"card-body\">\n")
          (insert "#+END_export\n")
          (insert preview)
-                                       ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
          (insert "\n")
           (insert (concat "[[file:" relpath "][Read More...]]\n"))
          (insert "#+BEGIN_export html\n")
          (insert "</section> <!-- END CARD-BODY-->\n")
          (insert "#+END_export\n")
          ))
-      ;; kill the first hrule to make this look OK
-      ;(goto-char (point-min))
-      ;(let ((kill-whole-line t)) (kill-line))
       ;; insert a title and save
       (insert "#+OPTIONS: title:nil\n")
       (insert "#+TITLE: Frederik Vanrenterghem's blog\n")
@@ -215,10 +205,55 @@ representation for the files to include, as returned by
                           "tag-index.org"))
     (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil)
     (replace-string "href=\"posts" "href=\"/posts"))
+  ;; Delete the sitemap and rss files created so they don't get picked up as original files in future publish actions.
   (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
   (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
-  (delete-file (file-name-concat my-blog-posts-folder "sitemap.org~")))
-  
+  (delete-file (file-name-concat my-blog-posts-folder "rss.org")))
+
+;; Define some custom functions to create the RSS feed. Main reason is the website has "read more" functionality,
+;; which is not nice to have in RSS.
+
+(defun fv/format-rss-feed (title list)
+  "Generate RSS feed, as a string.
+TITLE is the title of the RSS feed.  LIST is an internal
+representation for the files to include, as returned by
+`org-list-to-lisp'.  PROJECT is the current project."
+  (concat "#+TITLE: " title "\n"
+         "#+AUTHOR: Frederik Vanrenterghem\n"
+         "#+EMAIL: frederik@vanrenterghem.biz\n"
+         "#+OPTIONS: ^:nil\n" "\n"
+          (org-list-to-subtree list 1 '(:icount "" :istart ""))))
+
+(defun fv/format-rss-feed-entry (entry style project)
+  "Format ENTRY for the RSS feed.
+ENTRY is a file name.  STYLE is either 'list' or 'tree'.
+PROJECT is the current project."
+  (cond ((not (directory-name-p entry))
+         (let* ((file (org-publish--expand-file-name entry project))
+                (title (org-publish-find-title entry project))
+                (date (format-time-string "%Y-%m-%d %H:%M" (org-publish-find-date entry project)))
+                (link (concat (file-name-sans-extension entry) ".html")))
+           (with-temp-buffer
+            (org-mode)
+             (insert (format "* [[file:%s][%s]]\n" file title))
+            ;; add properties for `ox-rss.el' here
+            (org-set-property "RSS_PERMALINK" link)
+             (org-set-property "PUBDATE" date)
+            (org-set-property "RSS_TITLE" title)
+            ;; We simply chuck in the entire file.
+            ;; This assumes the file doesn't for instance start with a PROPERTIES drawer.
+             (insert-file-contents file)
+            ;; Ensure the list doesn't accidentally end due to blank lines at the end of
+            ;; entries. Still leaves problems when the entry has a double blank line in it
+            ;; as that ends an org list.
+            (goto-char (point-max))
+            (delete-blank-lines)
+             (buffer-string))))
+        ((eq style 'tree)
+         ;; Return only last subdir.
+         (file-name-nondirectory (directory-file-name entry)))
+        (t entry)))
+
 (setq org-publish-project-alist
       `(("landing"
          :base-directory ,my-blog-source-folder
@@ -245,7 +280,7 @@ representation for the files to include, as returned by
          :publishing-function org-mustache-html-publish-to-html
         :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
         :preparation-function my-blog-create-tags-files
-         :exclude "html*\\|assets*\\|index.org\\|sitemap.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
+         :exclude "html*\\|assets*\\|rss.org\\|index.org\\|sitemap.org"
         :html-content-class nil
          :section-numbers nil
          :with-toc nil
@@ -295,16 +330,26 @@ representation for the files to include, as returned by
        ("rss"
          :base-directory ,my-blog-posts-folder
          :base-extension "org"
+        :recursive nil
+        :exclude ,(regexp-opt '("rss.org" "index.org" "sitemap.org"))
          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
-         :publishing-function org-rss-publish-to-rss
+        :publishing-function org-rss-publish-to-rss
          :with-author t
         :title "Frederik Vanrenterghem's blog"
          :html-link-home ,my-blog-target-url
          :html-link-use-abs-url t
+        :html-link-org-files-as-html t
         :section-numbers nil
-         :exclude ".*"
-         :include ("sitemap.org")
+        :exclude ".*"
+         :include ("rss.org")
          :table-of-contents nil
+        :auto-sitemap t
+        :sitemap-filename "rss.org"
+        :sitemap-title "Frederik Vanrenterghem's blog"
+        :sitemap-style list
+        :sitemap-sort-files anti-chronologically
+        :sitemap-function fv/format-rss-feed
+        :sitemap-format-entry fv/format-rss-feed-entry
          )
        
         ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
index 237312ee2ab63058eb52559cb326e695f981235b..a088e3d52187e2ead36cacd77ec3ffebb49d2ef6 100644 (file)
@@ -25,7 +25,7 @@ was born and raised in Belgium, but except for a year back in 2013 where he live
         <a class="btn u-email" href="mailto:frederik@vanrenterghem.biz"><i class="fas fa-envelope big-icon"></i></a>
       </li>
       <li class="list-group-item">
-        <a class="btn" href="https://www.vanrenterghem.biz/posts/sitemap.xml"><i class="fas fa-rss big-icon"></i></a>
+        <a class="btn" href="https://www.vanrenterghem.biz/posts/rss.xml"><i class="fas fa-rss big-icon"></i></a>
       </li>
     </ul>
   </div>