-
Notifications
You must be signed in to change notification settings - Fork 24
/
blog-admin-backend.el
143 lines (116 loc) · 4.37 KB
/
blog-admin-backend.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
;;; blog-admin-backend.el --- backend for blog-admin -*- lexical-binding: t; -*-
;; Copyright (C) 2016
;; Author: code.falling@gmail.com
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 's)
(require 'f)
(require 'names)
(define-namespace blog-admin-backend-
(defvar plist nil
"Backend plist")
(defvar path nil
"Blog path in filesystem")
(defvar type 'hexo
"Type of blog backend, options :hexo")
(defvar new-post-in-drafts nil
"`nil`->new post will be publish, `t`-> new post will be placed in drafts")
(defvar new-post-with-same-name-dir t
"`nil`->new post will be a single file, `t`-> new post will come with a same-name directory")
(defvar after-new-post-hook nil
"Hook after new post create, should be execute at backend-*.el")
(defun build-datasource (keyword)
"Build data source from backend defined"
(let* ((blog-backend (plist-get plist keyword))
(scan-posts (plist-get blog-backend :scan-posts-func))
(read-info (plist-get blog-backend :read-info-func))
)
(mapcar (lambda (file)
"Convert info into datesource"
(let ((info (funcall read-info file)))
;; put file path at last for read from table
(list (plist-get info :date) (plist-get info :publish) (plist-get info :title) file)
)
) (funcall scan-posts))
))
(defun define (keyword backend)
"Put backend define into plist"
(setq plist (plist-put plist keyword backend)))
(defun get-backend ()
"Return backend"
(plist-get plist type)
)
;; org
;; helper
(defun -full-path (append-path)
"Return full absolute path base on `path`"
(expand-file-name
(concat
(file-name-as-directory path)
append-path)))
(defun -format-datetime (datetime)
(let*
(
(datetime-in-plist
(if (not (stringp datetime))
(plist-get (plist-get (car datetime) 'timestamp) :raw-value)) ;; orgmode 8.2.10 return a plist
nil
)
(datetime-str (cond
((eq datetime nil) "") ;; nil
((stringp datetime-in-plist) datetime-in-plist)
((stringp datetime) datetime)
(t (car datetime)) ;; some version return a list
))
(l (parse-time-string datetime-str)))
(if (equal l
'(nil nil nil nil nil nil nil nil nil)) ;; can't parse
"Can't Parse"
(format-time-string "%Y-%m-%d"
(encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l))))))
(defun -default (value default)
(if value value default))
(defun -read-org-info (post)
"Read info of org post"
(with-temp-buffer
(insert-file-contents post)
(let ((info nil))
(setq info (plist-put info :title
(s-trim (-default
(s-chop-prefix "#+TITLE:" (car (s-match "^#\\+TITLE:.*?\n" (buffer-string))))
""
))))
(plist-put info :date
(s-trim (-default
(s-chop-prefix "#+DATE:" (car (s-match "^#\\+DATE:.*?\n" (buffer-string))))
"")))
info
)
))
(defun -read-md-info (post)
"Read info of markdown post"
(with-temp-buffer
(insert-file-contents post)
(let ((info nil))
(setq info (plist-put info :title
(s-chop-prefix "title: " (car (s-match "^title: .*?\n" (buffer-string))))))
(plist-put info :date
(s-chop-prefix "date: " (car (s-match "^date: .*?\n" (buffer-string)))))
info
)
))
) ;; namespace end here
(provide 'blog-admin-backend)
;;; blog-admin-backend.el ends here