diff --git a/.gitignore b/.gitignore index e5e2b24..28ed9d7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /posts/index.org /build.mk /Eldev +/publish.el # Added automatically by `eldev init'. /.eldev diff --git a/GNUmakefile b/GNUmakefile index 091af28..ae8a015 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,11 +1,7 @@ -define tangle = - eldev emacs -quick -batch \ +build.mk: literate-build.org + emacs -quick -batch \ -eval "(package-initialize)" \ -load ob-tangle \ - -eval "(org-babel-tangle-file \"$<\" \"$(PWD)/$@\"$(if $1, \"$1\"))" -endef - -build.mk: literate-build.org - $(call tangle) + -eval "(org-babel-tangle-file \"$<\"))" include build.mk diff --git a/literate-build.org b/literate-build.org index 2570531..e7b1ce3 100644 --- a/literate-build.org +++ b/literate-build.org @@ -1,25 +1,24 @@ #+title: ryuslash's website's build's files #+subtitle: Literate configuration of how to build this site #+options: num:nil prop:t +#+macro: kbd @@html:@@$1@@html:@@ I'm a big fan of [[file:literate-programming.org][Literate Programming]], so I figured I'd make the builds for my website a literate configuration file as well. First I need to build the build files. This is the smallest make file that I can think to make to enable me to build the rest out. This make file is duplicated in both this file and the source code repository since I don't know of a way not to. -First I define a function =tangle= that can be used to tangle an org file to the code file that it needs to be. Then I specify that any =.mk= file should depend on /this/ file ({{{input-file}}}) and that it is generated by running =org-babel-tangle-file= on it. The =$<= is the first dependency and =$@= is the current target file (whatever =.mk= file we're generating). +I specify that =build.mk= file should depend on /this/ file ({{{input-file}}}) and that it is generated by running =org-babel-tangle-file= on it. The =$<= is the first dependency and =$@= is the current target file (whatever =.mk= file we're generating). #+begin_src makefile-gmake :tangle GNUmakefile -define tangle = - eldev emacs -quick -batch \ +build.mk: literate-build.org + emacs -quick -batch \ -eval "(package-initialize)" \ -load ob-tangle \ - -eval "(org-babel-tangle-file \"$<\" \"$(PWD)/$@\"$(if $1, \"$1\"))" -endef - -build.mk: literate-build.org - $(call tangle) + -eval "(org-babel-tangle-file \"$<\"))" #+end_src +I can't use Eldev in this particular recipe because it needs a file with proper package headers and all, and that doesn't get generated until we run this. I'm cheating a little bit because I'm expecting this command to also generate the other files the build needs without explicitly saying so. + After that it's just a matter of including the file I want. #+begin_src makefile-gmake :tangle GNUmakefile @@ -91,7 +90,7 @@ clean: ## Remove all of the build files @rm -rvf *.elc @rm -rvf public @rm -rvf .org-timestamps - @rm -rvf posts/index.org build.mk Eldev + @rm -rvf posts/index.org build.mk Eldev publish.el #+end_src The =serve= target is a convenience target for when I'm writing or making modifications to the build and publish processes. It just starts a simple =php= web server in the =public/= directory so that I can easily load it in my browser. @@ -126,13 +125,6 @@ theme: Eldev ## Generate the theme CSS -eval '(write-file "src/less/yoshi.css")' #+end_src -The =Eldev= target just tangles the {{{input-file}}} into the =Eldev= file. This uses the =tangle= function defined in the intro section. - -#+begin_src makefile-gmake -Eldev: literate-build.org - $(call tangle) -#+end_src - Finally, as a precaution, I specify that all of the main targets are phony targets. This way if I ever introduce any file with the same name as these targets they will still build and not assume that because the file exists, everything is up-to-date. #+begin_src makefile-gmake @@ -144,12 +136,12 @@ Finally, as a precaution, I specify that all of the main targets are phony targe :header-args:emacs-lisp: :tangle Eldev :END: -With Eldev I can install dependencies of my =publish.el= locally. Really the only reason I chose to use Eldev is because it is available in the [[file:guix.org][Guix]] repository. +With Eldev I can install dependencies of my [[Publishing project]] locally. Really the only reason I chose to use Eldev is because it is available in the [[file:guix.org][Guix]] repository. Since this is an actually full-fledged Emacs Lisp file and I don't know what I'm going to be doing in the future, I should be sure to enable lexical binding. Otherwise Emacs defaults to using dynamic binding, and that might cause some surprises in the future, since I'm quite used to using lexical binding everywhere. #+begin_src emacs-lisp -; -*- lexical-binding: t -*- +; -*- lexical-binding: t; -*- #+end_src All I'm doing here, really, is enable the various Emacs Lisp Package Archives. @@ -160,8 +152,321 @@ All I'm doing here, really, is enable the various Emacs Lisp Package Archives. (eldev-use-package-archive 'melpa) #+end_src -* COMMENT Local Variables +* Publishing project +:PROPERTIES: +:header-args:emacs-lisp: :tangle publish.el +:END: +Before anything else, here too I need to add a file header and enable lexical binding. If I don't add the file header Eldev won't work right. + +#+begin_src emacs-lisp +;;; publish.el --- Publish project for ryuslash.org -*- lexical-binding: t; -*- +#+end_src + +And then I have to keep a list of required packages so that Eldev can figure out which to download. I also need the Version header to keep Eldev happy. + +#+begin_src emacs-lisp :noweb yes +;; Version: 1 +;; Package-Requires: (<>) +#+end_src + +I install =dockerfile-mode= because some of my posts include examples of docker files. And I use the latest =org= mode package. + +#+begin_src emacs-lisp :exports none :tangle no :noweb-ref required-packages :noweb-sep " " +dockerfile-mode org htmlize +#+end_src + +Some packages must be loaded to make sure their features can be used by the export process. + +#+begin_src emacs-lisp +(require 'dockerfile-mode) +(require 'ob-dot) +(require 'ox-publish) +(require 'ox-rss) +(require 'rainbow-delimiters) +(require 'subr-x) +#+end_src + +After that I define a constant for the current root so that it's easy to refer to. I use =defconst= here instead of =defvar= for 2 reasons: + +1. It's a constant, and shouldn't be changed during execution. +2. Re-evaluating a =defvar= doesn't do anything[fn:1], but re-evaluating a =defconst= updates the value. + +#+begin_src emacs-lisp +(defconst publish-root + (file-name-directory + (or load-file-name + (buffer-file-name))) + "The directory where ‘oni-org’ was loaded from.") +#+end_src + +Keep the timestamp cache (used to determine which pages need to be cleaned up) local. This prevents publishing from relying on global state on my PC and makes it easy to reset everything. + +#+begin_src emacs-lisp +(setq org-publish-timestamp-directory + (expand-file-name ".org-timestamps/" publish-root)) +#+end_src + +I add the =rainbow-delimiters-mode= to the =prog-mode-hook= so that delimiters are highlighted like they are in my usual Emacs sessions. + +#+begin_src emacs-lisp +(add-hook 'prog-mode-hook 'rainbow-delimiters-mode) +#+end_src + +This requires that I have the =rainbow-delimiters= package installed. + +#+begin_src emacs-lisp :exports none :tangle no :noweb-ref required-packages :noweb-sep " " +rainbow-delimiters +#+end_src + +Anything that is marked with either the =noexport= or =draft= tag shouldn't be exported. + +#+begin_src emacs-lisp +(setq org-export-exclude-tags '("noexport" "draft")) +#+end_src + +Don't ask for my confirmation whether babel blocks should be evaluated. If I've added them I want them to run. + +#+begin_src emacs-lisp +(setq org-confirm-babel-evaluate nil) +#+end_src + +Reset the org-mode style, I don't want the default style interfering with my own. + +#+begin_src emacs-lisp +(setq org-html-head-include-default-style nil) +#+end_src + +Just add CSS class names to code blocks, I'll make sure that the CSS selectors exist. + +#+begin_src emacs-lisp +(setq org-html-htmlize-output-type 'css) +#+end_src + +Use HTML5 and all its fancy elements. + +#+begin_src emacs-lisp +(setq org-html-html5-fancy t) +(setq org-html-doctype "html5") +#+end_src + +** Projects + +Here come the actual projects. + +#+begin_src emacs-lisp :noweb no-export :noweb-prefix no +(setq org-publish-project-alist `(<>)) +#+end_src + +*** Pages + +The pages project is the main publishing project. It exports all of the =.org= files except for =README.org= and anything found in the =posts/= directory. The =README.org= is only relevant when you're looking at the source code for this website and =posts/= has its own project. + +It also loads the stylesheet into the HTML head, and add a link to my Mastodon account in the postamble, for verification purposes. + +#+begin_src emacs-lisp :tangle no :noweb-ref projects +("pages" + :base-directory "." + :base-extension "org" + :publishing-directory "public/" + :recursive t + :exclude ,(rx string-start + (or "posts/" + (and "README.org" string-end))) + :publishing-function org-html-publish-to-html + :html-head "" + :html-postamble t + :html-postamble-format (("en" "

Find me on Mastodon

+

Date: %C

+

%c

"))) +#+end_src + +*** Posts + +The posts project is an experiment right now to see if I can find a way I'm happy with to actually publish my blog through plain old org files. This goes together with the [[RSS]] project. This also calculates a rough estimate of how long the reading time for the page will be and adds it to the preamble of each post. + +First I need a few of functions. Calculate how many minutes it would take to read the given buffer. I don't remember where I got this particular algorithm from, and I haven't tried out how accurate it is, but my experience with other websites doing this is that it's generally not very accurate anyway. + +#+begin_src emacs-lisp +(defun publish-calculate-reading-time (buffer) + "Calculate the amount of minutes it would take to read the contents of BUFFER." + (with-current-buffer buffer + (max 1 (/ (count-words (point-min) (point-max)) 228)))) +#+end_src + +I decided to split the calculations and formatting functions, so here is just a simple function that format the given reading time. + +#+begin_src emacs-lisp +(defun publish-format-reading-time (time) + "Return a string describing TIME." + (format "Reading time: %d minute%s" + time + (if (= time 1) "" "s"))) +#+end_src + +And then I need a function to actually generate the preamble. This function ignores the =index.org= file, because that is not a page to read but just and index of my blog posts. + +#+begin_src emacs-lisp +(defun publish-generate-post-preamble (_project) + "Generate the preamble for a post with the estimated reading time." + (unless (string= (file-name-nondirectory (buffer-file-name)) "index.org") + (publish-format-reading-time + (publish-calculate-reading-time (current-buffer))))) +#+end_src + +Finally the actual project. It publishes files inside the =posts/= directory. + +#+begin_src emacs-lisp :tangle no :noweb-ref projects +("posts" + :base-directory "posts/" + :base-extension "org" + :publishing-directory "public/posts/" + :recursive t + :publishing-function org-html-publish-to-html + :html-head "" ; (ref:html-head) + :html-preamble publish-generate-post-preamble) ; (ref:html-preamble) +#+end_src + +At [[(html-head)]] I again load the CSS style sheet for this site. [[(html-preamble)]] calculates the page's reading time and puts it on the page. + +*** RSS + +The =rss= project works together with the =posts= project. This project very specifically generates and targets a specific file and exports it as an RSS feed. For this I require the =ox-rss= package. + +#+begin_src emacs-lisp :exports none :tangle no :noweb-ref required-packages :noweb-sep " " +ox-rss +#+end_src + +Before I can define my project I still need to have some helper functions. The first I'll make is the =publish-empty-time=. It's just a dumb little function that takes the current time and subtracts it with itself. This is used as an empty value, so that if compared to another time it'll always be less than the other time. + +#+begin_src emacs-lisp +(defun publish-empty-time () + "Get an empty time value." + (let ((current-time (current-time))) + (time-subtract current-time current-time))) +#+end_src + +Next up is =publish-get-latest-modified-time= which takes a list of files and finds the latest time at which any of the files were modified. + +#+begin_src emacs-lisp +(defun publish-get-latest-modified-time (files) + "Get the latest modification time of any file from the list FILES." + (car + (last + (sort (mapcar #'org-publish-cache-mtime-of-src files) #'time-less-p)))) +#+end_src + +I don't know why exactly, but there is a =time-less-p= and a =time-equal-p=, but no =time-greater-p= or any other. Just for clarity I decided to implement ~publish-time>=~ which is just the complement of =time-less-p= (because if the time is not less, it has to be either greater than or equal to). + +#+begin_src emacs-lisp +(defun publish-time>= (a b) + "Check if time A is greater than or equal to time B." + (not (time-less-p a b))) +#+end_src + +Now a bigger function with a bit more meat to it. This function opens an org file and tries to get some information out of it to construct a headline from it with a link to the actual post. This is used to generate the index of all of the posts. It goes through the file trying to find any headings tagged with the tag =summary= and extracts the contents from the first one. This gives us something to put on the index so it's not just a plain list with links. + +#+begin_src emacs-lisp +(defun publish-extract-summary-from-file (file props) + "Extract a summary from FILE. +PROPS is used as an aid in getting the right information from the file." + (format "* %s\n:PROPERTIES:\n:CUSTOM_ID: %s\n:PUBDATE: %s\n:RSS_PERMALINK: %s\n:END:\n\n%s\n\n[[file:%s][Read More]]\n\n" + (car (org-publish-find-property file :title props)) + (file-name-nondirectory file) + (format-time-string "[%Y-%m-%d %a %H:%M]" (org-timestamp-to-time (car (org-publish-find-property file :date props)))) + (file-name-nondirectory file) + (car (org-map-entries (lambda () (let ((element-data (cadr (org-element-at-point)))) + (buffer-substring-no-properties + (map-elt element-data :contents-begin) + (map-elt element-data :contents-end)))) + "summary" + (list file))) + (file-name-nondirectory file))) +#+end_src + +Finally the last function mushes it all together and actually generates an =index.org= file. I just looks through the =posts/= directory and finds any files that start with a date. It compares the last modification time of the =index.org= and that of the latest modified post, and skips generating if the =index.org= is newer. Then it takes the 30 latest posts and extracts the necessary information from them and writes it all into =index.org=. + +#+begin_src emacs-lisp +(defun publish-generate-index (props) + "Generate an index from my posts. +Argument PROPS +." + (let* ((index-file (expand-file-name "posts/index.org")) + (index-generated-time (or (and (file-exists-p index-file) + (org-publish-cache-mtime-of-src index-file)) + (publish-empty-time))) + (files (directory-files "posts/" t (rx bos (= 8 digit) "-" (= 4 digit) "-" (one-or-more nonl) (not "~") eos))) + (latest-modification-time (publish-get-latest-modified-time files))) + (if (publish-time>= index-generated-time latest-modification-time) + (message "Not generating index...") + (progn + (message "Generating index...") + (with-temp-buffer + (insert "#+title: ryuslash's blog\n") + (insert "#+options: num:nil\n") + (insert "#+html_link_up: /") + (insert "#+html_link_home: /") + (insert "\n") + (apply 'insert + (mapcar (lambda (file) (publish-extract-summary-from-file file props)) + (take 30 (reverse files)))) + (write-file "posts/index.org")))))) +#+end_src + +With that all out of the way I can finally assemble my project. + +#+begin_src emacs-lisp :tangle no :noweb-ref projects +("rss" + :base-directory "posts/" + :base-extension "org" + :rss-extension "xml" + :preparation-function publish-generate-index ; (ref:preparation-function) + :publishing-directory "public/posts/" + :publishing-function (org-rss-publish-to-rss) ; (ref:publishing-function) + :html-link-home "https://ryuslash.org/posts/" + :html-link-use-abs-url t + :section-numbers nil + :exclude ".*" ; (ref:exclude) + :include ("index.org") + :table-of-contents nil) +#+end_src + +The [[(preparation-function)]] uses my =publish-generate-index= function to create the =index.org= before trying to turn it into an RSS feed. The [[(publishing-function)]] exports the org file to an RSS feed, this is from the =ox-rss= package. And [[(exclude)]] excludes everything, and then we include the =index.org= again so that it only tries to turn the =index.org= into an RSS feed, not anything else. + +*** Assets + +This is a simple project that just goes around and copies over any asset files (=.svg=, =.jpg=, etc.) from the source directory into the publish directory. + +#+begin_src emacs-lisp :tangle no :noweb-ref projects +("assets" + :base-directory "." + :recursive t + :exclude "^public/" + :base-extension "svg\\|png\\|jpg\\|ico" + :publishing-function org-publish-attachment + :publishing-directory "public/") +#+end_src + +*** All + +This is a convenience project so that I can publish everything all at once. It also establishes the right order in which to do so. Really the only order is that [[RSS]] has to go before [[Posts]] because the RSS preparation function needs to happen before the =index.org= it generates can be included in the Posts export. + +#+begin_src emacs-lisp :tangle no :noweb-ref projects +("all" :components ("pages" "rss" "posts" "assets")) +#+end_src + +** File footer + +Finally I need to add a file footer, again to keep Eldev happy. + +#+begin_src emacs-lisp +(provide 'publish) +;;; publish.el ends here +#+end_src + +* Footnotes + +[fn:1] You have to evaluate it in a special way. Either with {{{kbd(C-M-x)}}} or {{{kbd(C-u C-x C-e)}}}. # Local Variables: # org-src-preserve-indentation: t # End: diff --git a/publish.el b/publish.el deleted file mode 100644 index 659a5d2..0000000 --- a/publish.el +++ /dev/null @@ -1,180 +0,0 @@ -;;; publish.el --- Publishing configuration for ryuslash.org -*- lexical-binding: t; -*- - -;; Copyright (C) 2020 Tom Willemse - -;; Author: Tom Willemse -;; Keywords: -;; Version: 1 -;; Package-Requires: (dockerfile-mode htmlize org org-contrib rainbow-delimiters ox-rss) - -;; This program is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation, either version 3 of the License, or -;; (at your option) any later version. - -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . - -;;; Commentary: - -;; - -;;; Code: - -(require 'dockerfile-mode) -(require 'ob-dot) -(require 'ox-publish) -(require 'ox-rss) -(require 'rainbow-delimiters) -(require 'subr-x) - -(defconst publish-root - (file-name-directory - (or load-file-name - (buffer-file-name))) - "The directory where ‘oni-org’ was loaded from.") - -(add-hook 'prog-mode-hook 'rainbow-delimiters-mode) - -(defun publish-calculate-reading-time (buffer) - "Calculate the amount of minutes it would take to read the contents of BUFFER." - (with-current-buffer buffer - (max 1 (/ (count-words (point-min) (point-max)) 228)))) - -(defun publish-format-reading-time (time) - "Return a string describing TIME." - (format "%d minute%s" - time - (if (= time 1) "" "s"))) - -(defun publish-extract-summary-from-file (file props) - "Extract a summary from FILE. -PROPS is used as an aid in getting the right information from the file." - (format "* %s\n:PROPERTIES:\n:CUSTOM_ID: %s\n:PUBDATE: %s\n:RSS_PERMALINK: %s\n:END:\n\n%s\n\n[[file:%s][Read More]]\n\n" - (car (org-publish-find-property file :title props)) - (file-name-nondirectory file) - (format-time-string "[%Y-%m-%d %a %H:%M]" (org-timestamp-to-time (car (org-publish-find-property file :date props)))) - (file-name-nondirectory file) - (car (org-map-entries (lambda () (let ((element-data (cadr (org-element-at-point)))) - (buffer-substring-no-properties - (map-elt element-data :contents-begin) - (map-elt element-data :contents-end)))) - "summary" - (list file))) - (file-name-nondirectory file))) - -(defun publish-get-latest-modified-time (files) - "Get the latest modification time of any file from the list FILES." - (car - (last - (sort (mapcar #'org-publish-cache-mtime-of-src files) #'time-less-p)))) - -(defun publish-time>= (a b) - "Check if time A is greater than or equal to time B." - (not (time-less-p a b))) - -(defun publish-empty-time () - "Get an empty time value." - (let ((current-time (current-time))) - (time-subtract current-time current-time))) - -(defun publish-generate-index (props) - "Generate an index from my posts. -Argument PROPS -." - (let* ((index-file (expand-file-name "posts/index.org")) - (index-generated-time (or (and (file-exists-p index-file) - (org-publish-cache-mtime-of-src index-file)) - (publish-empty-time))) - (files (directory-files "posts/" t (rx bos (= 8 digit) "-" (= 4 digit) "-" (one-or-more nonl) (not "~") eos))) - (latest-modification-time (publish-get-latest-modified-time files))) - (if (publish-time>= index-generated-time latest-modification-time) - (message "Not generating index...") - (progn - (message "Generating index...") - (with-temp-buffer - (insert "#+title: ryuslash's blog\n") - (insert "#+options: num:nil\n") - (insert "\n") - (apply 'insert - (mapcar (lambda (file) (publish-extract-summary-from-file file props)) - (take 30 (reverse files)))) - (write-file "posts/index.org")))))) - -(setq org-export-exclude-tags '("noexport" "draft")) - -(setq org-confirm-babel-evaluate nil) - -(setq org-html-head-include-default-style nil) - -(setq org-html-htmlize-output-type 'css) - -(setq org-publish-timestamp-directory - (concat default-directory "/.org-timestamps/")) - -(setq org-html-html5-fancy t) - -(setq org-html-doctype "html5") - -(setq org-publish-project-alist - `(("index" - :base-directory "." - :base-extension "org" - :publishing-directory "public/" - :recursive t - :exclude ,(rx string-start - (or "posts/" - (and "README.org" string-end))) - :publishing-function org-html-publish-to-html - :html-head "" - :html-postamble t - :html-postamble-format (("en" "

Find me on Mastodon

-

Date: %C

-

%c

"))) - ("posts" - :base-directory "posts/" - :base-extension "org" - :publishing-directory "public/posts/" - :recursive t - :publishing-function org-html-publish-to-html - :html-head "" - :html-preamble (lambda (project) - (let ((buffer (find-file-noselect (buffer-file-name)))) - (unless (member (car (org-publish-find-property (buffer-file-name) :title project)) - '("ryuslash.org" "ryuslash's blog")) - (publish-format-reading-time - (publish-calculate-reading-time buffer)))))) - ("rss" - :base-directory "posts/" - :base-extension "org" - :rss-extension "xml" - :preparation-function publish-generate-index - :publishing-directory "public/posts/" - :publishing-function (org-rss-publish-to-rss) - :html-link-home "https://ryuslash.org/posts/" - :html-link-use-abs-url t - :section-numbers nil - :exclude ".*" - :include ("index.org") - :table-of-contents nil) - ("assets" - :base-directory "." - :recursive t - :exclude "^public/" - :base-extension "svg\\|png\\|jpg\\|ico" - :publishing-function org-publish-attachment - :publishing-directory "public/") - ("all" :components ("index" "rss" "posts" "assets")))) - -(defvar publish-feed-url-format - ;"https://gitlab.com/ryuslash/ryuslash.org/-/commits/master/%s?feed_token=Rf8otgpS8YEiYakJN4NR&format=atom" - "https://code.ryuslash.org/new-ryuslash.org/atom/%s?h=master" - "Format string for the URL to the page’s atom feed.") - -(provide 'publish) -;;; publish.el ends here