10-09-13
[emacs/old-mirror.git] / share / emacs / site-lisp / w3m / w3m-rss.el
blob7bac9d4b47d7f7d2b98dcf0287e648f349599969
1 ;;; w3m-rss.el --- RSS functions
3 ;; Copyright (C) 2004, 2005 TSUCHIYA Masatoshi <tsuchiya@namazu.org>
5 ;; Authors: TSUCHIYA Masatoshi <tsuchiya@namazu.org>
6 ;; Keywords: w3m, WWW, hypermedia
8 ;; This file is a part of emacs-w3m.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; w3m-rss.el provides RSS-related functions for emacs-w3m. For more
29 ;; detail about emacs-w3m, see:
31 ;; http://emacs-w3m.namazu.org/
34 ;;; Acknowledgment:
36 ;; I refered functions in `sb-rss.el' to implement this module.
37 ;; Thanks to Koichiro Ohba and NAKAJIMA Mikio.
40 ;;; Code:
42 (eval-when-compile (require 'cl))
43 (autoload 'xml-parse-region "xml")
45 (eval-and-compile
46 (autoload 'timezone-parse-date "timezone")
47 (autoload 'timezone-parse-time "timezone"))
49 (eval-when-compile
50 ;; Avoid warning for Emacs 19 and XEmacs.
51 (unless (fboundp 'match-string-no-properties)
52 (autoload 'match-string-no-properties "poe"))
53 ;; Avoid warning for Emacs 19.
54 (unless (fboundp 'split-string)
55 (autoload 'split-string "poe")))
57 (defun w3m-rss-parse-date-string (date)
58 "Decode DATE string written in the ISO 8601 format or the RFC822 style.
59 Return a list of numbers which conforms to the Emacs internal format.
60 Valid types in the ISO 8601 format include:
62 Year:
63 YYYY (eg 1997)
64 Year and month:
65 YYYY-MM (eg 1997-07)
66 Complete date:
67 YYYY-MM-DD (eg 1997-07-16)
68 Complete date plus hours and minutes:
69 YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
70 Complete date plus hours, minutes and seconds:
71 YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
72 Complete date plus hours, minutes, seconds and a decimal fraction
73 of a second
74 YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
76 where:
77 YYYY = four-digit year
78 MM = two-digit month (01=January, etc.)
79 DD = two-digit day of month (01 through 31)
80 hh = two digits of hour (00 through 23) (am/pm NOT allowed)
81 mm = two digits of minute (00 through 59)
82 ss = two digits of second (00 through 59)
83 s = one or more digits representing a decimal fraction of a second
84 TZD = time zone designator (Z or +hh:mm or -hh:mm)
86 For more detail about ISO 8601 date format, see
87 <URL:http://www.w3.org/TR/NOTE-datetime>.
89 In addition to the above, it also supports the date format in the
90 RFC822 style which RSS 2.0 allows. Valid types are the same as ones
91 which are supported by the `timezone-parse-date' function (which see)."
92 (cond ((not date) nil)
93 ((string-match " [0-9]+ " date)
94 (let* ((vector (timezone-parse-date date))
95 (year (string-to-number (aref vector 0)))
96 time)
97 (when (>= year 1970)
98 (setq time (timezone-parse-time (aref vector 3)))
99 (encode-time
100 (string-to-number (aref time 2))
101 (string-to-number (aref time 1))
102 (string-to-number (aref time 0))
103 (string-to-number (aref vector 2))
104 (string-to-number (aref vector 1))
105 year
106 (aref vector 4)))))
107 ((string-match "\
108 \\([0-9][0-9][0-9][0-9]\\)\\(?:-\\([0-9][0-9]\\)\\)?\\(?:-\\([0-9][0-9]\\)\\)?\
109 T?\\(?:\\([0-9][0-9]\\):\\([0-9][0-9]\\)\\(?::\\([.0-9]+\\)\\)?\\)?\
110 \\(?:\\([-+]\\)\\([0-9][0-9]\\):?\\([0-9][0-9]\\)\\|Z\\)?"
111 date)
112 (labels ((substr (n default)
113 (if (match-beginning n)
114 (string-to-number
115 (match-string-no-properties n date))
116 default)))
117 (encode-time
118 (substr 6 0) ;; seconds
119 (substr 5 0) ;; minitue
120 (substr 4 0) ;; hour
121 (substr 3 1) ;; day
122 (substr 2 1) ;; month
123 (substr 1 0) ;; year
124 (if (match-beginning 7)
125 (funcall (intern (match-string-no-properties 7 date))
127 (* 3600 (substr 8 0))
128 (* 60 (substr 9 0)))
129 0))))))
131 (defun w3m-rss-find-el (tag data)
132 "Find the all matching elements in the data. Careful with this on
133 large documents!"
134 (let (found)
135 (when (listp data)
136 (dolist (bit data)
137 (when (car-safe bit)
138 (when (equal tag (car bit))
139 (setq found (nconc found (list bit))))
140 (setq found
141 (nconc found
142 (w3m-rss-find-el
144 (if (and (listp (car-safe (caddr bit)))
145 (not (stringp (caddr bit))))
146 (caddr bit)
147 (cddr bit))))))))
148 found))
150 (defun w3m-rss-get-namespace-prefix (el uri)
151 "Given EL (containing a parsed element) and URI (containing a string
152 that gives the URI for which you want to retrieve the namespace
153 prefix), return the prefix.
154 See http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
155 for more RSS namespaces."
156 (let* ((prefix (car (rassoc uri (cadar el))))
157 (nslist (when prefix
158 (split-string (symbol-name prefix) ":")))
159 (ns (cond ((eq (length nslist) 1) ; no prefix given
161 ((eq (length nslist) 2) ; extract prefix
162 (cadr nslist)))))
163 (if (and ns (not (equal ns "")))
164 (concat ns ":")
165 ns)))
167 (provide 'w3m-rss)
169 ;;; w3m-rss.el ends here