Allow specifying field values from org-mode properties (#22)
* Allow specifying field values from org-mode properties * Update documentation and fine-tune the diff * Refactor, fix indentation, and other minor fixes Renamed all-fields to named-fields since the heading and content before first subheading can also be a field. --------- Co-authored-by: Renat Galimov <renat@Renats-MacBook-Pro.local> Co-authored-by: orgtre <orgtre@posteo.net>
This commit is contained in:
parent
87394670a2
commit
ab7b33b48c
2 changed files with 113 additions and 92 deletions
13
README.org
13
README.org
|
@ -84,6 +84,19 @@ If you're using [[https://github.com/radian-software/straight.el][straight.el]]
|
||||||
This works for all note types, just make the first 2 fields absent and
|
This works for all note types, just make the first 2 fields absent and
|
||||||
~anki-editor~ will use note heading as first field and the text below the heading as second field.
|
~anki-editor~ will use note heading as first field and the text below the heading as second field.
|
||||||
|
|
||||||
|
,* You can extract a field value from an org-property
|
||||||
|
:PROPERTIES:
|
||||||
|
:ANKI_NOTE_TYPE: Basic
|
||||||
|
:ANKI_FIELD_FRONT: Can one define an anki-field inside an org-mode property?
|
||||||
|
:ANKI_PREPEND_HEADING: nil
|
||||||
|
:END:
|
||||||
|
|
||||||
|
Yes. In this example, =anki-editor= will use the =ANKI_FIELD_FRONT= property value as
|
||||||
|
a front side of the Anki card and the body of the card as its back.
|
||||||
|
|
||||||
|
,** Front
|
||||||
|
Notice that property fields will override subheading fields.
|
||||||
|
This block will be skipped
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
- Anki deck is provided by ~ANKI_DECK~ property. This property is
|
- Anki deck is provided by ~ANKI_DECK~ property. This property is
|
||||||
|
|
|
@ -442,6 +442,8 @@ The implementation is borrowed and simplified from ox-html."
|
||||||
(defconst anki-editor-prop-deck "ANKI_DECK")
|
(defconst anki-editor-prop-deck "ANKI_DECK")
|
||||||
(defconst anki-editor-prop-format "ANKI_FORMAT")
|
(defconst anki-editor-prop-format "ANKI_FORMAT")
|
||||||
(defconst anki-editor-prop-prepend-heading "ANKI_PREPEND_HEADING")
|
(defconst anki-editor-prop-prepend-heading "ANKI_PREPEND_HEADING")
|
||||||
|
(defconst anki-editor-prop-field-prefix "ANKI_FIELD_"
|
||||||
|
"Anki fields with names got from an org-node property.")
|
||||||
(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")
|
||||||
|
@ -806,6 +808,14 @@ Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
|
||||||
do (org-forward-heading-same-level nil t)
|
do (org-forward-heading-same-level nil t)
|
||||||
until (= last-pt (point)))))
|
until (= last-pt (point)))))
|
||||||
|
|
||||||
|
(defun anki-editor--property-fields (fields)
|
||||||
|
"Extract Anki FIELDS from entry properties."
|
||||||
|
(cl-loop for field in fields
|
||||||
|
for property = (concat anki-editor-prop-field-prefix (upcase field))
|
||||||
|
for property-value = (org-entry-get-with-inheritance property)
|
||||||
|
when property-value
|
||||||
|
collect (cons field property-value)))
|
||||||
|
|
||||||
(defun anki-editor--note-contents-before-subheading ()
|
(defun anki-editor--note-contents-before-subheading ()
|
||||||
"Get content between heading at point and next sub/heading.
|
"Get content between heading at point and next sub/heading.
|
||||||
|
|
||||||
|
@ -857,32 +867,30 @@ When the `subheading-fields' don't match the `note-type's fields,
|
||||||
map missing fields to the `heading' and/or `content-before-subheading'.
|
map missing fields to the `heading' and/or `content-before-subheading'.
|
||||||
Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
|
Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
|
||||||
(anki-editor--with-collection-data-updated
|
(anki-editor--with-collection-data-updated
|
||||||
(let* ((fields-matching (cl-intersection
|
(let* ((model-fields (alist-get
|
||||||
(alist-get note-type
|
note-type anki-editor--model-fields
|
||||||
anki-editor--model-fields
|
nil nil #'string=))
|
||||||
nil nil #'string=)
|
(property-fields (anki-editor--property-fields model-fields))
|
||||||
(mapcar #'car subheading-fields)
|
(named-fields (seq-uniq (append subheading-fields property-fields)
|
||||||
|
(lambda (left right)
|
||||||
|
(string= (car left) (car right)))))
|
||||||
|
(fields-matching (cl-intersection
|
||||||
|
model-fields (mapcar #'car named-fields)
|
||||||
:test #'string=))
|
:test #'string=))
|
||||||
(fields-missing (cl-set-difference
|
(fields-missing (cl-set-difference
|
||||||
(alist-get note-type
|
model-fields (mapcar #'car named-fields)
|
||||||
anki-editor--model-fields
|
|
||||||
nil nil #'string=)
|
|
||||||
(mapcar #'car subheading-fields)
|
|
||||||
:test #'string=))
|
:test #'string=))
|
||||||
(fields-extra (cl-set-difference
|
(fields-extra (cl-set-difference
|
||||||
(mapcar #'car subheading-fields)
|
(mapcar #'car named-fields) model-fields
|
||||||
(alist-get note-type
|
|
||||||
anki-editor--model-fields
|
|
||||||
nil nil #'string=)
|
|
||||||
:test #'string=))
|
:test #'string=))
|
||||||
(fields (cl-loop for f in fields-matching
|
(fields (cl-loop for f in fields-matching
|
||||||
collect (cons f (alist-get
|
collect (cons f (alist-get
|
||||||
f subheading-fields
|
f named-fields
|
||||||
nil nil #'string=))))
|
nil nil #'string=))))
|
||||||
(heading-format anki-editor-prepend-heading-format))
|
(heading-format anki-editor-prepend-heading-format))
|
||||||
(cond ((equal 0 (length fields-missing))
|
(cond ((equal 0 (length fields-missing))
|
||||||
(when (< 0 (length fields-extra))
|
(when (< 0 (length fields-extra))
|
||||||
(user-error "Failed to map all subheadings to a field")))
|
(user-error "Failed to map all named fields")))
|
||||||
((equal 1 (length fields-missing))
|
((equal 1 (length fields-missing))
|
||||||
(if (equal 0 (length fields-extra))
|
(if (equal 0 (length fields-extra))
|
||||||
(if (equal "" (string-trim content-before-subheading))
|
(if (equal "" (string-trim content-before-subheading))
|
||||||
|
|
Loading…
Reference in a new issue