From 5a682c030a07fc869dc05c6497e09a5136964f52 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Sun, 6 Sep 2026 02:42:49 +0100 Subject: Move link types to separate file. Move link types to a separate file independent of the ennum-html backend. The new link export interface exposes the info communication channel. So, we no longer need to override the html link transcoder. --- ennum-html.el | 163 +++------------------------------------------------------- ennum-link.el | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ennum.el | 1 + 3 files changed, 157 insertions(+), 155 deletions(-) create mode 100644 ennum-link.el diff --git a/ennum-html.el b/ennum-html.el index 337fa8e..f9d2d32 100644 --- a/ennum-html.el +++ b/ennum-html.el @@ -10,8 +10,7 @@ (org-export-define-derived-backend 'ennum-html 'html :translate-alist - '((inner-template . ennum-html-inner-template) - (link . ennum-html-link)) + '((inner-template . ennum-html-inner-template)) :options-alist '((:summary "SUMMARY" nil nil parse) (:thumbnail "THUMBNAIL" nil nil t) @@ -52,11 +51,11 @@ (pcase-lambda (`(,lang . ,slug)) (replace-regexp-in-string "")) -(defun ennum-html-find-link (type path info) - "Find link of TYPE and PATH in post object stored in -:ennum-posts of property list INFO." - (seq-find (lambda (link) - (and (eq (ennum-link-type link) type) - (string= (ennum-link-path link) path))) - (ennum-post-links (plist-get info :ennum-post)))) - -(defun ennum-html-link (link desc info) - ;; We override the html link transcoder to handle image, post and - ;; video links differently. We cannot use the `:export' property of - ;; `org-link-parameters' since those functions cannot access the - ;; `info' communication channel. - (let ((path (org-element-property :path link))) - (pcase (org-element-property :type link) - ("image" - ;; Convert image links to file links, get them transcoded by - ;; `org-html-link' and then remove the file:// scheme from the - ;; URI. Finally insert the transcoded image link in a link to a - ;; larger image as specified by the :image-link-width setting. - (format "%s" - (expand-file-name* - (ennum-image-output-filename - path (ennum-setting :image-link-width)) - (ennum-setting :images-directory)) - (replace-regexp-in-string - (rx (group (or "src" "data")) "=\"file://") "\\1=\"" - (org-html-link - (org-element-put-property - (org-element-put-property - link :path (url-encode-url - (expand-file-name* - (ennum-image-output-filename - path (ennum-setting :image-width)) - (ennum-setting :images-directory)))) - :type "file") - desc info)))) - ("post" - (ennum-html-export-post - path - (or desc (ennum-post-link-target-title - (ennum-html-find-link 'post path info))) - (org-export-backend-name (plist-get info :back-end)))) - ("video" - (xmlgen - `(video :src ,(url-encode-url - (expand-file-name* path (ennum-setting :videos-directory))) - :poster ,(url-encode-url - (expand-file-name* - (ennum-video-link-poster - (ennum-html-find-link 'video path info)) - (ennum-setting :images-directory))) - :preload "none" - :controls ""))) - ;; Pass other link types to org-html-link - (_ (org-html-link link desc info))))) - -(defmacro ennum-html-follow (path) - `(ennum-with-current-directory (ennum-setting :working-directory) - (find-file ,path))) - -;; TODO: Pass title through org-export-data-with-backend or something -;; similar in order to export org syntax in title -(defun ennum-html-export-post (path desc backend) - (pcase backend - ((or 'ennum-html 'html) - (xmlgen `(a :href ,(url-encode-url - (expand-file-name* path (ennum-setting :posts-directory))) - ,desc))))) - -(defun ennum-html-follow-post (path) - (ennum-html-follow (expand-file-name (concat path ".org") - (ennum-setting :posts-directory)))) - -(org-link-set-parameters - "post" - :export 'ennum-html-export-post - :follow 'ennum-html-follow-post) - -(defun ennum-html-follow-image (path) - (ennum-html-follow (expand-file-name path (ennum-setting :images-directory)))) - -(org-link-set-parameters - "image" :follow 'ennum-html-follow-image) - -(defun ennum-html-export-thumbnail (path _desc backend) - (pcase backend - ((or 'ennum-html 'html) - (xmlgen - `(img :src ,(url-encode-url - (expand-file-name* - (ennum-image-output-filename - path (ennum-setting :image-thumbnail-width)) - (ennum-setting :images-directory)))))))) - -(org-link-set-parameters - "thumbnail" - :export 'ennum-html-export-thumbnail - :follow 'ennum-html-follow-image) - -(defun ennum-html-follow-video (path) - (ennum-html-follow (expand-file-name path - (ennum-setting :videos-directory)))) - -(org-link-set-parameters - "video" - :follow 'ennum-html-follow-video) - -(defun ennum-html-export-pdf (path desc backend) - (pcase backend - ((or 'ennum-html 'html) - (xmlgen - (let ((link-path (url-encode-url - (expand-file-name* path (ennum-setting :static-directory))))) - `(object :data ,link-path - :type "application/pdf" - :width ,(ennum-setting :pdf-width) - :height ,(ennum-setting :pdf-height) - "Download " (a :href ,link-path ,path))))))) - -(org-link-set-parameters - "pdf" :export 'ennum-html-export-pdf) - -(defun ennum-html-export-static (path desc backend) - (pcase backend - ((or 'ennum-html 'html) - (xmlgen - `(a :href ,(url-encode-url - (expand-file-name* path (ennum-setting :static-directory))) - ,desc))))) - -(org-link-set-parameters - "static" :export 'ennum-html-export-static) - -(org-link-set-parameters - "tangle" :export 'ennum-html-export-static) - -(defun ennum-html-export-tag (tag desc backend) - (pcase backend - ((or 'ennum-html 'html) - (xmlgen - `(a :href ,(url-encode-url - (expand-file-name* tag (ennum-setting :tag-directory))) - ,(or desc tag)))))) - -(org-link-set-parameters - "tag" :export 'ennum-html-export-tag) - (provide 'ennum-html) diff --git a/ennum-link.el b/ennum-link.el new file mode 100644 index 0000000..4c514b5 --- /dev/null +++ b/ennum-link.el @@ -0,0 +1,148 @@ +;; -*- lexical-binding: t -*- + +(defun ennum-link-find (type path info) + "Find link of TYPE and PATH in post object stored in :ennum-posts of +property list INFO." + (seq-find (lambda (link) + (and (eq (ennum-link-type link) type) + (string= (ennum-link-path link) path))) + (ennum-post-links (plist-get info :ennum-post)))) + +(defmacro ennum-link-follow (path) + `(ennum-with-current-directory (ennum-setting :working-directory) + (find-file ,path))) + +;; TODO: Pass title through org-export-data-with-backend or something +;; similar in order to export org syntax in title +;; See fist-2017 post +(defun ennum-link-export-post (path desc backend info) + (let ((post-url (url-encode-url + (expand-file-name* path (ennum-setting :posts-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(a :href ,post-url + ,desc))) + ((or 'ennum-gemini 'gemini) + (format "=> %s.gmi %s" post-url desc))))) + +(defun ennum-link-follow-post (path) + (ennum-link-follow (expand-file-name (concat path ".org") + (ennum-setting :posts-directory)))) + +(defun ennum-link-export-image (path desc backend info) + (let ((image-url (url-encode-url + (expand-file-name* + (ennum-image-output-filename + path (ennum-setting :image-width)) + (ennum-setting :images-directory)))) + (image-link-url (expand-file-name* + (ennum-image-output-filename + path (ennum-setting :image-link-width)) + (ennum-setting :images-directory)))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(a :href ,image-link-url + (img :src ,image-url)))) + ((or 'ennum-gemini 'gemini) + (format "=> %s" image-link-url))))) + +(defun ennum-link-follow-image (path) + (ennum-link-follow (expand-file-name path (ennum-setting :images-directory)))) + +(defun ennum-link-export-thumbnail (path _desc backend info) + (let ((image-url (url-encode-url + (expand-file-name* + (ennum-image-output-filename + path (ennum-setting :image-thumbnail-width)) + (ennum-setting :images-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(img :src ,image-url))) + ((or 'ennum-gemini 'gemini) + (format "=> %s" image-url))))) + +(defun ennum-link-export-video (path desc backend info) + (let ((video-url (url-encode-url + (expand-file-name* path (ennum-setting :videos-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen + `(video :src ,video-url + :poster ,(url-encode-url + (expand-file-name* + (ennum-video-link-poster + (ennum-link-find 'video path info)) + (ennum-setting :images-directory))) + :preload "none" + :controls ""))) + ((or 'ennum-gemini 'gemini) + (format "=> %s" video-url))))) + +(defun ennum-link-follow-video (path) + (ennum-link-follow (expand-file-name path + (ennum-setting :videos-directory)))) + +(defun ennum-link-export-pdf (path desc backend info) + (let ((link-path (url-encode-url + (expand-file-name* path (ennum-setting :static-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(object :data ,link-path + :type "application/pdf" + :width ,(ennum-setting :pdf-width) + :height ,(ennum-setting :pdf-height) + "Download " (a :href ,link-path ,path)))) + ((or 'ennum-gemini 'gemini) + (format "=> %s" link-path))))) + +(defun ennum-link-export-static (path desc backend info) + (let ((file-url (url-encode-url + (expand-file-name* path (ennum-setting :static-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(a :href ,file-url ,desc))) + ((or 'ennum-gemini 'gemini) + (format "=> %s %s" file-url desc))))) + +(defun ennum-link-export-tag (tag desc backend info) + (let ((tag-url (url-encode-url + (expand-file-name* tag (ennum-setting :tag-directory))))) + (pcase backend + ((or 'ennum-html 'html) + (xmlgen `(a :href ,tag-url ,(or desc tag)))) + ((or 'ennum-gemini 'gemini) + (format "=> %s.gmi %s" tag-url (or desc tag)))))) + +(org-link-set-parameters + "post" + :export 'ennum-link-export-post + :follow 'ennum-link-follow-post) + +(org-link-set-parameters + "image" + :export 'ennum-link-export-image + :follow 'ennum-link-follow-image) + +(org-link-set-parameters + "thumbnail" + :export 'ennum-link-export-thumbnail + :follow 'ennum-link-follow-image) + +(org-link-set-parameters + "video" + :export 'ennum-link-export-video + :follow 'ennum-link-follow-video) + +(org-link-set-parameters + "pdf" :export 'ennum-link-export-pdf) + +(org-link-set-parameters + "static" :export 'ennum-link-export-static) + +(org-link-set-parameters + "tangle" :export 'ennum-link-export-static) + +(org-link-set-parameters + "tag" :export 'ennum-link-export-tag) + +(provide 'ennum-link) diff --git a/ennum.el b/ennum.el index cfcc0ef..b58a205 100644 --- a/ennum.el +++ b/ennum.el @@ -2,6 +2,7 @@ (require 'ennum-html) (require 'ennum-image) +(require 'ennum-link) (require 'ox) (require 'seq) (require 'loadhist) -- cgit 1.4.1