Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-153
[gnus] / lisp / html2text.el
1 ;;; html2text.el --- a simple html to plain text converter
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 ;; Author: Joakim Hove <hove@phys.ntnu.no>
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; These functions provide a simple way to wash/clean html infected
26 ;; mails.  Definitely do not work in all cases, but some improvement
27 ;; in readability is generally obtained. Formatting is only done in
28 ;; the buffer, so the next time you enter the article it will be
29 ;; "re-htmlized".
30 ;;
31 ;; The main function is "html2text"
32
33 ;;; Code:
34
35 ;;
36 ;; <Global variables>
37 ;;
38
39 (eval-when-compile
40   (require 'cl))
41
42 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
43
44 (defvar html2text-replace-list
45   '(("&nbsp;" . " ") ("&gt;" . ">") ("&lt;" . "<") ("&quot;" . "\"")
46     ("&amp;" . "&") ("&apos;" . "'"))
47   "The map of entity to text.
48
49 This is an alist were each element is a dotted pair consisting of an
50 old string, and a replacement string. This replacement is done by the
51 function \"html2text-substitute\" which basically performs a
52 replace-string operation for every element in the list. This is
53 completely verbatim - without any use of REGEXP.")
54
55 (defvar html2text-remove-tag-list
56   '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta")
57   "A list of removable tags.
58
59 This is a list of tags which should be removed, without any
60 formatting.  Observe that if you the tags in the list are presented
61 *without* any \"<\" or \">\". All occurences of a tag appearing in
62 this list are removed, irrespective of whether it is a closing or
63 opening tag, or if the tag has additional attributes. The actual
64 deletion is done by the function \"html2text-remove-tags\".
65
66 For instance the text:
67
68 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
69
70 will be reduced to:
71
72 \"Here comes something big.\"
73
74 If this list contains the element \"font\".")
75
76 (defvar html2text-format-tag-list
77   '(("b"          . html2text-clean-bold)
78     ("u"          . html2text-clean-underline)
79     ("i"          . html2text-clean-italic)
80     ("blockquote" . html2text-clean-blockquote)
81     ("a"          . html2text-clean-anchor)
82     ("ul"         . html2text-clean-ul)
83     ("ol"         . html2text-clean-ol)
84     ("dl"         . html2text-clean-dl)
85     ("center"     . html2text-clean-center))
86   "An alist of tags and processing functions.
87
88 This is an alist where each dotted pair consists of a tag, and then
89 the name of a function to be called when this tag is found. The
90 function is called with the arguments p1, p2, p3 and p4. These are
91 demontrated below:
92
93 \"<b> This is bold text </b>\"
94  ^   ^                 ^    ^
95  |   |                 |    |
96 p1  p2                p3   p4
97
98 Then the called function will typically format the text somewhat and
99 remove the tags.")
100
101 (defvar html2text-remove-tag-list2  '("li" "dt" "dd" "meta")
102   "Another list of removable tags.
103
104 This is a list of tags which are removed similarly to the list
105 `html2text-remove-tag-list' - but these tags are retained for the
106 formatting, and then moved afterward.")
107
108 ;;
109 ;; </Global variables>
110 ;;
111
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114
115 ;;
116 ;; <Utility functions>
117 ;;
118
119
120 (defun html2text-replace-string (from-string to-string p1 p2)
121   (goto-char p1)
122   (let ((delta (- (string-width to-string) (string-width from-string)))
123         (change 0))
124     (while (search-forward from-string p2 t)
125       (replace-match to-string)
126       (setq change (+ change delta)))
127     change))
128
129 ;;
130 ;; </Utility functions>
131 ;;
132
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135
136 ;;
137 ;; <Functions related to attributes> i.e. <font size=+3>
138 ;;
139
140 (defun html2text-attr-value (attr-list attr)
141   (nth 1 (assoc attr attr-list)))
142
143 (defun html2text-get-attr (p1 p2)
144   (save-restriction
145     (narrow-to-region p1 p2)
146     (let (result)
147       (goto-char (point-min))
148       (while (not (eobp))
149         (when (re-search-forward "[^= ]+" nil t)
150           (push
151            (list
152             (match-string 0)
153             (when (looking-at " *= *")
154               (goto-char (match-end 0))
155               (buffer-substring 
156                (point)
157                (goto-char (or (ignore-errors (scan-sexps (point) 1))
158                               (point-max))))))
159            result)))
160       result)))
161 ;;
162 ;; </Functions related to attributes>
163 ;;
164
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167
168 ;;
169 ;; <Functions to be called to format a tag-pair>
170 ;;
171 (defun html2text-clean-list-items (p1 p2 list-type)
172   (goto-char p1)
173   (let ((item-nr 0)
174         (items   0))
175     (while (search-forward "<li>" p2 t)
176       (setq items (1+ items)))
177     (goto-char p1)
178     (while (< item-nr items)
179       (setq item-nr (1+ item-nr))
180       (search-forward "<li>" (point-max) t)
181       (cond
182        ((string= list-type "ul") (insert " o "))
183        ((string= list-type "ol") (insert (format " %s: " item-nr)))
184        (t (insert " x "))))))
185
186 (defun html2text-clean-dtdd (p1 p2)
187   (goto-char p1)
188   (let ((items   0)
189         (item-nr 0))
190     (while (search-forward "<dt>" p2 t)
191       (setq items (1+ items)))
192     (goto-char p1)
193     (while (< item-nr items)
194       (setq item-nr (1+ item-nr))
195       (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
196       (when (match-string 1)
197         (delete-region (point) (- (point) (string-width (match-string 1)))))
198       (let ((def-p1 (point))
199             (def-p2 0))
200         (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
201         (if (match-string 1)
202             (progn
203               (let* ((mw1 (string-width (match-string 1)))
204                      (mw2 (string-width (match-string 2)))
205                      (mw  (+ mw1 mw2)))
206                 (goto-char (- (point) mw))
207                 (delete-region (point) (+ (point) mw1))
208                 (setq def-p2 (point))))
209           (setq def-p2 (- (point) (string-width (match-string 2)))))
210         (put-text-property def-p1 def-p2 'face 'bold)))))
211
212 (defun html2text-delete-tags (p1 p2 p3 p4)
213   (delete-region p1 p2)
214   (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
215
216 (defun html2text-delete-single-tag (p1 p2)
217   (delete-region p1 p2))
218
219 (defun html2text-clean-hr (p1 p2)
220   (html2text-delete-single-tag p1 p2)
221   (goto-char p1)
222   (newline 1)
223   (insert (make-string fill-column ?-)))
224
225 (defun html2text-clean-ul (p1 p2 p3 p4)
226   (html2text-delete-tags p1 p2 p3 p4)
227   (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul"))
228
229 (defun html2text-clean-ol (p1 p2 p3 p4)
230   (html2text-delete-tags p1 p2 p3 p4)
231   (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol"))
232
233 (defun html2text-clean-dl (p1 p2 p3 p4)
234   (html2text-delete-tags p1 p2 p3 p4)
235   (html2text-clean-dtdd p1 (- p3 (- p1 p2))))
236
237 (defun html2text-clean-center (p1 p2 p3 p4)
238   (html2text-delete-tags p1 p2 p3 p4)
239   (center-region p1 (- p3 (- p2 p1))))
240
241 (defun html2text-clean-bold (p1 p2 p3 p4)
242   (put-text-property p2 p3 'face 'bold)
243   (html2text-delete-tags p1 p2 p3 p4))
244
245 (defun html2text-clean-title (p1 p2 p3 p4)
246   (put-text-property p2 p3 'face 'bold)
247   (html2text-delete-tags p1 p2 p3 p4))
248
249 (defun html2text-clean-underline (p1 p2 p3 p4)
250   (put-text-property p2 p3 'face 'underline)
251   (html2text-delete-tags p1 p2 p3 p4))
252
253 (defun html2text-clean-italic (p1 p2 p3 p4)
254   (put-text-property p2 p3 'face 'italic)
255   (html2text-delete-tags p1 p2 p3 p4))
256
257 (defun html2text-clean-font (p1 p2 p3 p4)
258   (html2text-delete-tags p1 p2 p3 p4))
259
260 (defun html2text-clean-blockquote (p1 p2 p3 p4)
261   (html2text-delete-tags p1 p2 p3 p4))
262
263 (defun html2text-clean-anchor (p1 p2 p3 p4)
264   ;; If someone can explain how to make the URL clickable I will
265   ;; surely improve upon this.
266   (let ((href (html2text-attr-value (html2text-get-attr p1 p2) "href")))
267     (delete-region p1 p4)
268     (when href
269       (goto-char p1)
270       (insert (substring href 1 -1 ))
271       (put-text-property p1 (point) 'face 'bold))))
272
273 ;;
274 ;; </Functions to be called to format a tag-pair>
275 ;;
276
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279
280 ;;
281 ;; <Functions to be called to fix up paragraphs>
282 ;;
283
284 (defun html2text-fix-paragraph (p1 p2)
285   (goto-char p1)
286   (let ((has-br-line)
287         (refill-start)
288         (refill-stop))
289     (when (re-search-forward "<br>$" p2 t)
290       (setq has-br-line t))
291     (when has-br-line
292         (goto-char p1)
293         (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
294           (beginning-of-line)
295           (setq refill-start (point))
296           (goto-char p2)
297           (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
298           (next-line 1)
299           (end-of-line)
300           ;; refill-stop should ideally be adjusted to
301           ;; accomodate the "<br>" strings which are removed
302           ;; between refill-start and refill-stop.  Can simply
303           ;; be returned from my-replace-string
304           (setq refill-stop (+ (point)
305                                (html2text-replace-string
306                                 "<br>" ""
307                                 refill-start (point))))
308           ;; (message "Point = %s  refill-stop = %s" (point) refill-stop)
309           ;; (sleep-for 4)
310           (fill-region refill-start refill-stop))))
311   (html2text-replace-string "<br>" "" p1 p2))
312
313 ;;
314 ;; This one is interactive ...
315 ;;
316 (defun html2text-fix-paragraphs ()
317   "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
318 fashion, quite close to pure guess-work. It does work in some cases though."
319   (interactive)
320   (goto-char (point-min))
321   (replace-regexp "^<br>$" "")
322   ;; Removing lonely <br> on a single line, if they are left intact we
323   ;; dont have any paragraphs at all.
324   (goto-char (point-min))
325   (while (not (eobp))
326     (let ((p1 (point)))
327       (forward-paragraph 1)
328       ;;(message "Kaller fix med p1=%s  p2=%s " p1 (1- (point))) (sleep-for 5)
329       (html2text-fix-paragraph p1 (1- (point)))
330       (goto-char p1)
331       (when (not (eobp))
332         (forward-paragraph 1)))))
333
334 ;;
335 ;; </Functions to be called to fix up paragraphs>
336 ;;
337
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340
341 ;;
342 ;; <Interactive functions>
343 ;;
344
345 (defun html2text-remove-tags (tag-list)
346   "Removes the tags listed in the list \"html2text-remove-tag-list\".
347 See the documentation for that variable."
348   (interactive)
349   (dolist (tag tag-list)
350     (goto-char (point-min))
351     (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
352       (delete-region (match-beginning 0) (match-end 0)))))
353
354 (defun html2text-format-tags ()
355   "See the variable \"html2text-format-tag-list\" for documentation"
356   (interactive)
357   (dolist (tag-and-function html2text-format-tag-list)
358     (let ((tag      (car tag-and-function))
359           (function (cdr tag-and-function)))
360       (goto-char (point-min))
361       (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
362                                 (point-max) t)
363         (let ((p1)
364               (p2 (point))
365               (p3) (p4)
366               (attr (match-string 0)))
367           (search-backward "<" (point-min) t)
368           (setq p1 (point))
369           (search-forward (format "</%s>" tag) (point-max) t)
370           (setq p4 (point))
371           (search-backward "</" (point-min) t)
372           (setq p3 (point))
373           (funcall function p1 p2 p3 p4)
374           (goto-char p1)
375           )))))
376
377 (defun html2text-substitute ()
378   "See the variable \"html2text-replace-list\" for documentation"
379   (interactive)
380   (dolist (e html2text-replace-list)
381     (goto-char (point-min))
382     (let ((old-string (car e))
383           (new-string (cdr e)))
384       (html2text-replace-string old-string new-string (point-min) (point-max)))
385     ))
386
387 (defun html2text-format-single-elements ()
388   ""
389   (interactive)
390   (dolist (tag-and-function html2text-format-single-element-list)
391     (let ((tag      (car tag-and-function))
392           (function (cdr tag-and-function)))
393       (goto-char (point-min))
394       (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
395                                 (point-max) t)
396         (let ((p1)
397               (p2 (point)))
398           (search-backward "<" (point-min) t)
399           (setq p1 (point))
400           (funcall function p1 p2))))))
401
402 ;;
403 ;; Main function
404 ;;
405
406 ;;;###autoload
407 (defun html2text ()
408   "Convert HTML to plain text in the current buffer."
409   (interactive)
410   (save-excursion
411     (let ((case-fold-search t)
412           (buffer-read-only))
413       (html2text-remove-tags html2text-remove-tag-list)
414       (html2text-format-tags)
415       (html2text-remove-tags html2text-remove-tag-list2)
416       (html2text-substitute)
417       (html2text-format-single-elements)
418       (html2text-fix-paragraphs))))
419
420 ;;
421 ;; </Interactive functions>
422 ;;
423
424 ;;; arch-tag: e9e57b79-35d4-4de1-a647-e7e01fe56d1e
425 ;;; html2text.el ends here