;;; 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: ;;; ;;; Reader for Org syntax which invokes `org-export' via the Emacs daemon for ;;; rendering and metadata extraction. ;;; ;;; Code: (define-module (jakob reader org-mode) #:use-module (ice-9 match) #:use-module (ice-9 popen) #:use-module (ice-9 regex) #:use-module (ice-9 textual-ports) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) #:use-module (haunt reader) #:use-module (ice-9 match) #:use-module (sxml simple) #:export (org-mode-reader)) (define (rewrite-image-urls subtree) (match subtree (('img ('@ ('src src) attrs ...)) (let* ((src (if (string-prefix? "./" src) (substring src 2) src)) (src (string-append "/static/image/" src))) `(img (@ (src ,src) ,@attrs)))) ((elems ...) (map rewrite-image-urls elems)) (elem elem))) (define (eval-in-emacs form) "Evaluate FORM in the current Emacs (daemon) session and return the result" (let* ((stringified (call-with-output-string (cut write form <>))) (port (open-pipe* OPEN_READ "emacsclient" "-e" stringified)) (result (read port)) (result (if (eqv? result 'nil) '() result))) (if (eqv? 0 (status:exit-val (close-pipe port))) result (error "could not eval" form)))) (define (render-org-mode-file file-name) (define output-port (mkstemp! (string-copy "/tmp/emacs-eval-XXXXXX"))) (define result (eval-in-emacs `(save-excursion (let ((enable-local-variables :all)) (set-buffer (find-file-noselect ,file-name))) (setq-local org-export-filter-latex-fragment-functions (list (lambda (data backend channel) (org-html-encode-plain-text data)))) (let ((result (org-export-as 'html nil nil t))) (with-temp-buffer (insert result) (write-region (point-min) (point-max) ,(port-filename output-port))))))) (define parsed (call-with-input-file (port-filename output-port) get-string-all)) (format #f "~a" parsed)) (define %default-additional-keys '("CROSSPOST" "SCRIPTS" "META-TAGS")) (define* (extract-org-mode-metadata file-name #:optional (additional-keys %default-additional-keys)) `(,@(map (match-lambda (("DATE" date) `(date . ,(string->date date "<~Y-~m-~d ~a ~H:~M>")))) (eval-in-emacs `(save-excursion (let ((enable-local-variables :all)) (set-buffer (find-file-noselect ,file-name))) (org-collect-keywords '("DATE"))))) ,@(map (match-lambda (("TAGS" tags) `(tags . ,(string-split tags #\space)))) (eval-in-emacs `(save-excursion (let ((enable-local-variables :all)) (set-buffer (find-file-noselect ,file-name))) (org-collect-keywords '("TAGS"))))) ,@(map (match-lambda ((key value) `(,(string->symbol (string-downcase key)) . ,value))) (eval-in-emacs `(save-excursion (let ((enable-local-variables :all)) (set-buffer (find-file-noselect ,file-name))) (org-collect-keywords ',(append '("TITLE") additional-keys))))))) (define (read-org-mode-post file-name) (values (extract-org-mode-metadata file-name) (match (call-with-input-string (render-org-mode-file file-name) xml->sxml) (('*TOP* ('html sxml ...)) (rewrite-image-urls sxml))))) (define org-mode-reader (make-reader (make-file-extension-matcher "org") read-org-mode-post))