From 1c22c76ff634882d7861d4ac1348a2c1de9d94aa Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Fri, 17 Apr 2020 16:55:54 -0400 Subject: [PATCH] Add some elisp scratch code --- copy_log.sh | 2 +- export.el | 9 --------- util.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) delete mode 100644 export.el create mode 100644 util.el diff --git a/copy_log.sh b/copy_log.sh index 7c9afdb..19b06c3 100755 --- a/copy_log.sh +++ b/copy_log.sh @@ -1,4 +1,4 @@ #!/bin/sh ./org_fix_links.py < /mnt/share/syncthing/notes/log.org > log_tmp.org -emacs -q --batch -l export.el -f export-run +emacs -q --batch -l util.el -f export-run diff --git a/export.el b/export.el deleted file mode 100644 index ed4adfd..0000000 --- a/export.el +++ /dev/null @@ -1,9 +0,0 @@ -;; See still: -;; command-line-args - -(require 'ox-hugo) -(defun export-run () - (progn - (find-file "./log_tmp.org") - (org-hugo-export-as-md) - (write-file "./log_tmp.md"))) diff --git a/util.el b/util.el new file mode 100644 index 0000000..9a0189c --- /dev/null +++ b/util.el @@ -0,0 +1,58 @@ +;; See still: +;; command-line-args + +(require 'ox-hugo) +(require 'cl) + +;; Predicate for if 'el' is a headline that has some tag. +(defun element-has-tag-p (el tag) + (if (not (eq (org-element-type el) 'headline)) + nil + (let* ((tags (org-element-property :tags el)) + (match_fn (lambda (tag_test) (string= tag tag_test))) + (match (mapcar match_fn tags))) + (some 'identity match)))) + +;; Creates a new buffer of the given name, and write the contents of +;; some org mode AST into it (e.g. from org-element-parse-buffer) +(defun interpret-to-buffer (name tree) + (let ((buf (generate-new-buffer name))) + (with-current-buffer buf + (org-mode) + (mapc 'insert (org-element-interpret-data tree))) + buf)) + +;; Modifies 'tree' destructively to remove any headline that contains +;; 'tag'. +(defun filter-out-tag (tree tag) + (if (let ((el-type (org-element-type tree))) + (not (or (eq el-type 'headline) + (eq el-type 'org-data)))) + tree + (if (element-has-tag-p tree tag) + ;; has tag - stop recursing here: + nil + ;; lacks tag - keep searching: + (org-element-set-contents + tree (mapcar (lambda (el) (filter-out-tag el tag)) + (org-element-contents tree)))))) + +;; Produce a new buffer +(defun filter-buffer (fname newbuf tag) + (let* ((buf (find-file-noselect fname)) + (tree (with-current-buffer buf (org-element-parse-buffer))) + (tree (filter-out-tag tree tag))) + (interpret-to-buffer newbuf tree))) + +(defun filter-org-tag (tag src-name dst-name) + (with-current-buffer (filter-buffer src-name "tmp.org" tag) + (write-file dst-name))) + +;; (filter-out-tag-from-file "/Users/hodapp/log_tmp2.org" "tmp.org" "dream") +;; (filter-org-tag "dream" "/Users/hodapp/log_tmp2.org" "/Users/hodapp/log_tmp_filter.org") + +(defun export-run () + (progn + (find-file "./log_tmp.org") + (org-hugo-export-as-md) + (write-file "./log_tmp.md")))