-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtributary.el
184 lines (138 loc) · 5.24 KB
/
tributary.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
;;; tributary.el --- Edit Confluence wiki pages in Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Marek Rudnicki
;; Author: Marek Rudnicki <mrkrd@posteo.de>
;; Version: 2
;; URL: https://github.com/mrkrd/tributary
;; Package-Requires: (request)
;;; Commentary:
;; This package provides a major mode to pull Confluence wiki pages
;; via REST API, edit them as XML files, and push changes back to the
;; server.
;;; Code:
(require 'request)
(defcustom tributary-api-url nil "URL to the Confluence API.")
(define-derived-mode tributary-mode nxml-mode "Tributary"
"Tributary is a major mode for editing and publishing Confluence pages."
(setq-local tributary--id nil)
(setq-local tributary--version-number nil)
(setq-local tributary--title nil)
(setq-local tributary--space-key nil)
(define-key tributary-mode-map (kbd "C-c C-c") 'tributary-push)
(define-key tributary-mode-map (kbd "C-c C-p") 'tributary-pretty-print-buffer)
(define-key tributary-mode-map (kbd "C-c C-k") 'tributary-kill-buffer)
)
(defun tributary-pull-id (id)
(interactive "sPage ID: ")
(let* ((buf-name (tributary--buffer-name id))
(prompt (format "Buffer %s already exists, overwrite?" buf-name))
(buf (get-buffer buf-name)))
(if buf
(when (yes-or-no-p prompt)
(progn
(kill-buffer buf)
(tributary--request-id id)))
(tributary--request-id id)
)))
;; (tributary-pull-id "1704722479")
(defun tributary-pull-url (url)
(interactive "sPage URL: ")
(let* ((parsed-url (url-generic-parse-url url))
(filename (url-filename parsed-url))
(_ (string-match (rx "/wiki/spaces/" (one-or-more alnum) "/pages/" (group (one-or-more digit)) "/" (zero-or-more any))
filename))
(id (match-string 1 filename))
(_ (setf (url-filename parsed-url) "/wiki/rest/api/content/"))
(api-url (url-recreate-url parsed-url)))
(tributary-pull-id id)))
(defun tributary--request-id (id)
(let ((url (concat (file-name-as-directory tributary-api-url) id "?expand=body.storage,space,version")))
(request url
:auth "basic"
:parser 'json-read
:sync t
:success (cl-function
(lambda (&key data &allow-other-keys)
(when data
(tributary--setup-current-buffer data))))
:error (cl-function
(lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))
)))
(defun tributary--buffer-name (id)
(format "*tributary*%s*" id))
(defun tributary--setup-current-buffer (data)
(let-alist data
(switch-to-buffer (tributary--buffer-name .id))
(tributary-mode)
(tributary--setup-local-vars data)
(insert "<confluence>")
(insert .body.storage.value)
(insert "\n</confluence>\n")
(tributary--set-schema)
(goto-char (point-min))
;; (sgml-pretty-print (point-min) (point-max))
)
)
(defun tributary--setup-local-vars (data)
(let-alist data
(setq tributary--id .id)
(setq tributary--version-number (1+ .version.number))
(setq tributary--space-key .space.key)
(tributary-set-title .title)
))
(defun tributary-set-title (title)
(interactive (list (read-string "New Title: " tributary--title)))
(setq tributary--title title)
(setq header-line-format (format "%s [%s]" tributary--title tributary--version-number))
)
(defun tributary--set-schema ()
(let ((dir (file-name-directory (locate-library "tributary"))))
(rng-set-schema-file (concat dir "confluence.rnc"))))
(defun tributary-push ()
(interactive)
(let ((url (concat (file-name-as-directory tributary-api-url) tributary--id))
(text (buffer-substring-no-properties (point-min) (point-max))))
(request url
:auth "basic"
:type "PUT"
:sync t
:data (json-encode `((id . ,tributary--id)
(type . "page")
(title . ,tributary--title)
(space (key . ,tributary--space-key))
(body (storage (value . ,text)
(representation . "storage")))
(version (number . ,tributary--version-number))
))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(tributary--setup-local-vars data)
(message "Version %s pushed." (let-alist data .version.number))))
:error (cl-function
(lambda (&key data &allow-other-keys)
(let-alist data
(message "ERROR %s: %s" .statusCode .message))))
)))
(defun tributary-pretty-print-buffer ()
(interactive)
(call-process-region
(point-min)
(point-max)
"tidy"
t ; delete
t ; destination
t ; display
"--input-xml" "yes"
"--output-xml" "yes"
"--indent" "yes"
"--quiet" "yes"
"--wrap" "0"))
(defun tributary-kill-buffer ()
(interactive)
(when (y-or-n-p "Kill buffer? ")
(kill-buffer))
)
(provide 'tributary)
;;; tributary.el ends here