Skip to content

Instantly share code, notes, and snippets.

@tanakahx
Created August 29, 2014 16:17
Show Gist options
  • Save tanakahx/e98f96e8b2f7673ce741 to your computer and use it in GitHub Desktop.
Save tanakahx/e98f96e8b2f7673ce741 to your computer and use it in GitHub Desktop.
Update date and time in a octopress's markdown file.
;;; Update date and time in a octopress's markdown file.
;;; This script also updates date in the file name.
(load "~/quicklisp/setup.lisp")
(ql:quickload :cl-ppcre)
;; Temporary file created in the current directory
(defparameter *temp-filename* "temp.txt")
(defun time-string (&optional detail)
"Returne current time in string.
If detail is not nil then return it with hour, minute and second."
(multiple-value-bind
(second minute hour date month year day-of-week dst-p tz)
(get-decoded-time)
(if detail
(format nil "~a-~2,'0d-~2,'0d ~2,'0d:~2,'0d:~2,'0d +0900"
year month date hour minute second)
(format nil "~a-~2,'0d-~2,'0d"
year month date))))
(defun update-and-output-to-file (input-file output-file)
"Update date in input-file and write it to output-file."
(with-open-file (out output-file :direction :output :if-exists :supersede)
(let ((*standard-output* out))
(update-file-contents input-file))))
(defun update-file-contents (input-file)
"Search 'date: yyyy-mm-dd-' and replace it with current time."
(with-open-file (in input-file)
(loop for line = (read-line in nil)
while line
do (if (ppcre:scan "^date" line)
(format t "~a~%" (concatenate 'string "date: " (time-string t)))
(format t "~a~%" line)))))
(defun up-to-date-filename (filename)
"Replace date contained in filename with current date. The date format is yyyy-mm-dd."
(let ((path (make-pathname :defaults filename)))
(multiple-value-bind (string matchp)
(ppcre:regex-replace "\\d\\d\\d\\d-\\d\\d-\\d\\d" (pathname-name path) (time-string))
(if matchp
(namestring (make-pathname :name string :defaults filename))
filename))))
(defun main (args)
(when (null (cdr args))
(format t "Usage: update-md file~%")
(quit))
(let ((filename (car (cdr sb-ext:*posix-argv*))))
(update-and-output-to-file filename *temp-filename*)
(delete-file (pathname filename))
(rename-file (pathname *temp-filename*)
(pathname (up-to-date-filename filename)))))
(main sb-ext:*posix-argv*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment