Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Last active May 5, 2020 17:21
Show Gist options
  • Save ympbyc/ba866f072f862ba063cfbe888e45d6a9 to your computer and use it in GitHub Desktop.
Save ympbyc/ba866f072f862ba063cfbe888e45d6a9 to your computer and use it in GitHub Desktop.
html/xml batch structural replacer
(defpackage :replacer
(:use :cl :plump :clss))
(in-package :replacer)
(defun slurp (path)
(with-open-file (stream path)
(let ((data (make-string (file-length stream))))
(read-sequence data stream)
data)))
;; example
(defparameter *header* (plump:parse (slurp "header.html")))
(loop for path in (directory "./**/*.html")
for dom = (plump:parse (slurp path))
do (with-open-file (out-stream path
:direction :output
:if-exists :overwrite)
(plump:replace-child
(aref (clss:select "header" dom) 0)
*header*)
(handler-bind ((error (lambda (c) (declare (ignore c))
(invoke-restart (find-restart 'abort)))))
(plump:serialize dom out-stream))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment