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,66 +543,63 @@ 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-begin includes drawers and scheduling data,
(contents-end (org-element-property :contents-end field-heading)) ;; which we'd like to ignore, here we skip these
(exporter (or (org-entry-get-with-inheritance anki-editor-prop-exporter) ;; elements and reset contents-begin.
anki-editor-exporter-default)) (cl-loop for eoh = (org-element-property :contents-begin element)
(end-of-header (org-element-property :contents-begin field-heading)) then (org-element-property :end subelem)
raw-content for subelem = (progn
content-elem) (goto-char eoh)
(when (string= exporter anki-editor-exporter-raw) (org-element-context))
;; contents-begin includes drawers and scheduling data, while (memq (org-element-type subelem)
;; which we'd like to ignore, here we skip these '(drawer planning property-drawer))
;; elements and reset contents-begin. finally return (org-element-property :begin subelem)))
(while (progn (t (org-element-property :contents-begin element)))
(goto-char end-of-header) for end = (org-element-property :contents-end element)
(setq content-elem (org-element-context)) for raw = (or (and begin
(memq (car content-elem) '(drawer planning property-drawer))) end
(setq end-of-header (org-element-property :end content-elem))) (buffer-substring-no-properties
(setq contents-begin (org-element-property :begin content-elem))) begin
(setq raw-content (or (and contents-begin ;; in case the buffer is narrowed,
contents-end ;; e.g. by `org-map-entries' when
(buffer-substring ;; scope is `tree'
contents-begin (min (point-max) end)))
;; in case the buffer is narrowed, "")
;; e.g. by `org-map-entries' when for content = (cond
;; scope is `tree' ((string= exporter anki-editor-exporter-raw)
(min (point-max) contents-end))) raw)
"")) ((string= exporter anki-editor-exporter-default)
(push (cons field-name (or (org-export-string-as
(pcase exporter raw
((pred (string= anki-editor-exporter-raw)) anki-editor--ox-anki-html-backend
raw-content) t
((pred (string= anki-editor-exporter-default)) anki-editor--ox-export-ext-plist)
(or (org-export-string-as ;; 8.2.10 version of
raw-content ;; `org-export-filter-apply-functions'
anki-editor--ox-anki-html-backend ;; returns nil for an input of empty string,
t ;; which will cause AnkiConnect to fail
anki-editor--ox-export-ext-plist) ""))
;; 8.2.10 version of (t (error "Invalid exporter: %s" exporter)))
;; `org-export-filter-apply-functions' collect (cons heading content)
;; returns nil for an input of empty string, ;; proceed to next field entry and check last-pt to
;; which will cause AnkiConnect to fail ;; see if it's already the last entry
"")) do (org-forward-heading-same-level nil t)
(_ (error "Invalid exporter: %s" exporter)))) until (= last-pt (point)))))
fields)
(org-forward-heading-same-level nil t))))
(reverse fields))))
;;; Minor mode ;;; Minor mode