diff --git a/anki-editor.el b/anki-editor.el index 8a5dee0..e3c9df5 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -68,12 +68,15 @@ (defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE") (defconst anki-editor-prop-note-id "ANKI_NOTE_ID") +(defconst anki-editor-prop-exporter "ANKI_EXPORTER") (defconst anki-editor-prop-deck "ANKI_DECK") (defconst anki-editor-prop-tags "ANKI_TAGS") (defconst anki-editor-prop-tags-plus (concat anki-editor-prop-tags "+")) (defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON") (defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*") (defconst anki-editor-org-tag-regexp "^\\([[:alnum:]_@#%]+\\)+$") +(defconst anki-editor-exporter-raw "raw") +(defconst anki-editor-exporter-default "default") (defconst anki-editor-ankiconnect-version 5) (defgroup anki-editor nil @@ -466,6 +469,7 @@ Where the subtree is created depends on PREFIX." (pcase property ((pred (string= anki-editor-prop-deck)) (anki-editor-deck-names)) ((pred (string= anki-editor-prop-note-type)) (anki-editor-note-types)) + ((pred (string= anki-editor-prop-exporter)) (list anki-editor-exporter-raw anki-editor-exporter-default)) ((pred (string-match-p (format "%s\\+?" anki-editor-prop-tags))) (anki-editor-all-tags)) (_ nil))) @@ -552,27 +556,47 @@ name and the cdr of which is field content." :raw-value field-heading))) (contents-begin (org-element-property :contents-begin field-heading)) - (contents-end (org-element-property :contents-end field-heading))) - + (contents-end (org-element-property :contents-end field-heading)) + (exporter (or (org-entry-get-with-inheritance anki-editor-prop-exporter) + anki-editor-exporter-default)) + (end-of-header (org-element-property :contents-begin field-heading)) + raw-content + content-elem) + (when (string= exporter anki-editor-exporter-raw) + ;; contents-begin includes drawers and scheduling data, + ;; which we'd like to ignore, here we skip these + ;; elements and reset contents-begin. + (while (progn + (goto-char end-of-header) + (setq content-elem (org-element-context)) + (memq (car content-elem) '(drawer planning property-drawer))) + (setq end-of-header (org-element-property :end content-elem))) + (setq contents-begin (org-element-property :begin content-elem))) + (setq raw-content (or (and contents-begin + contents-end + (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))) + "")) (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 ""))) + (pcase exporter + ((pred (string= anki-editor-exporter-raw)) + raw-content) + ((pred (string= anki-editor-exporter-default)) + (or (org-export-string-as + raw-content + 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 + "")) + (_ (error "Invalid exporter: %s" exporter)))) fields) (org-forward-heading-same-level nil t)))) (reverse fields)))) diff --git a/examples.org b/examples.org index 8e06c70..58e846c 100644 --- a/examples.org +++ b/examples.org @@ -19,6 +19,31 @@ Cards of this note will be created in {{c1::Languages::which deck?}} +* Raw fields + :PROPERTIES: + :ANKI_NOTE_TYPE: Basic + :END: + +** Front + + How to send the content of a field or fields to Anki as is? + +** Back + :PROPERTIES: + :ANKI_EXPORTER: raw + :END: + + With property :ANKI_EXPORTER: raw, content of the + field will be sent to Anki unprocessed. You can use + whatever Anki supports, like HTML tags. +
+
+ This property is retrieved with inheritance, meaning that it can be + set in any ancestor entries or at the top of the file with + #+PROPERTY: ANKI_EXPORTER raw, it's also possible to + override an outer level raw exporter with :ANKI_EXPORTER: + default. + * Languages :PROPERTIES: :ANKI_DECK: Languages