]> code.delx.au - gnu-emacs/blob - lisp/gnus/html2text.el
31d1869c695213db4c2eec9a6f7170b4b187a5b2
[gnu-emacs] / lisp / gnus / 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 )
128 change
129 )
130 )
131
132 ;;
133 ;; </Utility functions>
134 ;;
135
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138
139 ;;
140 ;; <Functions related to attributes> i.e. <font size=+3>
141 ;;
142
143 (defun html2text-attr-value (attr-list attr)
144 (nth 1 (assoc attr attr-list))
145 )
146
147 (defun html2text-get-attr (p1 p2 tag)
148 (goto-char p1)
149 (re-search-forward " +[^ ]" p2 t)
150 (let* ((attr-string (buffer-substring-no-properties (1- (point)) (1- p2)))
151 (tmp-list (split-string attr-string))
152 (attr-list)
153 (counter 0)
154 (prev (car tmp-list))
155 (this (nth 1 tmp-list))
156 (next (nth 2 tmp-list))
157 (index 1))
158
159 (cond
160 ;; size=3
161 ((string-match "[^ ]=[^ ]" prev)
162 (let ((attr (nth 0 (split-string prev "=")))
163 (value (nth 1 (split-string prev "="))))
164 (setq attr-list (cons (list attr value) attr-list))
165 )
166 )
167 ;; size= 3
168 ((string-match "[^ ]=\\'" prev)
169 (setq attr-list (cons (list (substring prev 0 -1) this) attr-list))
170 )
171 )
172
173 (while (< index (length tmp-list))
174 (cond
175 ;; size=3
176 ((string-match "[^ ]=[^ ]" this)
177 (let ((attr (nth 0 (split-string this "=")))
178 (value (nth 1 (split-string this "="))))
179 (setq attr-list (cons (list attr value) attr-list))
180 )
181 )
182 ;; size =3
183 ((string-match "\\`=[^ ]" this)
184 (setq attr-list (cons (list prev (substring this 1)) attr-list)))
185
186 ;; size= 3
187 ((string-match "[^ ]=\\'" this)
188 (setq attr-list (cons (list (substring this 0 -1) next) attr-list))
189 )
190
191 ;; size = 3
192 ((string= "=" this)
193 (setq attr-list (cons (list prev next) attr-list))
194 )
195 )
196 (setq index (1+ index))
197 (setq prev this)
198 (setq this next)
199 (setq next (nth (1+ index) tmp-list))
200 )
201
202 ;;
203 ;; Tags with no accompanying "=" i.e. value=nil
204 ;;
205 (setq prev (car tmp-list))
206 (setq this (nth 1 tmp-list))
207 (setq next (nth 2 tmp-list))
208 (setq index 1)
209
210 (if (not (string-match "=" prev))
211 (progn
212 (if (not (string= (substring this 0 1) "="))
213 (setq attr-list (cons (list prev nil) attr-list))
214 )
215 )
216 )
217
218 (while (< index (1- (length tmp-list)))
219 (if (not (string-match "=" this))
220 (if (not (or (string= (substring next 0 1) "=")
221 (string= (substring prev -1) "=")))
222 (setq attr-list (cons (list this nil) attr-list))
223 )
224 )
225 (setq index (1+ index))
226 (setq prev this)
227 (setq this next)
228 (setq next (nth (1+ index) tmp-list))
229 )
230
231 (if this
232 (progn
233 (if (not (string-match "=" this))
234 (progn
235 (if (not (string= (substring prev -1) "="))
236 (setq attr-list (cons (list this nil) attr-list))
237 )
238 )
239 )
240 )
241 )
242 attr-list ;; return - value
243 )
244 )
245
246 ;;
247 ;; </Functions related to attributes>
248 ;;
249
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252
253 ;;
254 ;; <Functions to be called to format a tag-pair>
255 ;;
256 (defun html2text-clean-list-items (p1 p2 list-type)
257 (goto-char p1)
258 (let ((item-nr 0)
259 (items 0))
260 (while (re-search-forward "<li>" p2 t)
261 (setq items (1+ items)))
262 (goto-char p1)
263 (while (< item-nr items)
264 (setq item-nr (1+ item-nr))
265 (re-search-forward "<li>" (point-max) t)
266 (cond
267 ((string= list-type "ul") (insert " o "))
268 ((string= list-type "ol") (insert (format " %s: " item-nr)))
269 (t (insert " x ")))
270 )
271 )
272 )
273
274 (defun html2text-clean-dtdd (p1 p2)
275 (goto-char p1)
276 (let ((items 0)
277 (item-nr 0))
278 (while (re-search-forward "<dt>" p2 t)
279 (setq items (1+ items)))
280 (goto-char p1)
281 (while (< item-nr items)
282 (setq item-nr (1+ item-nr))
283 (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
284 (when (match-string 1)
285 (delete-region (point) (- (point) (string-width (match-string 1)))))
286 (let ((def-p1 (point))
287 (def-p2 0))
288 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
289 (if (match-string 1)
290 (progn
291 (let* ((mw1 (string-width (match-string 1)))
292 (mw2 (string-width (match-string 2)))
293 (mw (+ mw1 mw2)))
294 (goto-char (- (point) mw))
295 (delete-region (point) (+ (point) mw1))
296 (setq def-p2 (point))))
297 (setq def-p2 (- (point) (string-width (match-string 2)))))
298 (put-text-property def-p1 def-p2 'face 'bold)))))
299
300 (defun html2text-delete-tags (p1 p2 p3 p4)
301 (delete-region p1 p2)
302 (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
303
304 (defun html2text-delete-single-tag (p1 p2)
305 (delete-region p1 p2))
306
307 (defun html2text-clean-hr (p1 p2)
308 (html2text-delete-single-tag p1 p2)
309 (goto-char p1)
310 (newline 1)
311 (insert (make-string fill-column ?-))
312 )
313
314 (defun html2text-clean-ul (p1 p2 p3 p4)
315 (html2text-delete-tags p1 p2 p3 p4)
316 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul")
317 )
318
319 (defun html2text-clean-ol (p1 p2 p3 p4)
320 (html2text-delete-tags p1 p2 p3 p4)
321 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol")
322 )
323
324 (defun html2text-clean-dl (p1 p2 p3 p4)
325 (html2text-delete-tags p1 p2 p3 p4)
326 (html2text-clean-dtdd p1 (- p3 (- p1 p2)))
327 )
328
329 (defun html2text-clean-center (p1 p2 p3 p4)
330 (html2text-delete-tags p1 p2 p3 p4)
331 (center-region p1 (- p3 (- p2 p1)))
332 )
333
334 (defun html2text-clean-bold (p1 p2 p3 p4)
335 (put-text-property p2 p3 'face 'bold)
336 (html2text-delete-tags p1 p2 p3 p4)
337 )
338
339 (defun html2text-clean-title (p1 p2 p3 p4)
340 (put-text-property p2 p3 'face 'bold)
341 (html2text-delete-tags p1 p2 p3 p4)
342 )
343
344 (defun html2text-clean-underline (p1 p2 p3 p4)
345 (put-text-property p2 p3 'face 'underline)
346 (html2text-delete-tags p1 p2 p3 p4)
347 )
348
349 (defun html2text-clean-italic (p1 p2 p3 p4)
350 (put-text-property p2 p3 'face 'italic)
351 (html2text-delete-tags p1 p2 p3 p4)
352 )
353
354 (defun html2text-clean-font (p1 p2 p3 p4)
355 (html2text-delete-tags p1 p2 p3 p4)
356 )
357
358 (defun html2text-clean-blockquote (p1 p2 p3 p4)
359 (html2text-delete-tags p1 p2 p3 p4)
360 )
361
362 (defun html2text-clean-anchor (p1 p2 p3 p4)
363 ;; If someone can explain how to make the URL clickable I will
364 ;; surely improve upon this.
365 (let* ((attr-list (html2text-get-attr p1 p2 "a"))
366 (href (html2text-attr-value attr-list "href")))
367 (delete-region p1 p4)
368 (when href
369 (goto-char p1)
370 (insert (substring href 1 -1 ))
371 (put-text-property p1 (point) 'face 'bold))))
372
373 ;;
374 ;; </Functions to be called to format a tag-pair>
375 ;;
376
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379
380 ;;
381 ;; <Functions to be called to fix up paragraphs>
382 ;;
383
384 (defun html2text-fix-paragraph (p1 p2)
385 (goto-char p1)
386 (let ((has-br-line)
387 (refill-start)
388 (refill-stop))
389 (if (re-search-forward "<br>$" p2 t)
390 (setq has-br-line t)
391 )
392 (if has-br-line
393 (progn
394 (goto-char p1)
395 (if (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
396 (progn
397 (beginning-of-line)
398 (setq refill-start (point))
399 (goto-char p2)
400 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
401 (next-line 1)
402 (end-of-line)
403 ;; refill-stop should ideally be adjusted to
404 ;; accomodate the "<br>" strings which are removed
405 ;; between refill-start and refill-stop. Can simply
406 ;; be returned from my-replace-string
407 (setq refill-stop (+ (point)
408 (html2text-replace-string
409 "<br>" ""
410 refill-start (point))))
411 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
412 ;; (sleep-for 4)
413 (fill-region refill-start refill-stop)
414 )
415 )
416 )
417 )
418 )
419 (html2text-replace-string "<br>" "" p1 p2)
420 )
421
422 ;;
423 ;; This one is interactive ...
424 ;;
425 (defun html2text-fix-paragraphs ()
426 "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
427 fashion, quite close to pure guess-work. It does work in some cases though."
428 (interactive)
429 (goto-char (point-min))
430 (replace-regexp "^<br>$" "")
431 ;; Removing lonely <br> on a single line, if they are left intact we
432 ;; dont have any paragraphs at all.
433 (goto-char (point-min))
434 (while (not (eobp))
435 (let ((p1 (point)))
436 (forward-paragraph 1)
437 ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5)
438 (html2text-fix-paragraph p1 (1- (point)))
439 (goto-char p1)
440 (when (not (eobp))
441 (forward-paragraph 1)))))
442
443 ;;
444 ;; </Functions to be called to fix up paragraphs>
445 ;;
446
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449
450 ;;
451 ;; <Interactive functions>
452 ;;
453
454 (defun html2text-remove-tags (tag-list)
455 "Removes the tags listed in the list \"html2text-remove-tag-list\".
456 See the documentation for that variable."
457 (interactive)
458 (dolist (tag tag-list)
459 (goto-char (point-min))
460 (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
461 (delete-region (match-beginning 0) (match-end 0)))))
462
463 (defun html2text-format-tags ()
464 "See the variable \"html2text-format-tag-list\" for documentation"
465 (interactive)
466 (dolist (tag-and-function html2text-format-tag-list)
467 (let ((tag (car tag-and-function))
468 (function (cdr tag-and-function)))
469 (goto-char (point-min))
470 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
471 (point-max) t)
472 (let ((p1)
473 (p2 (point))
474 (p3) (p4)
475 (attr (match-string 1)))
476 (search-backward "<" (point-min) t)
477 (setq p1 (point))
478 (re-search-forward (format "</%s>" tag) (point-max) t)
479 (setq p4 (point))
480 (search-backward "</" (point-min) t)
481 (setq p3 (point))
482 (funcall function p1 p2 p3 p4)
483 (goto-char p1)
484 )
485 )
486 )
487 )
488 )
489
490 (defun html2text-substitute ()
491 "See the variable \"html2text-replace-list\" for documentation"
492 (interactive)
493 (dolist (e html2text-replace-list)
494 (goto-char (point-min))
495 (let ((old-string (car e))
496 (new-string (cdr e)))
497 (html2text-replace-string old-string new-string (point-min) (point-max))
498 )
499 )
500 )
501
502 (defun html2text-format-single-elements ()
503 ""
504 (interactive)
505 (dolist (tag-and-function html2text-format-single-element-list)
506 (let ((tag (car tag-and-function))
507 (function (cdr tag-and-function)))
508 (goto-char (point-min))
509 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
510 (point-max) t)
511 (let ((p1)
512 (p2 (point)))
513 (search-backward "<" (point-min) t)
514 (setq p1 (point))
515 (funcall function p1 p2)
516 )
517 )
518 )
519 )
520 )
521
522 ;;
523 ;; Main function
524 ;;
525
526 ;;;###autoload
527 (defun html2text ()
528 "Convert HTML to plain text in the current buffer."
529 (interactive)
530 (save-excursion
531 (let ((case-fold-search t)
532 (buffer-read-only))
533 (html2text-remove-tags html2text-remove-tag-list)
534 (html2text-format-tags)
535 (html2text-remove-tags html2text-remove-tag-list2)
536 (html2text-substitute)
537 (html2text-format-single-elements)
538 (html2text-fix-paragraphs))))
539
540 ;;
541 ;; </Interactive functions>
542 ;;
543
544 ;;; arch-tag: e9e57b79-35d4-4de1-a647-e7e01fe56d1e
545 ;;; html2text.el ends here