summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThanos Apollo <[email protected]>2024-06-28 04:22:13 +0300
committerThanos Apollo <[email protected]>2024-06-28 04:22:25 +0300
commit37d8110853e7247aa1c8ed41af00edcf298096cb (patch)
tree59c681eaddf1f42090b2f9fbe39082568bd3a7b5
parent0749549451eddf3ccc9a758d9a55a9c67a5d5ea5 (diff)
gnosis-display-image: Refactor to center image
-rw-r--r--gnosis.el21
1 files changed, 21 insertions, 0 deletions
diff --git a/gnosis.el b/gnosis.el
index e6ab7fa..6f362bb 100644
--- a/gnosis.el
+++ b/gnosis.el
@@ -366,6 +366,27 @@ This will not be applied to sentences that start with double space."
(delete-region end (match-end 0))
(delete-region (match-beginning 0) start))))))))
+(cl-defun gnosis-display-image (id &optional (image 'images))
+ "Display image for note ID.
+
+IMAGE is the image type to display, usually should be either =images'
+or =extra-image'. Instead of using =extra-image' post review, prefer
+=gnosis-display-extra' which displays the =extra-image' as well.
+
+Refer to =gnosis-db-schema-extras' for informations on images stored."
+ (let* ((img (gnosis-get image 'extras `(= id ,id)))
+ (path-to-image (expand-file-name (or img "") (file-name-as-directory gnosis-images-dir)))
+ (image (create-image path-to-image 'png nil :width gnosis-image-width :height gnosis-image-height))
+ (image-width (car (image-size image t)))
+ (frame-width (window-text-width))) ;; Width of the current window in columns
+ (cond ((or (not img) (string-empty-p img))
+ (insert "\n\n"))
+ ((and img (file-exists-p path-to-image))
+ (let* ((padding-cols (/ (- frame-width (floor (/ image-width (frame-char-width)))) 2))
+ (padding (make-string (max 0 padding-cols) ?\s)))
+ (insert "\n\n" padding) ;; Insert padding before the image
+ (insert-image image)
+ (insert "\n\n"))))))
(defun gnosis-display-mcq-options (id)
"Display answer options for mcq note ID."