Finish updating wordpress importer.

This commit is contained in:
Brit Butler 2012-08-20 22:36:48 -04:00
parent 9ab56bddd1
commit ca37d63df8
2 changed files with 31 additions and 23 deletions

3
TODO
View file

@ -13,10 +13,11 @@ Plugins? Injection support for HEAD and BODY. What about predicate-based injecti
How is it served? Hunchentoot, Lighttpd, S3, whomever! How is it served? Hunchentoot, Lighttpd, S3, whomever!
TODO: TODO:
; fix plugins: s3
; doc themes and plugins ; doc themes and plugins
; populate prev and next links
; implement plugins: atom, markdown, pygment/highlighting ; implement plugins: atom, markdown, pygment/highlighting
; actually start using this for redlinernotes.com? Move over May-2007 to present. ; actually start using this for redlinernotes.com? Move over May-2007 to present.
; fix plugins: s3
;; Incremental compile: only "touched" posts+tags+months. By-20 must be redone, of course. ;; Incremental compile: only "touched" posts+tags+months. By-20 must be redone, of course.
;; ^^ Long as we're overhauling, maybe come up with a commithook scheme that runs main ;; ^^ Long as we're overhauling, maybe come up with a commithook scheme that runs main
;; and main becomes load-config, compile-theme, compile-blog? ;; and main becomes load-config, compile-theme, compile-blog?

View file

@ -1,4 +1,4 @@
(eval-when (:load-toplevel) (eval-when (:compile-toplevel :load-toplevel)
(ql:quickload '(coleslaw cxml cl-ppcre local-time))) (ql:quickload '(coleslaw cxml cl-ppcre local-time)))
(defpackage :coleslaw-import (defpackage :coleslaw-import
@ -7,29 +7,37 @@
#:load-config #:load-config
#:*config* #:*config*
#:repo) #:repo)
(:import-from :local-time #:+short-month-names+ (:import-from :local-time #:+short-month-names+)
#:encode-timestamp)
(:import-from :cl-ppcre #:regex-replace-all)) (:import-from :cl-ppcre #:regex-replace-all))
(in-package :coleslaw-import) (in-package :coleslaw-import)
(defun import-post (post) (defun node-val (name post)
(labels ((nodes (name) (flet ((value (node)
(dom:get-elements-by-tag-name post name))
(value (node)
(let ((child (dom:last-child node))) (let ((child (dom:last-child node)))
(when child (dom:data child)))) (when child (dom:data child)))))
(node-val (name) (let ((nodes (dom:get-elements-by-tag-name post name)))
(let ((nodes (nodes name)))
(if (string= "category" name) (if (string= "category" name)
(loop for node across nodes collecting (value node)) (loop for node across nodes collecting (value node))
(when (plusp (length nodes)) (value (elt nodes 0))))))) (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? (defun get-timestamp (post)
(export-post (node-val "title") (node-val "category") (node-val "pubDate") (destructuring-bind (day date month year time tz)
(regex-replace-all (string #\Newline) (cl-ppcre:split " " (node-val "pubDate" post))
(node-val "content:encoded") "<br>") (format nil "~a-~2,'0d-~2,'0d ~a" year (position month +short-month-names+
(format nil "~a.post" (slugify (node-val "title"))))))) :test #'string=) date time)))
(defun import-post (post)
(when (and (string= "publish" (node-val "wp:status" post)) ; is it public?
(string= "post" (node-val "wp:post_type" post)) ; is it a post?
(string>= (get-timestamp post) "2007-05"))
(let ((content (node-val "content:encoded" post))
(slug (slugify (node-val "title" post))))
(when (string= "" slug)
(error "No valid slug-title for post ~a." (get-timestamp post)))
(export-post (node-val "title" post) (node-val "category" post)
(get-timestamp post) content
(format nil "~a.post" slug)))))
(defun export-post (title tags date content path) (defun export-post (title tags date content path)
(with-open-file (out (merge-pathnames path (repo *config*)) (with-open-file (out (merge-pathnames path (repo *config*))
@ -43,12 +51,11 @@
(format out "date: ~A~%" date) (format out "date: ~A~%" date)
(format out "format: html~%") ; post format: html, md, rst, etc (format out "format: html~%") ; post format: html, md, rst, etc
(format out ";;;;;~%") (format out ";;;;;~%")
(format out "~A~%" (post-content post)))) (format out "~A~%" (regex-replace-all (string #\Newline) content "<br>"))))
(defun import-posts (filepath) (defun import-posts (filepath)
(let* ((xml (cxml:parse-file filepath (cxml-dom:make-dom-builder))) (let* ((xml (cxml:parse-file filepath (cxml-dom:make-dom-builder)))
(posts (dom:get-elements-by-tag-name xml "item"))) (posts (dom:get-elements-by-tag-name xml "item")))
(load-config) (load-config)
(ensure-directories-exist (repo *config*)) (ensure-directories-exist (repo *config*))
(dolist (post posts) (loop for post across posts do (import-post post))))
(import-post post))))