From 7f37518e4792f040a753a5d8d68d51e76cc0b2be Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Sat, 13 Jul 2024 17:55:47 -0400 Subject: Initial migration to org-reader Long-term I would like to merge the =posts-assets= and =posts= directories but currently Haunt will crash if something exists in the =posts= directory that can't be processed by a reader --- haunt/jakob/reader/org-mode-prime.scm | 36 +++++++++++++++++++++++++++++++++++ haunt/jakob/reader/org-mode.scm | 12 +++++++++++- haunt/jakob/utils.scm | 22 +++++++++++---------- 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 haunt/jakob/reader/org-mode-prime.scm (limited to 'haunt/jakob') diff --git a/haunt/jakob/reader/org-mode-prime.scm b/haunt/jakob/reader/org-mode-prime.scm new file mode 100644 index 0000000..118f1a6 --- /dev/null +++ b/haunt/jakob/reader/org-mode-prime.scm @@ -0,0 +1,36 @@ +;;; Copyright © 2019 - 2024 Jakob L. Kreuze +;;; +;;; 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: +;;; +;;; Wrapper around the `org-mode-reader' to apply rewrite rules specific to my +;;; website. +;;; +;;; Code: + +(define-module (jakob reader org-mode-prime) + #:use-module (haunt reader) + #:use-module (jakob reader org-mode) + #:use-module (jakob utils sxml) + #:use-module (srfi srfi-11) + #:export (org-mode-reader-prime)) + +(define org-mode-reader-prime + (make-reader (reader-matcher org-mode-reader) + (lambda args + (let-values (((metadata sxml) (apply (reader-proc org-mode-reader) args))) + (values metadata + (rewrite-absolute-urls-as-relative sxml)))))) diff --git a/haunt/jakob/reader/org-mode.scm b/haunt/jakob/reader/org-mode.scm index 7f064f1..b55dbf3 100644 --- a/haunt/jakob/reader/org-mode.scm +++ b/haunt/jakob/reader/org-mode.scm @@ -49,6 +49,10 @@ (or (getenv "HAUNT_ORG_READER_CACHE_DIR") "./.org-mode-reader-cache/")))) +;; File name of Elisp script to execute before anything else +(define %additional-emacs-preamble + (make-parameter (getenv "HAUNT_ORG_READER_EMACS_PREAMBLE"))) + ;; Additional Org-mode keywords to include in the extracted metadata. (define %additional-keys (make-parameter '("CROSSPOST" "SCRIPTS" "META-TAGS"))) @@ -62,7 +66,13 @@ If `%use-emacsclient' is truthy, evaluate FORM in the current running Emacs daemon. Assumes that FORM does not write to `standard-output'." - (let* (;; We need to explicitly request that Emacs write the result if using + (let* (;; If `HAUNT_ORG_READER_PREAMBLE' is specified, load that. + (form (if (%additional-emacs-preamble) + `(progn + (load-file ,(%additional-emacs-preamble)) + ,form) + form)) + ;; We need to explicitly request that Emacs write the result if using ;; Emacs batch mode (which is how we evaluate forms without ;; `emacsclient'.) (form (if (not (%use-emacsclient)) diff --git a/haunt/jakob/utils.scm b/haunt/jakob/utils.scm index a104510..ad35f4c 100644 --- a/haunt/jakob/utils.scm +++ b/haunt/jakob/utils.scm @@ -19,7 +19,8 @@ #:use-module (ice-9 match) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) - #:export (maybe-cons* + #:export (assq-map! + maybe-cons* maybe-list date->string* intersperse @@ -28,6 +29,12 @@ elide-string)) +(define (assq-map! alist key fn) + "Destructively apply FN to KEY in ALIST, if it exists" + (match (assq-ref alist key) + (#f alist) + (val (assq-set! alist key (fn val))))) + (define (maybe-list . args) "Create a list of all ARGS that are neither #f nor unspecified." (remove (lambda (element) @@ -62,16 +69,11 @@ is of an odd length and every second element is DELIM." content))) (define (first-paragraph post) - (let loop ((sxml (post-sxml post)) - (result '())) + (let loop ((sxml (post-sxml post))) (match sxml - (() (remove-footnote-references (reverse result))) - ((or (('p ...) tail ...) (paragraph tail ...)) - (if (and (list? paragraph) (eqv? 'header (car paragraph))) - (loop tail result) ; Ignore front-matter. - (remove-footnote-references (reverse (cons paragraph result))))) - ((head . tail) - (loop tail (cons head result)))))) + (((and ('p content ...) paragraph) . tail) + (remove-footnote-references paragraph)) + ((head . tail) (loop tail))))) (define (description-from-post post) (define (first-elem sxml) -- cgit v1.3