1 ;;; newst-reader.el --- Generic RSS reader functions.
3 ;; Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 ;; Author: Ulf Jasper <ulf.jasper@web.de>
6 ;; Filename: newst-reader.el
7 ;; URL: http://www.nongnu.org/newsticker
10 ;; ======================================================================
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; ======================================================================
32 ;; ======================================================================
35 (require 'newst-backend
)
37 ;; ======================================================================
39 ;; ======================================================================
40 (defun newsticker--set-customvar-formatting (symbol value
)
41 "Set newsticker-variable SYMBOL value to VALUE.
42 Calls all actions which are necessary in order to make the new
44 (if (or (not (boundp symbol
))
45 (equal (symbol-value symbol
) value
))
47 ;; something must have changed
49 (when (fboundp 'newsticker--forget-preformatted
)
50 (newsticker--forget-preformatted))))
52 ;; ======================================================================
54 (defgroup newsticker-reader nil
55 "Settings for the feed reader."
58 (defcustom newsticker-frontend
60 "Newsticker frontend for reading news.
61 This must be one of the functions `newsticker-plainview' or
62 `newsticker-treeview'."
63 :type
'(choice :tag
"Frontend"
64 (const :tag
"Single buffer (plainview)" newsticker-plainview
)
65 (const :tag
"Tree view (treeview)" newsticker-treeview
))
66 :group
'newsticker-reader
)
68 ;; image related things
69 (defcustom newsticker-download-logos
71 "If non-nil newsticker downloads logo images of subscribed feeds."
73 :group
'newsticker-reader
)
75 (defcustom newsticker-enable-logo-manipulations
77 "If non-nil newsticker manipulates logo images.
78 This enables the following image properties: heuristic mask for all
79 logos, and laplace-conversion for images without new items."
81 :group
'newsticker-reader
)
83 (defcustom newsticker-justification
85 "How to fill item descriptions.
86 If non-nil newsticker calls `fill-region' to wrap long lines in
87 item descriptions. However, if an item description contains HTML
88 text and `newsticker-html-renderer' is non-nil, filling is not
90 :type
'(choice :tag
"Justification"
91 (const :tag
"No filling" nil
)
92 (const :tag
"Left" left
)
93 (const :tag
"Right" right
)
94 (const :tag
"Center" center
)
95 (const :tag
"Full" full
))
96 :set
'newsticker--set-customvar-formatting
97 :group
'newsticker-reader
)
99 (defcustom newsticker-use-full-width
101 "Decides whether to use the full window width when filling.
102 If non-nil newsticker sets `fill-column' so that the whole
103 window is used when filling. See also `newsticker-justification'."
105 :set
'newsticker--set-customvar-formatting
106 :group
'newsticker-reader
)
108 (defcustom newsticker-html-renderer
109 (if (fboundp 'libxml-parse-html-region
)
111 "Function for rendering HTML contents.
112 If non-nil, newsticker.el will call this function whenever it
113 finds HTML-like tags in item descriptions.
114 Possible functions include `shr-render-region', `w3m-region', `w3-region', and
115 `newsticker-htmlr-render'.
116 Newsticker automatically loads the respective package w3m, w3, or
117 htmlr if this option is set."
118 :type
'(choice :tag
"Function"
119 (const :tag
"None" nil
)
120 (const :tag
"SHR" shr-render-region
)
121 (const :tag
"w3" w3-region
)
122 (const :tag
"w3m" w3m-region
)
123 (const :tag
"htmlr" newsticker-htmlr-render
))
124 :set
'newsticker--set-customvar-formatting
125 :group
'newsticker-reader
)
127 (defcustom newsticker-date-format
129 "Format for the date part in item and feed lines.
130 See `format-time-string' for a list of valid specifiers."
132 :set
'newsticker--set-customvar-formatting
133 :group
'newsticker-reader
)
135 (defgroup newsticker-faces nil
136 "Settings for the faces of the feed reader."
137 :group
'newsticker-reader
)
139 (defface newsticker-feed-face
140 '((default :weight bold
:height
1.2)
141 (((class color
) (background dark
)) :foreground
"white")
142 (((class color
) (background light
)) :foreground
"black"))
143 "Face for news feeds."
144 :group
'newsticker-faces
)
146 (defface newsticker-extra-face
147 '((default :slant italic
:height
0.8)
148 (((class color
) (background dark
)) :foreground
"gray50")
149 (((class color
) (background light
)) :foreground
"gray50"))
150 "Face for newsticker dates."
151 :group
'newsticker-faces
)
153 (defface newsticker-enclosure-face
154 '((default :weight bold
)
155 (((class color
) (background dark
)) :background
"orange")
156 (((class color
) (background light
)) :background
"orange"))
157 "Face for enclosed elements."
158 :group
'newsticker-faces
)
160 ;; ======================================================================
161 ;;; Utility functions
162 ;; ======================================================================
163 (defun newsticker--insert-enclosure (item keymap
)
164 "Insert enclosure element of a news ITEM into the current buffer.
165 KEYMAP will be applied."
166 (let ((enclosure (newsticker--enclosure item
))
169 (let ((url (cdr (assoc 'url enclosure
)))
170 (length (string-to-number (or (cdr (assoc 'length enclosure
))
172 (type (cdr (assoc 'type enclosure
))))
173 (cond ((> length
1048576)
174 (insert (format "Enclosed file (%s, %1.2f MBytes)" type
175 (/ length
1048576))))
177 (insert (format "Enclosed file (%s, %1.2f KBytes)" type
180 (insert (format "Enclosed file (%s, %1.2f Bytes)" type
183 (insert (format "Enclosed file (%s, unknown size)" type
))))
184 (add-text-properties beg
(point)
185 (list 'mouse-face
'highlight
188 "mouse-2: visit (%s)" url
)
194 (defun newsticker--print-extra-elements (item keymap
&optional htmlish
)
195 "Insert extra-elements of ITEM in a pretty form into the current buffer.
196 KEYMAP is applied. If HTMLISH is non-nil then HTML-markup is used
198 (let ((ignored-elements '(items link title description content
199 content
:encoded encoded
201 dc
:date date entry item guid pubDate
204 (left-column-width 1))
205 (if htmlish
(insert "<ul>"))
206 (mapc (lambda (extra-element)
207 (when (listp extra-element
) ;; take care of broken xml
209 (unless (memq (car extra-element
) ignored-elements
)
210 (setq left-column-width
(max left-column-width
212 (car extra-element
))))))))
213 (newsticker--extra item
))
214 (mapc (lambda (extra-element)
215 (when (listp extra-element
) ;; take care of broken xml
217 (unless (memq (car extra-element
) ignored-elements
)
218 (newsticker--do-print-extra-element extra-element
222 (newsticker--extra item
))
223 (if htmlish
(insert "</ul>"))))
225 (defun newsticker--do-print-extra-element (extra-element width keymap htmlish
)
226 "Actually print an EXTRA-ELEMENT using the given WIDTH.
227 KEYMAP is applied. If HTMLISH is non-nil then HTML-markup is used
229 (let ((name (symbol-name (car extra-element
))))
231 (insert (format "<li>%s: " name
))
232 (insert (format "%s: " name
))
233 (insert (make-string (- width
(length name
)) ?
))))
234 (let (;;(attributes (cadr extra-element)) ;FIXME!!!!
235 (contents (cddr extra-element
)))
236 (cond ((listp contents
)
239 (string-match "^http://.*" i
))
241 (insert i
" ") ; avoid self-reference from the
245 (list 'mouse-face
'highlight
248 (format "mouse-2: visit (%s)" i
)
250 (insert (format "%s" i
))))
253 (insert (format "%s" contents
))))
258 (defun newsticker--image-read (feed-name-symbol disabled
&optional max-height
)
259 "Read the cached image for FEED-NAME-SYMBOL from disk.
260 If DISABLED is non-nil the image will be converted to a disabled look
261 \(unless `newsticker-enable-logo-manipulations' is not t\).
262 Optional argument MAX-HEIGHT specifies the maximal image height.
264 (let ((image-name (concat (newsticker--images-dir)
265 (symbol-name feed-name-symbol
))))
266 (when (file-exists-p image-name
)
267 (condition-case error-data
270 (and (fboundp 'imagemagick-types
)
274 :conversion
(and newsticker-enable-logo-manipulations
277 :mask
(and newsticker-enable-logo-manipulations
280 :max-height max-height
)
282 (message "Error: cannot create image for %s: %s"
283 feed-name-symbol error-data
))))))
285 (defun newsticker--icon-read (feed-name-symbol)
286 "Read the cached icon for FEED-NAME-SYMBOL from disk.
289 (when (file-exists-p (newsticker--icons-dir))
290 (dolist (file (directory-files (newsticker--icons-dir) t
291 (concat (symbol-name feed-name-symbol
) "\\..*")))
292 (condition-case error-data
293 (throw 'icon
(create-image
294 file
(and (fboundp 'imagemagick-types
)
302 (message "Error: cannot create icon for %s: %s"
303 feed-name-symbol error-data
)))))
304 ;; Fallback: default icon.
305 (find-image '((:type png
:file
"newsticker/rss-feed.png" :ascent center
)))))
307 ;; the functions we need for retrieval and display
309 (defun newsticker-show-news ()
310 "Start reading news. You may want to bind this to a key."
312 (newsticker-start t
) ;; will start only if not running
313 ;; Load the html rendering packages
314 (if newsticker-html-renderer
315 (cond ((eq newsticker-html-renderer
'w3m-region
)
317 ((eq newsticker-html-renderer
'w3-region
)
319 ((eq newsticker-html-renderer
'newsticker-htmlr-render
)
321 (funcall newsticker-frontend
))
323 ;; ======================================================================
325 ;; ======================================================================
327 (defun newsticker-browse-url-item (feed item
)
328 "Convert FEED ITEM to html and call `browse-url' on result."
330 (let ((t-file (make-temp-file "newsticker")))
331 (with-temp-file t-file
332 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>
333 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
334 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
335 <html xmlns=\"http://www.w3.org/1999/xhtml\">
337 (insert "<h1>" feed
": " (newsticker--title item
) "</h1>")
338 (insert (format-time-string newsticker-date-format
339 (newsticker--time item
)))
341 (insert (or (newsticker--desc item
) "[No Description]"))
342 (when (newsticker--enclosure item
)
343 (insert "<br/><hr/><i>")
344 (newsticker--insert-enclosure item nil
)
346 (when (newsticker--extra item
)
347 (insert "<br/><hr/><tt>")
348 (newsticker--print-extra-elements item nil
)
350 (insert "</body></html>"))
351 (browse-url t-file
)))
353 (provide 'newst-reader
)
355 ;;; newst-reader.el ends here