ox-jamzattack

Minor tweaks for ox-publish
git clone https://git.git.jamzattack.xyz/ox-jamzattack
Log | Files | Refs

ox-jamzattack.el (4762B)


      1 ;;; ox-jamzattack.el --- ox-publish helper functions for jamzattack.xyz  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020  Jamie Beardslee
      4 
      5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz>
      6 ;; Version: 2021.11.25
      7 ;; Keywords: 
      8 
      9 ;; This program is free software; you can redistribute it and/or modify
     10 ;; it under the terms of the GNU General Public License as published by
     11 ;; the Free Software Foundation, either version 3 of the License, or
     12 ;; (at your option) any later version.
     13 
     14 ;; This program is distributed in the hope that it will be useful,
     15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 ;; GNU General Public License for more details.
     18 
     19 ;; You should have received a copy of the GNU General Public License
     20 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     21 
     22 ;;; Commentary:
     23 
     24 ;; 
     25 
     26 ;;; Code:
     27 (require 'ox-publish)
     28 (require 'ox-html)
     29 (require 'cl-seq)
     30 
     31 (eval-when-compile
     32   (require 'use-package))
     33 
     34 
     35 ;;; Format Spec
     36 
     37 (defun ox-jamzattack:last-updated (file)
     38   "Return the time that changes were commited in FILE."
     39   (let* ((default-directory (file-name-directory file))
     40 	 (filename (file-relative-name file default-directory)))
     41     (nth 0
     42 	 (ignore-errors
     43 	   (process-lines (executable-find "git")
     44 			  "log" "-1" "--date=short" "--pretty=format:%cd"
     45 			  filename)))))
     46 
     47 (defun ox-jamzattack:format-spec (info)
     48   "Return format specification for preamble and postamble.
     49 INFO is a plist used as a communication channel.
     50 
     51 Differences from `org-html-format-spec':
     52 * %A - Author's name as a mailto link
     53 * %M - Last modified time according to git"
     54   (let ((timestamp-format (plist-get info :html-metadata-timestamp-format)))
     55     `((?t . ,(org-export-data (plist-get info :title) info))
     56       (?s . ,(org-export-data (plist-get info :subtitle) info))
     57       (?d . ,(org-export-data (org-export-get-date info timestamp-format)
     58 			      info))
     59       (?T . ,(format-time-string timestamp-format))
     60       (?a . ,(org-export-data (plist-get info :author) info))
     61       (?e . ,(mapconcat
     62 	      (lambda (e) (format "<a href=\"mailto:%s\">%s</a>" e e))
     63 	      (split-string (plist-get info :email)  ",+ *")
     64 	      ", "))
     65 
     66       (?A . ,(format "<a href=\"mailto:%s\">%s</a>"
     67 		     (plist-get info :email)
     68 		     (org-export-data (plist-get info :author) info)))
     69       (?M . ,(let ((file (plist-get info :input-file)))
     70 	       (or (ox-jamzattack:last-updated file)
     71 		   (format-time-string timestamp-format
     72 				       (and file (file-attribute-modification-time
     73 						  (file-attributes file)))))))
     74 
     75       (?c . ,(plist-get info :creator))
     76       (?C . ,(let ((file (plist-get info :input-file)))
     77 	       (format-time-string timestamp-format
     78 				   (and file (file-attribute-modification-time
     79 					      (file-attributes file))))))
     80       (?v . ,(or (plist-get info :html-validation-link) "")))))
     81 
     82 (advice-add 'org-html-format-spec :override 'ox-jamzattack:format-spec)
     83 
     84 
     85 ;;; Sitemap
     86 
     87 (defun ox-jamzattack:sitemap (project &optional sitemap-filename)
     88   "If the file \".ring.org\" exists, include it in the sitemap.
     89 
     90 This is advice for `org-publish-sitemap', and it should not be
     91 called on its own.  PROJECT and SITEMAP-FILENAME are both used to
     92 determine where the sitemap is located."
     93   (let* ((root (expand-file-name
     94 		(file-name-as-directory
     95 		 (org-publish-property :base-directory project))))
     96 	 (sitemap-filename (concat root (or sitemap-filename "sitemap.org"))))
     97     (with-current-buffer (find-file-noselect sitemap-filename)
     98       (when (file-exists-p ".ring.org")
     99 	(goto-char (point-max))
    100 	(insert "\n\n#+include: .ring.org")
    101 	(save-buffer)))))
    102 
    103 (advice-add 'org-publish-sitemap :after 'ox-jamzattack:sitemap)
    104 
    105 
    106 ;;; RSS
    107 
    108 (use-package webfeeder
    109   :straight t
    110   :defer t)
    111 
    112 (defun ox-jamzattack:make-rss (project &rest _ignored)
    113   "Create an RSS feed for PROJECT."
    114   (let* ((dir (expand-file-name
    115 	       (file-name-as-directory
    116 		(org-publish-property :publishing-directory project))))
    117 	 (files (cl-remove "\\`\\(index\\|sitemap\\)\\.html\\'"
    118 			   (mapcar
    119 			    (lambda (file)
    120 			      (replace-regexp-in-string
    121 			       dir "" file))
    122 			    (directory-files-recursively
    123 			     dir
    124 			     "\\.html\\'"))
    125 			   :test #'string-match-p))
    126 	 (project-name (car project))
    127 	 (url (format "https://%s.jamzattack.xyz" project-name))
    128 	 (title (pcase project-name
    129 		  ("music" "Music")
    130 		  ("blog" "The Yeet Log"))))
    131     (when (cl-member project-name
    132 		     '("blog"
    133 		       "music")
    134 		     :test #'string-equal)
    135       (webfeeder-build
    136        "rss.xml"
    137        dir
    138        url
    139        files
    140        :title title
    141        :builder 'webfeeder-make-rss))))
    142 
    143 (advice-add 'org-publish :after #'ox-jamzattack:make-rss)
    144 
    145 (provide 'ox-jamzattack)
    146 ;;; ox-jamzattack.el ends here