git.fiddlerwoaroof.com
Browse code

Remove iterator definitions, add various utility definitions

fiddlerwoaroof authored on 30/01/2017 20:50:51
Showing 7 changed files
... ...
@@ -1,2 +1,6 @@
1 1
 .*.sw[a-z]
2 2
 *~
3
+#.*#
4
+*.fasl
5
+#*#
6
+[#]*[#]
... ...
@@ -10,6 +10,7 @@
10 10
                #:drakma
11 11
                #:for
12 12
                #:fwoar.lisputils
13
+	       #:collection-class
13 14
                #:lquery
14 15
                #:plump
15 16
                #:serapeum
... ...
@@ -18,9 +19,9 @@
18 19
                #:split-sequence)
19 20
   :serial t
20 21
   :components ((:file "package")
21
-               (:file "collections")
22
-               (:file "collections-for")
23
-               #+sbcl (:file "collections-sbcl-iterators")
22
+               ;; (:file "collections")
23
+               ;; (:file "collections-for")
24
+               ;; #+sbcl (:file "collections-sbcl-iterators")
24 25
 
25 26
                (:file "alimenta")  
26 27
                (:file "data-class")
... ...
@@ -1,5 +1,5 @@
1 1
 ;;;; alimenta.lisp
2
-(declaim (optimize (speed 0) (safety 3) (debug 4)))
2
+(declaim (optimize (speed 0) (safety 3) (debug 3)))
3 3
 
4 4
 (in-package #:alimenta)
5 5
 
... ...
@@ -33,11 +33,13 @@
33 33
   (:documentation "Given a lisp object representing a feed, return an xml
34 34
                    document"))
35 35
 
36
+(defgeneric content-el (entity)
37
+  (:documentation "Return the element that contains the item's content"))
38
+
36 39
 (defclass item (feed-entity)
37 40
   ((author :initarg :author :initform nil :accessor author)
38 41
    (content :initarg :content :initform nil :accessor content)
39 42
    (date :initarg :date :initform nil :accessor date)
40
-   (doc :initarg :doc :initform nil :accessor doc)
41 43
    (id :initarg :id :initform nil :accessor id)
42 44
    (links :initform (make-hash-table :test #'equalp) :accessor links)))
43 45
 
... ...
@@ -97,6 +99,16 @@
97 99
             do (generate-xml item feed-type :partial result)))
98 100
     result))
99 101
 
102
+(defmethod -to-feed ((doc stream) doc-type &key feed-link)
103
+  (-to-feed (plump:parse doc)
104
+	    doc-type
105
+	    :feed-link feed-link))
106
+
107
+(defmethod -to-feed ((doc string) doc-type &key feed-link)
108
+  (-to-feed (plump:parse doc)
109
+	    doc-type
110
+	    :feed-link feed-link))
111
+    
100 112
 (defmethod -to-feed :around ((xml-dom plump:node) doc-type &key feed-link)
101 113
   "This wraps the particular methods so that _they_ don't have to implement
102 114
    item fetching.  NIL passed to the type activates auto-detection"
... ...
@@ -144,10 +156,9 @@
144 156
   (let ((root-node-name (make-keyword (string-upcase
145 157
                                         ($ (inline xml-dom) (children)
146 158
                                            (map #'tag-name) (node))))))
147
-    (setf type
148
-          (case root-node-name
149
-            ((:feed) :atom)
150
-            (t root-node-name)))))
159
+    (case root-node-name
160
+      ((:feed) :atom)
161
+      (t root-node-name))))
151 162
 
152 163
 (defgeneric get-random-item (feed)
153 164
   (:method ((feed feed))
... ...
@@ -117,10 +117,10 @@
117 117
   (list*
118 118
     'progn
119 119
     (loop for (name value &optional doc) in constants
120
-        collect `(defconstant ,name ,value ,doc))))
120
+        collect `(defconstant ,name ,value ,@(when doc (list doc))))))
121 121
 
122 122
 (defvar *defconstants-really-verbose* nil)
123
-(defmacro defconstants-really (&body constants)
123
+#+sbcl (defmacro defconstants-really (&body constants)
124 124
   "auto-invoke the continue restart . . ."
125 125
   `(handler-bind ((sb-ext:defconstant-uneql
126 126
                     (lambda (c)
... ...
@@ -132,6 +132,9 @@
132 132
                       (continue c))))
133 133
      (defconstants ,@constants)))
134 134
 
135
+#-sbcl (defmacro defconstants-really (&body constants)
136
+	 `(defconstants ,@constants))
137
+
135 138
 (defmethod generate-xml ((feed feed) (feed-type (eql :atom)) &key partial)
136 139
   (let ((feed-root (or ($1 (inline partial) "feed")
137 140
                     (plump:make-element (plump:make-root) "feed"))))
... ...
@@ -3,6 +3,7 @@
3 3
 
4 4
 (defmacro setup-libraries-for-feeds (&body body)
5 5
   `(let ((plump:*tag-dispatchers* plump:*xml-tags*)
6
+	 (drakma:*drakma-default-external-format* :utf-8)
6 7
          (drakma:*text-content-types* 
7 8
            (pairlis '("application" "application")
8 9
                     '("atom+xml"    "rss+xml")
... ...
@@ -39,8 +40,10 @@
39 40
 
40 41
 (defun fetch-feed-from-url (url &key type)
41 42
   (setup-libraries-for-feeds
42
-    (let* ((feeds (alimenta.discover:discover-feed (drakma:http-request url :user-agent *user-agent*)))
43
-           (feeds (if type (remove-if-not (lambda (x) (eql type (car x))) feeds) feeds)))
43
+    (let* ((feeds (alimenta.discover:discover-feed (drakma:http-request url
44
+									:user-agent *user-agent*
45
+									:decode-content t)))
46
+	   (feeds (if type (remove-if-not (lambda (x) (eql type (car x))) feeds) feeds)))
44 47
       (if (not feeds) (no-feed url)
45 48
         (fetch-doc-from-url
46 49
           (cdar 
... ...
@@ -16,7 +16,7 @@
16 16
   (:export #:to-feed #:generate-xml #:feed #:title #:link #:items #:feed-link
17 17
            #:doc #:source-type #:id #:date #:content #:item #:description
18 18
            #:%generate-xml #:%to-feed #:get-items #:make-item #:complex-value
19
-           #:primary-value #:render #:author))
19
+           #:primary-value #:render #:author #:content-el))
20 20
 
21 21
 (defpackage #:alimenta.html
22 22
   (:use #:cl #:should-test #:lquery #:alexandria #:anaphora #:alimenta #:data-class
... ...
@@ -29,7 +29,7 @@
29 29
   (:export #:language #:copyright #:managing-editor #:webmaster
30 30
            #:publication-date #:last-build-date #:categories #:generator #:docs
31 31
            #:cloud #:ttl #:image #:rating #:text-input #:skip-hours #:skip-days
32
-           #:rss-feed #:rss-item))
32
+           #:rss-feed #:rss-item #:category #:domain))
33 33
 
34 34
 (defpackage #:alimenta.atom
35 35
   (:use #:cl #:should-test #:lquery #:plump #:alexandria #:anaphora #:alimenta))
... ...
@@ -1,7 +1,6 @@
1 1
 (declaim (optimize (speed 0) (safety 3) (debug 3)))
2 2
 (in-package :alimenta.rss)
3 3
 
4
-
5 4
 (defclass rss-image ()
6 5
   ((url :initarg :url :initform nil)
7 6
    (title :initarg :title :initform nil)
... ...
@@ -11,8 +10,8 @@
11 10
    (description :initarg :description :initform nil)))
12 11
 
13 12
 (defclass rss-category ()
14
-  ((category :initarg :category :initform nil)
15
-   (domain :initarg :domain :initform nil)))
13
+  ((category :initarg :category :accessor category :initform nil)
14
+   (domain :initarg :domain :accessor domain :initform nil)))
16 15
 
17 16
 (define-data-class rss-feed (doc "channel") (feed)
18 17
   language copyright webmaster
... ...
@@ -36,7 +35,7 @@
36 35
 
37 36
 (define-data-class rss-item (doc "") (item)
38 37
   (categories "category" :value (get-categories doc "> category"))
39
-  source comments enclosure )
38
+  source comments enclosure description)
40 39
 
41 40
 (defmethod print-object ((self rss-image) stream)
42 41
   (print-unreadable-object (self stream :type t :identity t)
... ...
@@ -58,7 +57,6 @@
58 57
   (loop for char across str
59 58
         always (alpha-char-p char)))
60 59
 
61
-
62 60
 (defun extract-date-timezone (date-str)
63 61
   (declare (optimize (debug 3)))
64 62
   (let ((tz-inited nil))
... ...
@@ -179,6 +177,15 @@
179 177
          (attr "href" link)))
180 178
     xml-root))
181 179
 
180
+(defmethod content-el ((entity rss-item))
181
+  (fw.lu:let-each (:be *)
182
+    (doc entity)
183
+    ($1 (inline *)
184
+	(combine "> content::encoded"
185
+		 "> description"))
186
+    (or (elt (car *) 0)
187
+	(elt (cadr *) 0))))
188
+
182 189
 (defmethod make-item (xml-dom (type (eql :rss)))
183 190
   (let* ((*lquery-master-document* xml-dom)
184 191
          (item-title ($1 "> title" (text)))