git.fiddlerwoaroof.com
reading-list-reader.lisp
6f42890d
 (defpackage :reading-list-reader
   (:use :cl )
   (:export ))
 (in-package :reading-list-reader)
 
 (serapeum:eval-always
1aa0f8f7
   (named-readtables:in-readtable :objc-readtable))
6f42890d
 
1aa0f8f7
 (defun main ()
   #+(and build sbcl)
   (progn (sb-ext:disable-debugger)
          (sb-alien:alien-funcall
           (sb-alien:extern-alien "disable_lossage_handler"
                                  (function sb-alien:void))))
   (make-org-file *standard-output*
                  (translate-plist (get-bookmark-filename))))
6f42890d
 
1aa0f8f7
 (defun make-org-file (s bookmarks)
   (format s "~&* Safari Reading List~%")
   (serapeum:mapply (serapeum:partial 'make-org-entry s)
                    (get-readinglist-info bookmarks)))
6f42890d
 (defun make-org-entry (s title url preview tag)
   (format s "~&** ~a :~{~a:~}~% ~a~2% ~{~<~% ~1,80:;~a~> ~}~%"
           title (alexandria:ensure-list tag)
           url
           (serapeum:tokens preview)))
 (defun get-readinglist-info (bookmarks)
47822ccc
   (mapcar 'extract-link-info
6f42890d
           (gethash "Children"
                    (car
1aa0f8f7
                     (select-child bookmarks
                                   "com.apple.ReadingList")))))
6f42890d
 
47822ccc
 (defun extract-link-info (link)
   (list (fw.lu:pick '("URIDictionary" "title") link)
         (fw.lu:pick '("URLString") link)
         (plump:decode-entities (coerce (fw.lu:pick '("ReadingList" "PreviewText") link) 'simple-string) t)
         (fw.lu:may (slugify (fw.lu:pick '("ReadingListNonSync" "siteName") link)))))
 
1aa0f8f7
 (defun slugify (s)
   (cl-ppcre:regex-replace-all "\\s+"
                               (string-downcase s)
                               "_"))
6f42890d
 
1aa0f8f7
 (defun select-child (d title)
   (flet ((get-title (h)
            (equal (gethash "Title" h)
                   title)))
     (fw.lu:let-each (:be *)
       (gethash "Children" d)
       (remove-if-not #'get-title *))))