Add defaults for the data from the org buffer

This commit is contained in:
Tom Willemse 2021-06-24 22:02:52 -07:00
parent 7b5322b313
commit 7ce1d86c05

View file

@ -44,30 +44,44 @@
(defvar tekuti-host "http://127.0.0.1:8080") (defvar tekuti-host "http://127.0.0.1:8080")
(defun tekuti--format-date (date) (defun tekuti--format-date (&optional date)
"Format DATE into a date that tekuti understands." "Format DATE into a date that tekuti understands."
(format-time-string "%a, %d %b %Y %H:%M:%S %Z" (format-time-string "%a, %d %b %Y %H:%M:%S %Z"
(org-timestamp-to-time (unless (null date)
(org-timestamp-from-string date)) (org-timestamp-to-time
(org-timestamp-from-string date)))
t)) t))
(defun tekuti-build-data-from-org () (defun tekuti-build-data-from-org ()
(append "Parse the current org buffer to collect the necessary metadata."
(mapcar (let ((defaults `(("TITLE")
(lambda (pair) ("DATE" ,(tekuti--format-date))
(pcase pair ("TAGS")
(`("DATE" ,date) ("STATUS")
(list "DATE" (tekuti--format-date date))) ("COMMENTS")
(`("COMMENTS_DATE" ,date) ("COMMENTS-CLOSED-DATE")
(list "COMMENTS-CLOSED-DATE" ("BODY")))
(and (not (string-empty-p date)) (data (append
(tekuti--format-date date)))) (mapcar
(_ pair))) (lambda (pair)
(org-collect-keywords '("TITLE" "TAGS" "STATUS" "COMMENTS" "DATE" "COMMENTS_DATE"))) (pcase pair
(list (`("DATE" ,date)
(list "BODY" (list "DATE" (tekuti--format-date date)))
(with-current-buffer (org-html-export-as-html nil nil nil t) (`("COMMENTS_DATE" ,date)
(buffer-substring-no-properties (point-min) (point-max))))))) (list "COMMENTS-CLOSED-DATE"
(and (not (string-empty-p date))
(tekuti--format-date date))))
(_ pair)))
(org-collect-keywords '("TITLE" "TAGS" "STATUS" "COMMENTS" "DATE" "COMMENTS_DATE")))
(list
(list "BODY"
(with-current-buffer (org-html-export-as-html nil nil nil t)
(buffer-substring-no-properties (point-min) (point-max))))))))
(mapcar (lambda (item)
(let ((key (car item)))
`(,key ,@(or (alist-get key data nil nil #'string=)
(cdr item)))))
defaults)))
(defun tekuti-form-encode-pair (key value) (defun tekuti-form-encode-pair (key value)
(concat (url-hexify-string key) "=" (url-hexify-string value))) (concat (url-hexify-string key) "=" (url-hexify-string value)))
@ -79,6 +93,9 @@
data data
"&")) "&"))
(defun tekuti--send-finished (status &rest _)
(message "Finished: %s" status))
;;;###autoload ;;;###autoload
(defun tekuti-send () (defun tekuti-send ()
(interactive) (interactive)
@ -87,8 +104,7 @@
(url-request-extra-headers (url-request-extra-headers
`(("Content-Type" . "application/x-www-form-urlencoded"))) `(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data (tekuti-form-encode-data data))) (url-request-data (tekuti-form-encode-data data)))
(url-retrieve (format "%s/admin/new-post" tekuti-host) (url-retrieve (format "%s/admin/new-post" tekuti-host) #'tekuti--send-finished)))
(lambda (status &rest _) (message "Finished: %s" status)))))
(provide 'tekuti) (provide 'tekuti)
;;; tekuti.el ends here ;;; tekuti.el ends here