2012-08-20 20:17:30 -04:00
|
|
|
(eval-when (:load-toplevel)
|
|
|
|
(ql:quickload '(coleslaw cxml cl-ppcre local-time)))
|
2011-04-17 17:39:14 -04:00
|
|
|
|
|
|
|
(defpackage :coleslaw-import
|
2012-08-20 20:17:30 -04:00
|
|
|
(:use :cl :cxml)
|
|
|
|
(:import-from :coleslaw #:slugify
|
|
|
|
#:load-config
|
|
|
|
#:*config*
|
|
|
|
#:repo)
|
2011-04-17 17:39:14 -04:00
|
|
|
(:import-from :local-time #:+short-month-names+
|
|
|
|
#:encode-timestamp)
|
2011-04-17 19:07:04 -04:00
|
|
|
(:import-from :cl-ppcre #:regex-replace-all))
|
2011-04-17 17:39:14 -04:00
|
|
|
|
|
|
|
(in-package :coleslaw-import)
|
|
|
|
|
2012-08-20 20:17:30 -04:00
|
|
|
(defun import-post (post)
|
2011-04-17 17:39:14 -04:00
|
|
|
(labels ((nodes (name)
|
|
|
|
(dom:get-elements-by-tag-name post name))
|
|
|
|
(value (node)
|
|
|
|
(let ((child (dom:last-child node)))
|
|
|
|
(when child (dom:data child))))
|
|
|
|
(node-val (name)
|
|
|
|
(let ((nodes (nodes name)))
|
|
|
|
(if (string= "category" name)
|
|
|
|
(loop for node across nodes collecting (value node))
|
2012-08-20 20:17:30 -04:00
|
|
|
(when (plusp (length nodes)) (value (elt nodes 0)))))))
|
|
|
|
(when (and (string= "publish" (node-val "wp:status")) ; is it public?
|
|
|
|
(string= "post" (node-val "wp:post_type"))) ; is it a post?
|
|
|
|
(export-post (node-val "title") (node-val "category") (node-val "pubDate")
|
|
|
|
(regex-replace-all (string #\Newline)
|
|
|
|
(node-val "content:encoded") "<br>")
|
|
|
|
(format nil "~a.post" (slugify (node-val "title")))))))
|
2011-04-20 13:45:59 -04:00
|
|
|
|
2012-08-20 20:17:30 -04:00
|
|
|
(defun export-post (title tags date content path)
|
|
|
|
(with-open-file (out (merge-pathnames path (repo *config*))
|
|
|
|
:direction :output
|
|
|
|
:if-exists :supersede
|
|
|
|
:if-does-not-exist :create)
|
|
|
|
;; TODO: What other data/metadata should we write out?
|
|
|
|
(format out ";;;;;~%")
|
|
|
|
(format out "title: ~A~%" title)
|
|
|
|
(format out "tags: ~A~%" (format nil "~{~A, ~}" tags))
|
|
|
|
(format out "date: ~A~%" date)
|
|
|
|
(format out "type: html~%") ; post format: html, md, rst, etc
|
|
|
|
(format out ";;;;;~%")
|
|
|
|
(format out "~A~%" (post-content post))))
|
2011-04-17 17:39:14 -04:00
|
|
|
|
2012-08-20 20:17:30 -04:00
|
|
|
(defun import-posts (filepath)
|
2011-04-17 17:39:14 -04:00
|
|
|
(let* ((xml (cxml:parse-file filepath (cxml-dom:make-dom-builder)))
|
|
|
|
(posts (dom:get-elements-by-tag-name xml "item")))
|
2012-08-20 20:17:30 -04:00
|
|
|
(load-config)
|
|
|
|
(ensure-directories-exist (repo *config*))
|
|
|
|
(dolist (post posts)
|
|
|
|
(import-post post))))
|