Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DivineDominion/6724bcbf5932be5f890e7a708e92fea1 to your computer and use it in GitHub Desktop.
Save DivineDominion/6724bcbf5932be5f890e7a708e92fea1 to your computer and use it in GitHub Desktop.
Collects all :training: subtrees from denote.el managed .org files from the past 30 days
(defun ct/denote--date-key-regexp (file-type)
(if (equal file-type 'org)
"#\\+date:"
(message "Supports only 'org' at the moment")))
(ct/denote-extract-date-from-front-matter
(defun ct/denote-extract-date-from-front-matter (date-string)
"Returns a decoded time structure.
Format:
(seconds minutes hour day month year dow dst utcoff)"
(parse-time-string date-string))
(defun ct/denote--date-value-reverse-function (file-type)
(if (equal file-type 'org)
#'ct/denote-extract-date-from-front-matter
(message "Supports only 'org' at the moment")))
(defun ct/denote-retrieve-front-matter-date (file file-type)
(denote--file-with-temp-buffer file
(when (re-search-forward (ct/denote--date-key-regexp file-type) nil t 1)
(funcall (ct/denote--date-value-reverse-function file-type)
;; Assuming the regexp applies to the whole line
(buffer-substring-no-properties (point) (line-end-position))))))
;; --------------------------------------------------------------------
(let* ((files-and-dates
(mapcar (lambda (file)
(cons file (ct/denote-retrieve-front-matter-date file 'org)))
(denote-directory-files "_journal")))
;; Threshold for how many days back from today to look.
(days-past 30)
(files-in-range (seq-keep (lambda (file-with-date)
(let* ((file (car file-with-date))
(date (cdr file-with-date))
(time (encode-time date))
(threshold-date (time-subtract (current-time) (seconds-to-time (* days-past 24 60 60)))))
(if (time-less-p time threshold-date)
nil
file)))
files-and-dates))
(export (get-buffer-create "*denote export*")))
(switch-to-buffer-other-window export)
(erase-buffer)
(org-mode)
(mapcar (lambda (file)
(denote--file-with-temp-buffer file
(while (outline-next-heading)
(when (member "training" (org-get-tags))
(let (beg end match)
(setq beg (point))
(org-end-of-subtree t nil)
(setq end (point))
(setq match (buffer-substring-no-properties beg end))
(save-window-excursion
(switch-to-buffer export)
(insert match "\n")))))))
files-in-range))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment