coleslaw/plugins/import.lisp

55 lines
2.2 KiB
Common Lisp
Raw Normal View History

(eval-when (:load-toplevel)
(ql:quickload '(coleslaw cxml cl-ppcre local-time)))
2011-04-17 17:39:14 -04:00
(defpackage :coleslaw-import
(: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)
(:import-from :cl-ppcre #:regex-replace-all))
2011-04-17 17:39:14 -04:00
(in-package :coleslaw-import)
(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))
(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")))))))
(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)
2012-08-20 20:36:57 -04:00
(format out "format: html~%") ; post format: html, md, rst, etc
(format out ";;;;;~%")
(format out "~A~%" (post-content post))))
2011-04-17 17:39:14 -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")))
(load-config)
(ensure-directories-exist (repo *config*))
(dolist (post posts)
(import-post post))))