Add support for exporting field contents literally
This commit is contained in:
parent
5a19baefa9
commit
308cd97a9d
2 changed files with 69 additions and 20 deletions
|
@ -68,12 +68,15 @@
|
||||||
|
|
||||||
(defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE")
|
(defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE")
|
||||||
(defconst anki-editor-prop-note-id "ANKI_NOTE_ID")
|
(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-deck "ANKI_DECK")
|
||||||
(defconst anki-editor-prop-tags "ANKI_TAGS")
|
(defconst anki-editor-prop-tags "ANKI_TAGS")
|
||||||
(defconst anki-editor-prop-tags-plus (concat anki-editor-prop-tags "+"))
|
(defconst anki-editor-prop-tags-plus (concat anki-editor-prop-tags "+"))
|
||||||
(defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON")
|
(defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON")
|
||||||
(defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*")
|
(defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*")
|
||||||
(defconst anki-editor-org-tag-regexp "^\\([[:alnum:]_@#%]+\\)+$")
|
(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)
|
(defconst anki-editor-ankiconnect-version 5)
|
||||||
|
|
||||||
(defgroup anki-editor nil
|
(defgroup anki-editor nil
|
||||||
|
@ -466,6 +469,7 @@ Where the subtree is created depends on PREFIX."
|
||||||
(pcase property
|
(pcase property
|
||||||
((pred (string= anki-editor-prop-deck)) (anki-editor-deck-names))
|
((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-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))
|
((pred (string-match-p (format "%s\\+?" anki-editor-prop-tags))) (anki-editor-all-tags))
|
||||||
(_ nil)))
|
(_ nil)))
|
||||||
|
|
||||||
|
@ -552,27 +556,47 @@ name and the cdr of which is field content."
|
||||||
:raw-value
|
:raw-value
|
||||||
field-heading)))
|
field-heading)))
|
||||||
(contents-begin (org-element-property :contents-begin 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)
|
||||||
(push (cons field-name
|
anki-editor-exporter-default))
|
||||||
(cond
|
(end-of-header (org-element-property :contents-begin field-heading))
|
||||||
((and contents-begin contents-end) (or (org-export-string-as
|
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
|
(buffer-substring
|
||||||
contents-begin
|
contents-begin
|
||||||
;; in case the buffer is narrowed,
|
;; in case the buffer is narrowed,
|
||||||
;; e.g. by `org-map-entries' when
|
;; e.g. by `org-map-entries' when
|
||||||
;; scope is `tree'
|
;; scope is `tree'
|
||||||
(min (point-max) contents-end))
|
(min (point-max) contents-end)))
|
||||||
|
""))
|
||||||
|
(push (cons field-name
|
||||||
|
(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
|
anki-editor--ox-anki-html-backend
|
||||||
t
|
t
|
||||||
anki-editor--ox-export-ext-plist)
|
anki-editor--ox-export-ext-plist)
|
||||||
|
|
||||||
;; 8.2.10 version of
|
;; 8.2.10 version of
|
||||||
;; `org-export-filter-apply-functions'
|
;; `org-export-filter-apply-functions'
|
||||||
;; returns nil for an input of empty string,
|
;; returns nil for an input of empty string,
|
||||||
;; which will cause AnkiConnect to fail
|
;; which will cause AnkiConnect to fail
|
||||||
""))
|
""))
|
||||||
(t "")))
|
(_ (error "Invalid exporter: %s" exporter))))
|
||||||
fields)
|
fields)
|
||||||
(org-forward-heading-same-level nil t))))
|
(org-forward-heading-same-level nil t))))
|
||||||
(reverse fields))))
|
(reverse fields))))
|
||||||
|
|
25
examples.org
25
examples.org
|
@ -19,6 +19,31 @@
|
||||||
|
|
||||||
Cards of this note will be created in {{c1::Languages::which deck?}}
|
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 <code>:ANKI_EXPORTER: raw</code>, content of the
|
||||||
|
field will be sent to Anki <em>unprocessed</em>. You can use
|
||||||
|
whatever Anki supports, like HTML tags.
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
This property is retrieved with inheritance, meaning that it can be
|
||||||
|
set in any ancestor entries or at the top of the file with
|
||||||
|
<code>#+PROPERTY: ANKI_EXPORTER raw</code>, it's also possible to
|
||||||
|
override an outer level raw exporter with <code>:ANKI_EXPORTER:
|
||||||
|
default</code>.
|
||||||
|
|
||||||
* Languages
|
* Languages
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:ANKI_DECK: Languages
|
:ANKI_DECK: Languages
|
||||||
|
|
Loading…
Reference in a new issue