aboutsummaryrefslogtreecommitdiff
path: root/haunt/jakob/reader
diff options
context:
space:
mode:
authorJakob L. Kreuze2024-07-06 18:57:41 -0400
committerJakob L. Kreuze2024-07-06 18:57:41 -0400
commit6f095e2429b67be5e8357cc73b93a714e760d160 (patch)
treeebbe87bfdb8a16efca4fef4dffea9a0794b23bbb /haunt/jakob/reader
parent3757dd21a133966d706c2a56701ed1069698b7bb (diff)
Initial org-reader re-implementation
Diffstat (limited to 'haunt/jakob/reader')
-rw-r--r--haunt/jakob/reader/org-mode.scm113
1 files changed, 113 insertions, 0 deletions
diff --git a/haunt/jakob/reader/org-mode.scm b/haunt/jakob/reader/org-mode.scm
new file mode 100644
index 0000000..2faeef3
--- /dev/null
+++ b/haunt/jakob/reader/org-mode.scm
@@ -0,0 +1,113 @@
+;;; Copyright © 2019 - 2024 Jakob L. Kreuze <zerodaysfordays@sdf.org>
+;;;
+;;; 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
+;;; <http://www.gnu.org/licenses/>.
+
+;;; 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 "<html>~a</html>" 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))

© 2015 - 2026 Jakob L. Kreuze