1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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)
15 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Note: You need to have `w3' installed for some functions to work.
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
44 (autoload 'w3-parse-buffer "w3-parse")
48 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
49 "Where nnweb will save its files.")
51 (defvoo nnweb-type 'google
52 "What search engine type is being used.
53 Valid types include `google', `dejanews', `dejanewsold', `reference',
56 (defvar nnweb-type-definition
59 (id . "http://groups.google.com/groups?selm=%s&output=gplain")
60 (reference . identity)
61 (map . nnweb-google-create-mapping)
62 (search . nnweb-google-search)
63 (address . "http://groups.google.com/groups")
64 (identifier . nnweb-google-identity))
65 (dejanews ;; alias of google
66 ;;(article . nnweb-google-wash-article)
67 ;;(id . "http://groups.google.com/groups?as_umsgid=%s")
69 (id . "http://groups.google.com/groups?selm=%s&output=gplain")
70 ;;(reference . nnweb-google-reference)
71 (reference . identity)
72 (map . nnweb-google-create-mapping)
73 (search . nnweb-google-search)
74 (address . "http://groups.google.com/groups")
75 (identifier . nnweb-google-identity))
77 (article . nnweb-reference-wash-article)
78 (map . nnweb-reference-create-mapping)
79 (search . nnweb-reference-search)
80 (address . "http://www.reference.com/cgi-bin/pn/go")
81 (identifier . identity))
83 (article . nnweb-altavista-wash-article)
84 (map . nnweb-altavista-create-mapping)
85 (search . nnweb-altavista-search)
86 (address . "http://www.altavista.digital.com/cgi-bin/query")
87 (id . "/cgi-bin/news?id@%s")
88 (identifier . identity)))
89 "Type-definition alist.")
91 (defvoo nnweb-search nil
92 "Search string to feed to DejaNews.")
94 (defvoo nnweb-max-hits 999
95 "Maximum number of hits to display.")
97 (defvoo nnweb-ephemeral-p nil
98 "Whether this nnweb server is ephemeral.")
100 ;;; Internal variables
102 (defvoo nnweb-articles nil)
103 (defvoo nnweb-buffer nil)
104 (defvoo nnweb-group-alist nil)
105 (defvoo nnweb-group nil)
106 (defvoo nnweb-hashtb nil)
108 ;;; Interface functions
110 (nnoo-define-basics nnweb)
112 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
113 (nnweb-possibly-change-server group server)
115 (set-buffer nntp-server-buffer)
117 (let (article header)
118 (mm-with-unibyte-current-buffer
119 (while (setq article (pop articles))
120 (when (setq header (cadr (assq article nnweb-articles)))
121 (nnheader-insert-nov header))))
124 (deffoo nnweb-request-scan (&optional group server)
125 (nnweb-possibly-change-server group server)
126 (if nnweb-ephemeral-p
127 (setq nnweb-hashtb (gnus-make-hashtable 4095)))
128 (funcall (nnweb-definition 'map))
129 (unless nnweb-ephemeral-p
131 (nnweb-write-overview group)))
133 (deffoo nnweb-request-group (group &optional server dont-check)
134 (nnweb-possibly-change-server nil server)
136 (not (equal group nnweb-group))
137 (not nnweb-ephemeral-p))
138 (setq nnweb-group group
140 (let ((info (assoc group nnweb-group-alist)))
142 (setq nnweb-type (nth 2 info))
143 (setq nnweb-search (nth 3 info))
145 (nnweb-read-overview group)))))
147 ((not nnweb-articles)
148 (nnheader-report 'nnweb "No matching articles"))
150 (let ((active (if nnweb-ephemeral-p
151 (cons (caar nnweb-articles)
152 (caar (last nnweb-articles)))
153 (cadr (assoc group nnweb-group-alist)))))
154 (nnheader-report 'nnweb "Opened group %s" group)
156 "211 %d %d %d %s\n" (length nnweb-articles)
157 (car active) (cdr active) group)))))
159 (deffoo nnweb-close-group (group &optional server)
160 (nnweb-possibly-change-server group server)
161 (when (gnus-buffer-live-p nnweb-buffer)
163 (set-buffer nnweb-buffer)
164 (set-buffer-modified-p nil)
165 (kill-buffer nnweb-buffer)))
168 (deffoo nnweb-request-article (article &optional group server buffer)
169 (nnweb-possibly-change-server group server)
171 (set-buffer (or buffer nntp-server-buffer))
172 (let* ((header (cadr (assq article nnweb-articles)))
173 (url (and header (mail-header-xref header))))
175 (mm-with-unibyte-current-buffer
176 (mm-url-insert url)))
177 (and (stringp article)
178 (nnweb-definition 'id t)
179 (let ((fetch (nnweb-definition 'id))
181 (when (string-match "^<\\(.*\\)>$" article)
182 (setq art (match-string 1 article)))
183 (when (and fetch art)
184 (setq url (format fetch art))
185 (mm-with-unibyte-current-buffer
187 (if (nnweb-definition 'reference t)
189 (funcall (nnweb-definition
190 'reference) article)))))))
191 (unless nnheader-callback-function
192 (funcall (nnweb-definition 'article)))
193 (nnheader-report 'nnweb "Fetched article %s" article)
194 (cons group (and (numberp article) article))))))
196 (deffoo nnweb-close-server (&optional server)
197 (when (and (nnweb-server-opened server)
198 (gnus-buffer-live-p nnweb-buffer))
200 (set-buffer nnweb-buffer)
201 (set-buffer-modified-p nil)
202 (kill-buffer nnweb-buffer)))
203 (nnoo-close-server 'nnweb server))
205 (deffoo nnweb-request-list (&optional server)
206 (nnweb-possibly-change-server nil server)
208 (set-buffer nntp-server-buffer)
209 (nnmail-generate-active nnweb-group-alist)
212 (deffoo nnweb-request-update-info (group info &optional server)
213 (nnweb-possibly-change-server group server))
215 (deffoo nnweb-asynchronous-p ()
218 (deffoo nnweb-request-create-group (group &optional server args)
219 (nnweb-possibly-change-server nil server)
220 (nnweb-request-delete-group group)
221 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
225 (deffoo nnweb-request-delete-group (group &optional force server)
226 (nnweb-possibly-change-server group server)
227 (gnus-pull group nnweb-group-alist t)
229 (gnus-delete-file (nnweb-overview-file group))
232 (nnoo-define-skeleton nnweb)
234 ;;; Internal functions
236 (defun nnweb-read-overview (group)
237 "Read the overview of GROUP and build the map."
238 (when (file-exists-p (nnweb-overview-file group))
239 (mm-with-unibyte-buffer
240 (nnheader-insert-file-contents (nnweb-overview-file group))
241 (goto-char (point-min))
244 (setq header (nnheader-parse-nov))
246 (push (list (mail-header-number header)
247 header (mail-header-xref header))
249 (nnweb-set-hashtb header (car nnweb-articles)))))))
251 (defun nnweb-write-overview (group)
252 "Write the overview file for GROUP."
253 (with-temp-file (nnweb-overview-file group)
254 (let ((articles nnweb-articles))
256 (nnheader-insert-nov (cadr (pop articles)))))))
258 (defun nnweb-set-hashtb (header data)
259 (gnus-sethash (nnweb-identifier (mail-header-xref header))
262 (defun nnweb-get-hashtb (url)
263 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
265 (defun nnweb-identifier (ident)
266 (funcall (nnweb-definition 'identifier) ident))
268 (defun nnweb-overview-file (group)
269 "Return the name of the overview file of GROUP."
270 (nnheader-concat nnweb-directory group ".overview"))
272 (defun nnweb-write-active ()
273 "Save the active file."
274 (gnus-make-directory nnweb-directory)
275 (with-temp-file (nnheader-concat nnweb-directory "active")
276 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
278 (defun nnweb-read-active ()
279 "Read the active file."
280 (load (nnheader-concat nnweb-directory "active") t t t))
282 (defun nnweb-definition (type &optional noerror)
283 "Return the definition of TYPE."
284 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
287 (error "Undefined definition %s" type))
290 (defun nnweb-possibly-change-server (&optional group server)
293 (unless (nnweb-server-opened server)
294 (nnweb-open-server server)))
295 (unless nnweb-group-alist
298 (setq nnweb-hashtb (gnus-make-hashtable 4095)))
300 (when (and (not nnweb-ephemeral-p)
301 (equal group nnweb-group))
302 (nnweb-request-group group nil t))))
304 (defun nnweb-init (server)
305 "Initialize buffers and such."
306 (unless (gnus-buffer-live-p nnweb-buffer)
310 (nnheader-set-temp-buffer
311 (format " *nnweb %s %s %s*"
312 nnweb-type nnweb-search server))
313 (current-buffer))))))
315 ;; (defun nnweb-fetch-url (url)
318 ;; (if (not nnheader-callback-function)
321 ;; (mm-enable-multibyte)
322 ;; (let ((coding-system-for-read 'binary)
323 ;; (coding-system-for-write 'binary)
324 ;; (input-coding-system 'binary)
325 ;; (output-coding-system 'binary)
326 ;; (default-process-coding-system 'binary))
327 ;; (nnweb-insert url))
328 ;; (setq buf (buffer-string)))
332 ;; (nnweb-url-retrieve-asynch
333 ;; url 'nnweb-callback (current-buffer) nnheader-callback-function)
336 ;; (defun nnweb-callback (buffer callback)
337 ;; (when (gnus-buffer-live-p url-working-buffer)
339 ;; (set-buffer url-working-buffer)
340 ;; (funcall (nnweb-definition 'article))
341 ;; (nnweb-decode-entities)
342 ;; (set-buffer buffer)
343 ;; (goto-char (point-max))
344 ;; (insert-buffer-substring url-working-buffer))
345 ;; (funcall callback t)
346 ;; (gnus-kill-buffer url-working-buffer)))
348 ;; (defun nnweb-url-retrieve-asynch (url callback &rest data)
349 ;; (let ((url-request-method "GET")
350 ;; (old-asynch url-be-asynchronous)
351 ;; (url-request-data nil)
352 ;; (url-request-extra-headers nil)
353 ;; (url-working-buffer (generate-new-buffer-name " *nnweb*")))
354 ;; (setq-default url-be-asynchronous t)
356 ;; (set-buffer (get-buffer-create url-working-buffer))
357 ;; (setq url-current-callback-data data
358 ;; url-be-asynchronous t
359 ;; url-current-callback-func callback)
360 ;; (url-retrieve url nil))
361 ;; (setq-default url-be-asynchronous old-asynch)))
363 ;; (if (fboundp 'url-retrieve-synchronously)
364 ;; (defun nnweb-url-retrieve-asynch (url callback &rest data)
365 ;; (url-retrieve url callback data)))
368 ;;; DejaNews functions.
371 (defun nnweb-dejanews-create-mapping ()
372 "Perform the search and create an number-to-url alist."
374 (set-buffer nnweb-buffer)
376 (when (funcall (nnweb-definition 'search) nnweb-search)
380 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
383 map url parse a table group text)
385 ;; Go through all the article hits on this page.
386 (goto-char (point-min))
387 (setq parse (w3-parse-buffer (current-buffer))
388 table (nth 1 (nnweb-parse-find-all 'table parse)))
389 (dolist (row (nth 2 (car (nth 2 table))))
390 (setq a (nnweb-parse-find 'a row)
391 url (cdr (assq 'href (nth 1 a)))
392 text (nreverse (nnweb-text row)))
394 (setq subject (nth 4 text)
398 (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
399 (setq date (format "%s %s 00:00:00 %s"
400 (car (rassq (string-to-number
401 (match-string 2 date))
403 (match-string 3 date)
404 (match-string 1 date)))
405 (setq date "Jan 1 00:00:00 0000"))
407 (setq url (concat url "&fmt=text"))
408 (when (string-match "&context=[^&]+" url)
409 (setq url (replace-match "" t t url)))
410 (unless (nnweb-get-hashtb url)
414 (make-full-mail-header
415 (cdr active) (concat subject " (" group ")") from date
416 (concat "<" (nnweb-identifier url) "@dejanews>")
419 (nnweb-set-hashtb (cadar map) (car map)))))
420 ;; See whether there is a "Get next 20 hits" button here.
421 (goto-char (point-min))
422 (if (or (not (re-search-forward
423 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
424 (>= i nnweb-max-hits))
427 (setq more (match-string 1))
429 (mm-url-insert more)))
430 ;; Return the articles in the right order.
432 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
434 (defun nnweb-dejanews-search (search)
437 (nnweb-definition 'address)
439 (mm-url-encode-www-form-urlencoded
441 ("svcclass" . "dnyr")
443 ("defaultOp" . "AND")
445 ("OP" . "dnquery.xp")
449 ("format" . "verbose2")
450 ("showsort" . "date")
452 ("ageweight" . "1")))))
455 ;; (defun nnweb-dejanewsold-search (search)
457 ;; (nnweb-definition 'address)
458 ;; `(("query" . ,search)
459 ;; ("defaultOp" . "AND")
460 ;; ("svcclass" . "dnold")
461 ;; ("maxhits" . "100")
462 ;; ("format" . "verbose2")
463 ;; ("threaded" . "0")
464 ;; ("showsort" . "date")
466 ;; ("ageweight" . "1")))
469 (defun nnweb-dejanews-identity (url)
470 "Return an unique identifier based on URL."
471 (if (string-match "AN=\\([0-9]+\\)" url)
479 (defun nnweb-reference-create-mapping ()
480 "Perform the search and create an number-to-url alist."
482 (set-buffer nnweb-buffer)
484 (when (funcall (nnweb-definition 'search) nnweb-search)
488 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
490 Subject Score Date Newsgroups From Message-ID
493 ;; Go through all the article hits on this page.
494 (goto-char (point-min))
495 (search-forward "</pre><hr>" nil t)
496 (delete-region (point-min) (point))
497 (goto-char (point-min))
498 (while (re-search-forward "^ +[0-9]+\\." nil t)
501 (if (re-search-forward "^$" nil t)
504 (goto-char (point-min))
505 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
506 (setq url (match-string 1)))
507 (mm-url-remove-markup)
508 (goto-char (point-min))
509 (while (search-forward "\t" nil t)
511 (goto-char (point-min))
512 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
513 (set (intern (match-string 1)) (match-string 2)))
515 (search-forward "</pre>" nil t)
517 (unless (nnweb-get-hashtb url)
521 (make-full-mail-header
522 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
524 nil 0 (string-to-int Score) url))
526 (nnweb-set-hashtb (cadar map) (car map))))
528 ;; Return the articles in the right order.
530 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
532 (defun nnweb-reference-wash-article ()
533 (let ((case-fold-search t))
534 (goto-char (point-min))
535 (re-search-forward "^</center><hr>" nil t)
536 (delete-region (point-min) (point))
537 (search-forward "<pre>" nil t)
539 (let ((body (point-marker)))
540 (search-forward "</pre>" nil t)
541 (delete-region (point) (point-max))
542 (mm-url-remove-markup)
543 (goto-char (point-min))
544 (while (looking-at " *$")
546 (narrow-to-region (point-min) body)
547 (while (and (re-search-forward "^$" nil t)
550 (goto-char (point-min))
551 (while (looking-at "\\(^[^ ]+:\\) *")
552 (replace-match "\\1 " t)
554 (goto-char (point-min))
555 (when (re-search-forward "^References:" nil t)
557 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
560 (goto-char (point-min))
562 (unless (looking-at "References")
565 (goto-char (point-min))
566 (while (search-forward "," nil t)
567 (replace-match " " t t)))
569 (mm-url-decode-entities)
570 (set-marker body nil))))
572 (defun nnweb-reference-search (search)
575 (nnweb-definition 'address)
577 (mm-url-encode-www-form-urlencoded
578 `(("search" . "advanced")
579 ("querytext" . ,search)
584 ("organization" . "")
587 ("choice" . "Search")
588 ("startmonth" . "Jul")
590 ("startyear" . "1996")
595 ("verbosity" . "Verbose")
596 ("ranking" . "Relevance")
600 (setq buffer-file-name nil)
607 (defun nnweb-altavista-create-mapping ()
608 "Perform the search and create an number-to-url alist."
610 (set-buffer nnweb-buffer)
613 (when (funcall (nnweb-definition 'search) nnweb-search part)
617 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
619 subject date from id group
622 ;; Go through all the article hits on this page.
623 (goto-char (point-min))
624 (search-forward "<dt>" nil t)
625 (delete-region (point-min) (match-beginning 0))
626 (goto-char (point-min))
627 (while (search-forward "<dt>" nil t)
628 (replace-match "\n<blubb>"))
629 (mm-url-decode-entities)
630 (goto-char (point-min))
631 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
633 (setq url (match-string 1)
634 subject (match-string 2)
635 date (match-string 3)
636 group (match-string 4)
637 id (concat "<" (match-string 5) ">")
638 from (match-string 6))
640 (unless (nnweb-get-hashtb url)
644 (make-full-mail-header
645 (cdr active) (concat "(" group ") " subject) from date
648 (nnweb-set-hashtb (cadar map) (car map))))
649 ;; See if we want more.
650 (when (or (not nnweb-articles)
651 (>= i nnweb-max-hits)
652 (not (funcall (nnweb-definition 'search)
653 nnweb-search (incf part))))
655 ;; Return the articles in the right order.
657 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
659 (defun nnweb-altavista-wash-article ()
660 (goto-char (point-min))
661 (let ((case-fold-search t))
662 (when (re-search-forward "^<strong>" nil t)
663 (delete-region (point-min) (match-beginning 0)))
664 (goto-char (point-min))
665 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
666 (replace-match "\\1: \\2" t)
668 (when (re-search-backward "^References:" nil t)
669 (narrow-to-region (point) (progn (forward-line 1) (point)))
670 (goto-char (point-min))
671 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
672 (replace-match "<\\1> " t)))
674 (mm-url-remove-markup)
675 (mm-url-decode-entities)))
677 (defun nnweb-altavista-search (search &optional part)
680 (nnweb-definition 'address)
682 (mm-url-encode-www-form-urlencoded
685 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
691 (setq buffer-file-name nil)
695 ;;; Deja bought by google.com
698 (defun nnweb-google-wash-article ()
699 (let ((case-fold-search t) url)
700 (goto-char (point-min))
701 (re-search-forward "^<pre>" nil t)
702 (narrow-to-region (point-min) (point))
703 (search-backward "<table " nil t 2)
704 (delete-region (point-min) (point))
705 (if (re-search-forward "Search Result [0-9]+" nil t)
707 (if (re-search-forward "View complete thread ([0-9]+ articles?)" nil t)
709 (goto-char (point-min))
710 (while (search-forward "<br>" nil t)
711 (replace-match "\n"))
712 (mm-url-remove-markup)
713 (goto-char (point-min))
714 (while (re-search-forward "^[ \t]*\n" nil t)
716 (goto-char (point-max))
719 (narrow-to-region (point) (point-max))
720 (search-forward "</pre>" nil t)
721 (delete-region (point) (point-max))
722 (mm-url-remove-markup)
725 (defun nnweb-google-parse-1 (&optional Message-ID)
728 (active (cadr (assoc nnweb-group nnweb-group-alist)))
729 Subject Score Date Newsgroups From
732 (push (list nnweb-group (setq active (cons 1 0))
733 nnweb-type nnweb-search)
735 ;; Go through all the article hits on this page.
736 (goto-char (point-min))
737 (while (re-search-forward
738 "a href=/groups\\(\\?[^ \">]*selm=\\([^ &\">]+\\)\\)" nil t)
739 (setq mid (match-string 2)
741 "http://groups.google.com/groups?selm=%s&output=gplain" mid))
742 (narrow-to-region (search-forward ">" nil t)
743 (search-forward "</a>" nil t))
744 (mm-url-remove-markup)
745 (mm-url-decode-entities)
746 (setq Subject (buffer-string))
747 (goto-char (point-max))
750 (when (looking-at "<br><font[^>]+>")
751 (goto-char (match-end 0)))
752 (if (not (looking-at "<a[^>]+>"))
753 (skip-chars-forward " \t")
754 (narrow-to-region (point)
755 (search-forward "</a>" nil t))
756 (mm-url-remove-markup)
757 (mm-url-decode-entities)
758 (setq Newsgroups (buffer-string))
759 (goto-char (point-max))
761 (skip-chars-forward "- \t"))
763 "\\([0-9]+\\)[/ ]\\([A-Za-z]+\\)[/ ]\\([0-9]+\\)[ \t]*by[ \t]*\\([^<]*\\) - <a")
764 (setq From (match-string 4)
765 Date (format "%s %s 00:00:00 %s"
766 (match-string 2) (match-string 1)
770 (unless (nnweb-get-hashtb url)
774 (make-full-mail-header
775 (cdr active) (if Newsgroups
776 (concat "(" Newsgroups ") " Subject)
778 From Date (or Message-ID mid)
781 (nnweb-set-hashtb (cadar map) (car map))))
784 (defun nnweb-google-reference (id)
785 (let ((map (nnweb-google-parse-1 id)) header)
787 (nconc nnweb-articles map))
788 (when (setq header (cadar map))
789 (mm-with-unibyte-current-buffer
790 (mm-url-insert (mail-header-xref header)))
793 (defun nnweb-google-create-mapping ()
794 "Perform the search and create an number-to-url alist."
796 (set-buffer nnweb-buffer)
798 (when (funcall (nnweb-definition 'search) nnweb-search)
802 (nconc nnweb-articles (nnweb-google-parse-1)))
803 ;; FIXME: There is more.
805 ;; Return the articles in the right order.
807 (sort nnweb-articles 'car-less-than-car))))))
809 (defun nnweb-google-search (search)
812 (nnweb-definition 'address)
814 (mm-url-encode-www-form-urlencoded
821 ("sites" . "groups")))))
824 (defun nnweb-google-identity (url)
825 "Return an unique identifier based on URL."
826 (if (string-match "selm=\\([^ &>]+\\)" url)
831 ;;; General web/w3 interface utility functions
834 (defun nnweb-insert-html (parse)
835 "Insert HTML based on a w3 parse tree."
837 (insert (nnheader-string-as-multibyte parse))
838 (insert "<" (symbol-name (car parse)) " ")
841 (concat (symbol-name (car param)) "="
843 (if (consp (cdr param))
849 (mapcar 'nnweb-insert-html (nth 2 parse))
850 (insert "</" (symbol-name (car parse)) ">\n")))
852 (defun nnweb-parse-find (type parse &optional maxdepth)
853 "Find the element of TYPE in PARSE."
855 (nnweb-parse-find-1 type parse maxdepth)))
857 (defun nnweb-parse-find-1 (type contents maxdepth)
858 (when (or (null maxdepth)
859 (not (zerop maxdepth)))
860 (when (consp contents)
861 (when (eq (car contents) type)
862 (throw 'found contents))
863 (when (listp (cdr contents))
864 (dolist (element contents)
865 (when (consp element)
866 (nnweb-parse-find-1 type element
867 (and maxdepth (1- maxdepth)))))))))
869 (defun nnweb-parse-find-all (type parse)
870 "Find all elements of TYPE in PARSE."
872 (nnweb-parse-find-all-1 type parse)))
874 (defun nnweb-parse-find-all-1 (type contents)
876 (when (consp contents)
877 (if (eq (car contents) type)
878 (push contents result)
879 (when (listp (cdr contents))
880 (dolist (element contents)
881 (when (consp element)
883 (nconc result (nnweb-parse-find-all-1 type element))))))))
887 (defun nnweb-text (parse)
888 "Return a list of text contents in PARSE."
889 (let ((nnweb-text nil))
891 (nreverse nnweb-text)))
893 (defun nnweb-text-1 (contents)
894 (dolist (element contents)
895 (if (stringp element)
896 (push element nnweb-text)
897 (when (and (consp element)
898 (listp (cdr element)))
899 (nnweb-text-1 element)))))
903 ;;; nnweb.el ends here