Skip to content

Instantly share code, notes, and snippets.

@TeMPOraL
Created October 3, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeMPOraL/92e10724b095df670453ffb9ba658b6e to your computer and use it in GitHub Desktop.
Save TeMPOraL/92e10724b095df670453ffb9ba658b6e to your computer and use it in GitHub Desktop.
Some examples of using Plump.
(defun parse-tree/fix-anchors (tree base-url)
"Fix anchor URLs in the `TREE' to work correctly with the <base> tag."
(plump:traverse tree
(lambda (node)
(when (starts-with-subseq "#" (plump:attribute node "href"))
(setf (plump:attribute node "href") (concatenate 'string base-url (plump:attribute node "href")))))
:test (lambda (node)
(and (plump:element-p node)
(string-equal (plump:tag-name node) "a")))))
(defun parse-tree/extract-urls (tree)
(let ((urls))
(plump:traverse tree
(lambda (node)
(let ((url (or (plump:attribute node "href")
(plump:attribute node "src"))))
(when url
(pushnew url urls :test #'string=))))
:test (lambda (node)
(and (plump:element-p node)
(member (plump:tag-name node)
'("a" "img")
:test #'string-equal))))
urls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment