-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-sync-qiita.el
154 lines (131 loc) · 4.92 KB
/
org-sync-qiita.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
;;; org-sync-qiita.el --- Oynchronize Org subtree and Qiita post -*- lexical-binding: t -*-
;; Author: Yoshihide Chubachi
;; URL: https://github.com/ychubachi/org-sync-qiita
;; Version: 0.0.1
;; Package-Requires: ((emacs "27.1") (request-deferred "0.2.0") (ox-qmd "1.0.5"))
;;; Commentary:
;; This program is free software
;;; Code:
(require 'org-sync-qiita-api)
(require 'plstore)
(require 'request-deferred)
(require 'ox-qmd)
;; TODO: delete
(defun org-sync-qiita--string-to-tags (str)
(let* ((tags (split-string str " *, *"))
(v (make-vector (length tags) nil)))
(dotimes (i (length v))
(setf (aref v i)
`(("name" . ,(nth i tags)))))
v))
(defun org-sync-qiita--tags-to-string (tags)
"[((name . \"Emacs\") (versions . []))((name . \"Org\") (versions . []))]
Emacs,Org"
(message "tags=%s" tags)
(let (str)
(dotimes (i (length tags))
(let ((s (cdr (car (aref tags i)))))
(if (> i 0)
(setq str (format "%s,%s" str s))
(setq str (format "%s" s)))))
str))
;;
(defun org-sync-qiita--create-article (title body tags private)
(org-sync-qiita--api-items-post
title
body
tags
private
#'org-sync-qiita--put-properties))
(defun org-sync-qiita--delete-article (id)
(org-sync-qiita--api-items-delete id))
(defun org-sync-qiita--update-article (id title body tags private)
(org-sync-qiita--api-items-patch
id
title
body
tags
private
#'org-sync-qiita--put-properties))
;; (org-sync-qiita--update-article "585b1a87479e561e68ec" "このIDはとっとこう非公開" "
;; # こんにちはa
;; ははむう
;; # さようなら
;; euのい
;; (org-entry-get nil \"QIITA-ID\" t)\"585b1a87479e561e68ec\"
;; " '("TAG") t)
;; (id . "0e214dd921653db51bf5")
(defun org-sync-qiita--put-properties (response)
(if (not (derived-mode-p 'org-mode))
(error "Please use this command in org-mode"))
(save-excursion
(if (org-entry-get nil "QIITA-ID" t)
(goto-char org-entry-property-inherited-from))
(if-let ((id (cdr (assoc 'id response))))
(org-entry-put nil "QIITA-ID" id))
(let ((private (cdr (assoc 'private response))))
(if (eq private :json-false) (setq private "false") (setq private "true"))
(org-entry-put nil "QIITA-PRIVATE" private))
(org-entry-put nil "QIITA-TAGS" (org-sync-qiita--tags-to-string
(cdr (assoc 'tags response))))
(org-entry-put nil "QIITA-URL" (cdr (assoc 'url response)))
(org-back-to-heading)
(org-set-tags "Qiita"))
(message "記事の投稿が完了しました"))
(defun org-sync-qiita-at-point ()
"Orgで書いた記事をQiitaに投稿します。更新もできます。"
(interactive)
(save-excursion
(let ((id (org-entry-get nil "QIITA-ID" t)))
;; go to headline which has the GIITA-ID
(if id (goto-char org-entry-property-inherited-from))
(let* ((title (org-entry-get nil "ITEM"))
(body (org-sync-qiita-qmd)))
(if (equal body "\n")
(error "記事の本文をお書きください"))
(cond
(id
(let* ((private (org-entry-get nil "QIITA-PRIVATE"))
(tags (org-entry-get nil "QIITA-TAGS")))
(if (or (not tags) (equal tags ""))
(error "QIITA-TAGSに一つ以上のTAGが必要です")
(setq tags (org-sync-qiita--string-to-tags tags)))
(if (equal private "false") (setq private nil) (setq private t))
;; Update the article
(org-sync-qiita--update-article id title body
tags
private)))
(t
;; Create a new article as private
(org-sync-qiita--create-article title body
(org-sync-qiita--string-to-tags "Org")
t)))))))
(defun org-sync-qiita-unsync-at-point ()
"Orgで書いた記事をQiitaから削除します"
(interactive)
(let ((id (org-entry-get nil "QIITA-ID")))
(org-sync-qiita--delete-article id))
(mapc (lambda (x)
(if (string-match "^QIITA-" (car x))
(org-entry-delete nil (car x))))
(org-entry-properties nil))
(org-back-to-heading)
(org-set-tags nil))
(defun org-sync-qiita-qmd ()
(interactive)
(let (qiita-markdown original-value)
;; Store the original value and change it.
(setq original-value org-export-show-temporary-export-buffer)
(setq org-export-show-temporary-export-buffer nil)
;; Export
(save-excursion
(org-qmd-export-as-markdown nil t t))
(setq qiita-markdown
(with-current-buffer "*Org QMD Export*" (buffer-string)))
;; Restore the original value.
(setq org-export-show-temporary-export-buffer original-value)
;; Kill the export buffer.
(kill-buffer "*Org QMD Export*")
qiita-markdown))
(provide 'org-sync-qiita)
;;; org-sync-qiita.el ends here