Last active
May 5, 2020 17:21
-
-
Save ympbyc/ba866f072f862ba063cfbe888e45d6a9 to your computer and use it in GitHub Desktop.
html/xml batch structural replacer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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