Skip to content

Instantly share code, notes, and snippets.

@kebairia
Last active November 9, 2021 21:34
Show Gist options
  • Save kebairia/3f59ee9b53359dd4856cfe62e8b4e64c to your computer and use it in GitHub Desktop.
Save kebairia/3f59ee9b53359dd4856cfe62e8b4e64c to your computer and use it in GitHub Desktop.
i'm using Emacs and org mode to create my blog posts, so why don't i automate the process !it's Emacs after all!
;; I took `capitalize-first-char` function from here ↙↙
;; https://emacs.stackexchange.com/a/12614
(defun zk/capitalize-first-char (&optional string)
"Capitalize only the first character of the input STRING."
(when (and string (> (length string) 0))
(let ((first-char (substring string nil 1))
(rest-str (substring string 1)))
(concat (capitalize first-char) rest-str))))
;; Refine the post filename, remove spaces and subtitue them with '-'
(defun zk/refine-post-filename(string)
"Remove space from STRING"
(downcase (replace-regexp-in-string " " "-" string)))
(defun zk/create-post (&optional _post)
"Function for creating post
Prompt me for the post name, if it exist, warn me.
else write the post using the date as a prefix and use `.org' as an extension
and then put some org keywords "
(interactive)
(setq _post (read-string " Post: ")
_date (format-time-string "%Y-%m-%d" (current-time))
_ext ".org"
_path "/home/zakaria/dox/blog/content/")
;; concatenate all variables together, and subtitue all whitespaces with `-'
;; and downcase the name
(setq filename
(zk/refine-post-filename (concat _path _date "-" _post _ext)))
(if (file-exists-p filename)
(message (concat "File " (concat "'" _post "'" " already exists")))
(switch-to-buffer (find-file filename ))
;; insert template
(insert "#+TITLE: "(zk/capitalize-first-char(message _post))"\n#+SUBTITLE: \n#+AUTHOR: Zakaria.k \n#+EMAIL: "(message-user-mail-address)" \n#+DATE: "(format-time-string "%d %b %Y %a")" \n#+KEYWORDS: \n"))
;; And then start the local server
(zk/start-blog))
;; Keybinding for the function
(global-set-key (kbd "C-c P") 'zk/create-post)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment