Flexible Lisp Blogware. Fork for personal use. Mirrored from https://github.com/kingcons/coleslaw originally.

import.lisp 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (eval-when (:compile-toplevel :load-toplevel)
  2. (ql:quickload '(coleslaw cxml cl-ppcre local-time)))
  3. (defpackage :coleslaw-import
  4. (:use :cl :cxml)
  5. (:export #:enable)
  6. (:import-from :coleslaw #:slugify
  7. #:load-config
  8. #:*config*
  9. #:repo)
  10. (:import-from :local-time #:+short-month-names+)
  11. (:import-from :cl-ppcre #:regex-replace-all))
  12. (in-package :coleslaw-import)
  13. (defun node-val (name post)
  14. (flet ((value (node)
  15. (let ((child (dom:last-child node)))
  16. (when child (dom:data child)))))
  17. (let ((nodes (dom:get-elements-by-tag-name post name)))
  18. (if (string= "category" name)
  19. (loop for node across nodes collecting (value node))
  20. (when (plusp (length nodes)) (value (elt nodes 0)))))))
  21. (defun get-timestamp (post)
  22. (destructuring-bind (day date month year time tz)
  23. (cl-ppcre:split " " (node-val "pubDate" post))
  24. (format nil "~a-~2,'0d-~2,'0d ~a" year (position month +short-month-names+
  25. :test #'string=) date time)))
  26. (defun import-post (post output &optional since)
  27. (when (and (string= "publish" (node-val "wp:status" post)) ; is it public?
  28. (string= "post" (node-val "wp:post_type" post)) ; is it a post?
  29. (or (null since) (string>= (get-timestamp post) since)))
  30. (let ((slug (slugify (node-val "title" post))))
  31. (when (string= "" slug)
  32. (error "No valid slug-title for post ~a." (get-timestamp post)))
  33. (export-post (node-val "title" post) (node-val "category" post)
  34. (get-timestamp post) (node-val "content:encoded" post)
  35. (format nil "~a.post" slug) output))))
  36. (defun export-post (title tags date content path output)
  37. (with-open-file (out (merge-pathnames path (or output (repo *config*)))
  38. :direction :output
  39. :if-exists :supersede
  40. :if-does-not-exist :create
  41. :external-format :utf-8)
  42. ;; TODO: What other data/metadata should we write out?
  43. (format out ";;;;;~%")
  44. (format out "title: ~A~%" title)
  45. (format out "tags: ~A~%" (format nil "~{~A~^, ~}" tags))
  46. (format out "date: ~A~%" date)
  47. (format out "format: html~%") ; post format: html, md, rst, etc
  48. (format out ";;;;;~%")
  49. (format out "~A~%" (regex-replace-all (string #\Newline) content "<br>"))))
  50. (defun import-posts (filepath output &optional since)
  51. (when (probe-file filepath)
  52. (ensure-directories-exist (or output (repo *config*)))
  53. (let* ((xml (cxml:parse-file filepath (cxml-dom:make-dom-builder)))
  54. (posts (dom:get-elements-by-tag-name xml "item")))
  55. (loop for post across posts do (import-post post output since))
  56. (delete-file filepath))))
  57. (defun enable (&key filepath output)
  58. (import-posts filepath output))