From 7c87618b3a18fb7c4e49dae5bd955a697a30ec0e Mon Sep 17 00:00:00 2001 From: York Zhao Date: Sat, 1 Oct 2022 10:16:31 -0400 Subject: [PATCH 1/3] Add .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a89a5c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~* From ac67d96a7a147f8f434e523764bc006bc534f6a5 Mon Sep 17 00:00:00 2001 From: York Zhao Date: Thu, 29 Sep 2022 22:09:45 -0400 Subject: [PATCH 2/3] Break super long lines --- anki-editor.el | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/anki-editor.el b/anki-editor.el index 609ad94..ccb502c 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -550,14 +550,17 @@ Where the subtree is created depends on PREFIX." (mapcar #'org-entry-restore-space values))) (defun anki-editor--build-fields () - "Build a list of fields from subheadings of current heading, each element of which is a cons cell, the car of which is field name and the cdr of which is field content." + "Build a list of fields from subheadings of current heading, each +element of which is a cons cell, the car of which is field name +and the cdr of which is field content." (save-excursion (let (fields (point-of-last-child (point))) (when (org-goto-first-child) (while (/= point-of-last-child (point)) (setq point-of-last-child (point)) - (let* ((inhibit-message t) ;; suppress echo message from `org-babel-exp-src-block' + (let* ((inhibit-message t) ;; suppress echo message from + ;; `org-babel-exp-src-block' (field-heading (org-element-at-point)) (field-name (substring-no-properties (org-element-property @@ -568,22 +571,23 @@ Where the subtree is created depends on PREFIX." (push (cons field-name (cond - ((and contents-begin contents-end) (or (org-export-string-as - (buffer-substring - contents-begin - ;; in case the buffer is narrowed, - ;; e.g. by `org-map-entries' when - ;; scope is `tree' - (min (point-max) contents-end)) - anki-editor--ox-anki-html-backend - t - anki-editor--ox-export-ext-plist) - - ;; 8.2.10 version of - ;; `org-export-filter-apply-functions' - ;; returns nil for an input of empty string, - ;; which will cause AnkiConnect to fail - "")) + ((and contents-begin contents-end) + (or (org-export-string-as + (buffer-substring contents-begin + ;; in case the buffer is + ;; narrowed, e.g. by + ;; `org-map-entries' when scope + ;; is `tree' + (min (point-max) contents-end)) + anki-editor--ox-anki-html-backend + t + anki-editor--ox-export-ext-plist) + + ;; 8.2.10 version of + ;; `org-export-filter-apply-functions' + ;; returns nil for an input of empty string, + ;; which will cause AnkiConnect to fail + "")) (t ""))) fields) (org-forward-heading-same-level nil t)))) From 8b0fa6988accff35f2fb466800cf96bd541cb210 Mon Sep 17 00:00:00 2001 From: York Zhao Date: Sat, 1 Oct 2022 10:06:33 -0400 Subject: [PATCH 3/3] Support referencing any contents in the org file This is achieved by calling `org-export-to-buffer' to export a field (which is an org subtree) instead of treating the field as a string and calling `org-export-string-as' to export this string. The problem with the later (old) approach is that the HTML export backend is only able to see the current field (org subtree), and therefore the current field is not able to reference any contents outside of the field. --- anki-editor.el | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/anki-editor.el b/anki-editor.el index ccb502c..0ba3f66 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -549,6 +549,16 @@ Where the subtree is created depends on PREFIX." (values (and value (split-string value)))) (mapcar #'org-entry-restore-space values))) +(defun anki-editor--export-subtree () + (let ((org-export-show-temporary-export-buffer nil) + (outbuf "*anki-editor-export*")) + (org-export-to-buffer + anki-editor--ox-anki-html-backend + outbuf nil t nil t + anki-editor--ox-export-ext-plist) + (with-current-buffer outbuf + (buffer-substring-no-properties (point-min) (point-max))))) + (defun anki-editor--build-fields () "Build a list of fields from subheadings of current heading, each element of which is a cons cell, the car of which is field name @@ -565,30 +575,15 @@ and the cdr of which is field content." (field-name (substring-no-properties (org-element-property :raw-value - field-heading))) - (contents-begin (org-element-property :contents-begin field-heading)) - (contents-end (org-element-property :contents-end field-heading))) + field-heading)))) (push (cons field-name - (cond - ((and contents-begin contents-end) - (or (org-export-string-as - (buffer-substring contents-begin - ;; in case the buffer is - ;; narrowed, e.g. by - ;; `org-map-entries' when scope - ;; is `tree' - (min (point-max) contents-end)) - anki-editor--ox-anki-html-backend - t - anki-editor--ox-export-ext-plist) - - ;; 8.2.10 version of - ;; `org-export-filter-apply-functions' - ;; returns nil for an input of empty string, - ;; which will cause AnkiConnect to fail - "")) - (t ""))) + (or (anki-editor--export-subtree) + ;; 8.2.10 version of + ;; `org-export-filter-apply-functions' + ;; returns nil for an input of empty string, + ;; which will cause AnkiConnect to fail + "")) fields) (org-forward-heading-same-level nil t)))) (reverse fields))))