git.fiddlerwoaroof.com
Browse code

Use README.org as source of truth for source

Ed Langley authored on 22/09/2018 04:17:50
Showing 2 changed files
... ...
@@ -1,3 +1,12 @@
1
+(defpackage :objc-runtime.data-extractors
2
+  (:use :cl )
3
+  (:export
4
+   #:extract-from-objc
5
+   #:define-extractor
6
+   #:clear-extractors
7
+   #:add-extractor
8
+   #:get-plist))
9
+
1 10
 (in-package :objc-runtime.data-extractors)
2 11
 (named-readtables:in-readtable :objc-readtable)
3 12
 
... ...
@@ -35,7 +44,7 @@
35 44
     (mapcar (serapeum:op
36 45
               `(if (objc-isa ,obj ,(car _1))
37 46
                    (progn ,@(cdr _1))))
38
-            cases)))
47
+                   cases)))
39 48
 
40 49
 (defmacro objc-typecase (form &body ((case-type &body case-handler) &rest cases))
41 50
   (alexandria:once-only (form)
... ...
@@ -66,33 +75,14 @@
66 75
         (t (format t "~&other... ~s~%" (objc-runtime::objc-class-get-name
67 76
                                         (objc-runtime::object-get-class obj))))))))
68 77
 
69
-(defvar *objc-extractors* (list)
70
-  "Functions called to extract specific data types")
71
-
72
-(serapeum:eval-always
73
-  (defun add-extractor (class cb)
74
-    (unless (member class *objc-extractors* :test 'cffi:pointer-eq :key #'car)
75
-      (setf *objc-extractors*
76
-            (merge 'list *objc-extractors* (list (cons class cb))
77
-                   'objc-subclass-p
78
-                   :key 'car)))
79
-    *objc-extractors*))
80
-
81
-(defmacro define-extractor (class (o) &body body)
82
-  `(serapeum:eval-always
83
-     (add-extractor ,class
84
-                    (lambda (,o)
85
-                      ,@body))
86
-     *objc-extractors*))
87
-
88
-(defun clear-extractors ()
89
-  (setf *objc-extractors* ()))
90
-
91 78
 (defmacro funcall-some (fun &rest args)
92 79
   (alexandria:once-only (fun)
93 80
     `(if ,fun
94 81
          (funcall ,fun ,@args))))
95 82
 
83
+(defvar *objc-extractors* (list)
84
+  "Functions called to extract specific data types")
85
+
96 86
 (defun extract-from-objc (obj)
97 87
   (objc-typecase obj
98 88
     (#@NSDate [[[[#@NSISO8601DateFormatter @(alloc)]
... ...
@@ -110,3 +100,22 @@
110 100
     (t (or (funcall-some (cdr (objc-pick-by-type obj *objc-extractors*))
111 101
                          obj)
112 102
            obj))))
103
+
104
+(defmacro define-extractor (class (o) &body body)
105
+  `(serapeum:eval-always
106
+     (add-extractor ,class
107
+                    (lambda (,o)
108
+                      ,@body))
109
+     *objc-extractors*))
110
+
111
+(defun clear-extractors ()
112
+  (setf *objc-extractors* ()))
113
+
114
+(serapeum:eval-always
115
+  (defun add-extractor (class cb)
116
+    (unless (member class *objc-extractors* :test 'cffi:pointer-eq :key #'car)
117
+      (setf *objc-extractors*
118
+            (merge 'list *objc-extractors* (list (cons class cb))
119
+                   'objc-subclass-p
120
+                   :key 'car)))
121
+    *objc-extractors*))
... ...
@@ -15,50 +15,28 @@
15 15
   (make-org-file *standard-output*
16 16
                  (translate-plist (get-bookmark-filename))))
17 17
 
18
-(defparameter *reading-list-location* "Library/Safari/Bookmarks.plist")
19
-(defun get-bookmark-filename ()
20
-  (merge-pathnames *reading-list-location*
21
-                   (truename "~/")))
22
-
23
-(defun translate-plist (fn)
24
-  (objc-runtime.data-extractors:extract-from-objc
25
-   (objc-runtime.data-extractors:get-plist fn)))
26
-
27 18
 (defun make-org-file (s bookmarks)
28 19
   (format s "~&* Safari Reading List~%")
29 20
   (serapeum:mapply (serapeum:partial 'make-org-entry s)
30 21
                    (get-readinglist-info bookmarks)))
31
-
32 22
 (defun make-org-entry (s title url preview tag)
33 23
   (format s "~&** ~a :~{~a:~}~% ~a~2% ~{~<~% ~1,80:;~a~> ~}~%"
34 24
           title (alexandria:ensure-list tag)
35 25
           url
36 26
           (serapeum:tokens preview)))
37
-
38 27
 (defun get-readinglist-info (bookmarks)
39
-  (mapcar (serapeum:juxt
40
-           (fw.lu:op
41
-             (fw.lu:pick '("URIDictionary" "title")
42
-                         _))
43
-           (fw.lu:op
44
-             (fw.lu:pick '("URLString")
45
-                         _))
46
-           (fw.lu:op
47
-             (plump:decode-entities
48
-              (coerce (fw.lu:pick '("ReadingList" "PreviewText")
49
-                                  _)
50
-                      'simple-string)
51
-              t))
52
-           (fw.lu:op
53
-             (fw.lu:may
54
-               (slugify
55
-                (fw.lu:pick '("ReadingListNonSync" "siteName")
56
-                            _)))))
28
+  (mapcar 'extract-link-info
57 29
           (gethash "Children"
58 30
                    (car
59 31
                     (select-child bookmarks
60 32
                                   "com.apple.ReadingList")))))
61 33
 
34
+(defun extract-link-info (link)
35
+  (list (fw.lu:pick '("URIDictionary" "title") link)
36
+        (fw.lu:pick '("URLString") link)
37
+        (plump:decode-entities (coerce (fw.lu:pick '("ReadingList" "PreviewText") link) 'simple-string) t)
38
+        (fw.lu:may (slugify (fw.lu:pick '("ReadingListNonSync" "siteName") link)))))
39
+
62 40
 (defun slugify (s)
63 41
   (cl-ppcre:regex-replace-all "\\s+"
64 42
                               (string-downcase s)