-
Notifications
You must be signed in to change notification settings - Fork 2
/
velocity-backend-org.el
115 lines (92 loc) · 3.45 KB
/
velocity-backend-org.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;;; DEPENDENCIES
(require 'org)
(require 'velocity-api)
;;; REGISTRATION
(velocity-register-backend
'org-file
(list :create-fn 'velocity--org-create/file
:filter-result-fn 'velocity--org-filter-result
:get-content-unit-fn 'velocity--org-get-content-unit/file))
(velocity-register-backend
'org-heading-1
(list :visit-fn 'velocity--org-visit
:create-fn 'velocity--org-create/heading-1
:filter-result-fn 'velocity--org-filter-result
:get-content-unit-fn 'velocity--org-get-content-unit/heading-1))
(velocity-register-backend
'org-heading-2
(list :visit-fn 'velocity--org-visit
:create-fn 'velocity--org-create/heading-1
:filter-result-fn 'velocity--org-filter-result
:get-content-unit-fn 'velocity--org-get-content-unit/heading-2))
;;; CALLBACKS
(defun velocity--org-visit ()
(org-show-entry))
(defun velocity--org-create/file (title)
(goto-char (point-min))
(insert "# " title "\n\n")
(list :start-pos (point-min)
:end-pos (point)))
(defun velocity--org-create/heading-1 (title)
(goto-char (point-min))
(insert "* " title "\n\n")
(list :start-pos (point-min)
:end-pos (- (point) 1)))
(defun velocity--org-filter-result (basic-result)
(let* ((snippet-lines (split-string (plist-get basic-result :snippet) "\n"))
(snippet-title (velocity--org--prettify-title (car snippet-lines)))
(snippet-body (velocity--org--prettify-body (string-join (cdr snippet-lines) " "))))
(thread-first basic-result
(plist-put :title snippet-title)
(plist-put :body snippet-body))))
(defun velocity--org-get-content-unit/file (from-pos)
(if (= from-pos (point-max))
nil
(cons 1 (point-max))))
(defun velocity--org-get-content-unit/heading-1 (from-pos)
(velocity--move-to-next-separator "^\\* " from-pos))
(defun velocity--org-get-content-unit/heading-2 (from-pos)
(velocity--move-to-next-separator "^\\*\\* " from-pos))
;;; INTERNALS
(defun velocity--org--prettify-title (title)
(thread-first title
velocity--org--strip-stars
velocity--org--strip-tags
velocity--org--remove-bracket-links
velocity--org--surface-title
(propertize 'face 'org-level-1)))
(defun velocity--org--prettify-body (body)
(propertize (velocity--org--strip-properties body)
'face 'shadow))
(defun velocity--org--remove-bracket-links (string)
(if (string-match org-bracket-link-analytic-regexp string)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (re-search-forward org-bracket-link-analytic-regexp nil t)
(replace-match (match-string 5)))
(buffer-string))
string))
(defun velocity--org--strip-stars (title)
(replace-regexp-in-string "^\\*+ "
""
title))
(defun velocity--org--strip-tags (title)
(replace-regexp-in-string "\\( *:[^ ]*:\\).*\\'"
""
title nil nil 1))
(defun velocity--org--surface-title (title)
(replace-regexp-in-string "^#\\+title: "
""
title))
(defun velocity--org--strip-properties (body)
(with-temp-buffer
(insert body)
(goto-char (point-min))
(when (re-search-forward ":PROPERTIES:" nil t)
(let ((start (match-beginning 0)))
(when (re-search-forward ":END:" nil t)
(delete-region start (point)))))
(string-trim (buffer-string))))
;;; META
(provide 'velocity-backend-org)