Refactor anki-editor--build-fields

This commit is contained in:
louie 2020-02-10 23:23:44 +08:00
parent e665f2c28f
commit b2cbf48911

View file

@ -543,54 +543,49 @@ Where the subtree is created depends on PREFIX."
(mapcar #'org-entry-restore-space values))) (mapcar #'org-entry-restore-space values)))
(defun anki-editor--build-fields () (defun anki-editor--build-fields ()
"Build a list of fields from subheadings of current heading, "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." Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
(save-excursion (save-excursion
(let (fields (cl-loop with inhibit-message = t ; suppress echo message from `org-babel-exp-src-block'
(point-of-last-child (point))) initially (unless (org-goto-first-child)
(when (org-goto-first-child) (cl-return))
(while (/= point-of-last-child (point)) for last-pt = (point)
(setq point-of-last-child (point)) for element = (org-element-at-point)
(let* ((inhibit-message t) ;; suppress echo message from `org-babel-exp-src-block' for heading = (substring-no-properties
(field-heading (org-element-at-point)) (org-element-property :raw-value element))
(field-name (substring-no-properties for exporter = (or (org-entry-get-with-inheritance anki-editor-prop-exporter)
(org-element-property anki-editor-exporter-default)
:raw-value for begin = (cond
field-heading))) ((string= exporter anki-editor-exporter-raw)
(contents-begin (org-element-property :contents-begin 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, ;; contents-begin includes drawers and scheduling data,
;; which we'd like to ignore, here we skip these ;; which we'd like to ignore, here we skip these
;; elements and reset contents-begin. ;; elements and reset contents-begin.
(while (progn (cl-loop for eoh = (org-element-property :contents-begin element)
(goto-char end-of-header) then (org-element-property :end subelem)
(setq content-elem (org-element-context)) for subelem = (progn
(memq (car content-elem) '(drawer planning property-drawer))) (goto-char eoh)
(setq end-of-header (org-element-property :end content-elem))) (org-element-context))
(setq contents-begin (org-element-property :begin content-elem))) while (memq (org-element-type subelem)
(setq raw-content (or (and contents-begin '(drawer planning property-drawer))
contents-end finally return (org-element-property :begin subelem)))
(buffer-substring (t (org-element-property :contents-begin element)))
contents-begin for end = (org-element-property :contents-end element)
for raw = (or (and begin
end
(buffer-substring-no-properties
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) end)))
"")) "")
(push (cons field-name for content = (cond
(pcase exporter ((string= exporter anki-editor-exporter-raw)
((pred (string= anki-editor-exporter-raw)) raw)
raw-content) ((string= exporter anki-editor-exporter-default)
((pred (string= anki-editor-exporter-default))
(or (org-export-string-as (or (org-export-string-as
raw-content raw
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)
@ -599,10 +594,12 @@ name and the cdr of which is field content."
;; 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
"")) ""))
(_ (error "Invalid exporter: %s" exporter)))) (t (error "Invalid exporter: %s" exporter)))
fields) collect (cons heading content)
(org-forward-heading-same-level nil t)))) ;; proceed to next field entry and check last-pt to
(reverse fields)))) ;; see if it's already the last entry
do (org-forward-heading-same-level nil t)
until (= last-pt (point)))))
;;; Minor mode ;;; Minor mode