I like my urls to be the same as the title, so the first function here
normalizes the title to fit in the filesystem. I've forgotten where I
copied this code from, by thank you internet.
I have two types of posts. "mini" which just means its a standalone
file, and a full post, which is in a directory. I also turn on
automatic org-babel-tangle
on save, which I set as a local org
variable.
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
| (defun string-title-to-filename (str)
"FooBar => foo_bar"
(let ((case-fold-search nil))
(setq str (replace-regexp-in-string "\\([a-z0-9]\\)\\([A-Z]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "\\([A-Z]+\\)\\([A-Z][a-z]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "-" "_" str)) ; FOO-BAR => FOO_BAR
(setq str (replace-regexp-in-string "_+" "_" str))
(setq str (replace-regexp-in-string " " "_" str))
(downcase str)))
(transient-define-prefix blog-mode-create-menu ()
"Command for create blog post"
["Blog mode help"
("a" "Article" blog-mode-make-article-draft)
("h" "Howto" blog-mode-make-howto-draft)
("l" "Labnote" blog-mode-make-labnote-draft)
("f" "Fragment" blog-mode-make-fragment-draft)
])
(defun blog-mode-make-article-draft ()
"Create a new article"
(interactive)
(blog-mode-make-draft "articles" false))
(defun blog-mode-make-howto-draft ()
"Create a new howto"
(interactive)
(blog-mode-make-draft "howto" nil))
(defun blog-mode-make-labnote-draft ()
"Create a new labnote"
(interactive)
(blog-mode-make-draft "labnotes" nil))
(defun blog-mode-make-fragment-draft ()
"Create a new fragment"
(interactive)
(blog-mode-make-draft "fragments" t))
(defun blog-mode-make-draft (folder mini)
"Little function to create a org file inside of the blog"
(interactive)
(let* (
(title (read-from-minibuffer "Title: "))
(year (format-time-string "%Y"))
(filename (string-title-to-filename title))
(rootpath (concat blog-mode-base-dir "/" folder "/" year "/" filename))
(path (if mini (concat rootpath ".org") (concat rootpath "/index.org")))
)
(set-buffer (find-file path))
(insert "#+title: " title "\n")
(insert "#+date: " (format-time-string "%Y-%m-%dT%H:%M:%S") "\n")
(insert "#+draft: true\n")
(unless mini
(insert "\n* References\n# Local Variables:\n# eval: (add-hook 'after-save-hook (lambda ()(org-babel-tangle)) nil t)\n# End:\n"))))
|