wolfjb

bookmark-filter

Jun 22nd, 2020
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.78 KB | None | 0 0
  1. ;; gauche scheme: https://practical-scheme.net/gauche/memo.html
  2.  
  3. (use sxml.ssax)
  4. (use sxml.sxpath)
  5. (use sxml.serializer)
  6. (use sxml.tools)
  7.  
  8. (define format-filter
  9.   (lambda (type)
  10.     (format "not(contains(@href, \"~a\"))" type)))
  11.  
  12. (define href-filter
  13.   (lambda (types filters)
  14.     (cond
  15.      ((null? types)
  16.       (if (null? filters)
  17.       (format "/xbel/bookmark")
  18.       (format "/xbel/bookmark[~a]" (string-join filters " and "))))
  19.      (#t (href-filter (cdr types) (cons (format-filter (car types))  filters))))))
  20.  
  21.  
  22. ;; works in repl to give a list that does not contain image types:
  23. ;; ((sxpath "//bookmark[not(contains(@href, \"jpg\")) and not(contains(@href, \"svg\"))]") bs)
  24.  
  25. (define add-root
  26.   (lambda (results)
  27.     `(*TOP*
  28.       (@@ (*NAMESPACES* (ns0 "http://www.freedesktop.org/standards/shared-mime-info")
  29.             (ns1 "http://www.freedesktop.org/standards/desktop-bookmarks")))
  30.       (xbel (@ (version "1.0")) ,@results))))
  31.  
  32. (define main
  33.   (lambda (argv)
  34.     (with-input-from-file "example-small.xbel"
  35.       (lambda ()
  36.     (let* ((bookmarks
  37.         (ssax:xml->sxml (current-input-port)
  38.                 '((ns0 . "http://www.freedesktop.org/standards/shared-mime-info")
  39.                   (ns1 . "http://www.freedesktop.org/standards/desktop-bookmarks"))))
  40.            (filters (href-filter (cdr argv) '())))
  41.       (display (format "~%filters: ~a~%" filters))
  42.       (display (format "~%results: ~a~%" ((sxpath filters) bookmarks)))
  43.       (display (format "~%xml    : ~a~%" (srl:sxml->xml (add-root ((sxpath filters) bookmarks)))))
  44.       (with-output-to-file "example-filtered.xbel"
  45.         (lambda ()
  46.           ;; doesn't quite work, namespaces are all jacked up - I think
  47.           (srl:sxml->xml (add-root ((sxpath filters) bookmarks)) (current-output-port)))))))))
Add Comment
Please, Sign In to add comment