* html2text.el: New file from Joakim Hove <hove@phys.ntnu.no>.
[gnus] / lisp / html2text.el
1 ;; These functions provide a simple way to wash/clean html infected mails.
2 ;; Definitely do not work in all cases, but some improvement in readability 
3 ;; is generally obtained. Formatting is only done in the buffer, so the next
4 ;; time you enter the article it will be "re-htmlized".
5 ;;
6 ;; The main function is "html2text"
7 ;; 
8 ;; Joakim Hove <hove@phys.ntnu.no>
9
10
11
12 ;;
13 ;; <Global variables>
14 ;;
15
16 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
17
18 (defvar html2text-replace-list 
19   '(("&nbsp;" . " ") ("&gt;" . ">") ("&lt;" . "<") ("&quot;" . "\"")) 
20   "This is a alist(?) were each element is a dotted pair consisting of
21 an old string, and a replacement string. This replacement is done by
22 the function \"html2text-substitute\" which basically performs a 
23 replace-string operation for every element in the list. This is completely
24 verbatim - without any use of REGEXP")
25
26 (defvar html2text-remove-tag-list    
27   '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta")
28   "This is a list of tags which should be removed, without any formatting. 
29 Observe that if you the tags in the list are presented *without* any 
30 \"<\" or \">\". All occurences of a tag appearing in this list are removed,
31 irrespective of whether it is a closing or opening tag, or if the tag
32 has additional attributes. The actual deletion is done by the function
33 \"html2text-remove-tags\".
34
35 For instance the text:
36
37 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
38
39 will be reduced to:
40
41 \"Here comes something big.\"
42
43 If this list contains the element \"font\".")
44
45 (defvar html2text-format-tag-list    
46   '(("b"          . html2text-clean-bold) 
47     ("u"          . html2text-clean-underline) 
48     ("i"          . html2text-clean-italic) 
49     ("blockquote" . html2text-clean-blockquote)
50     ("a"          . html2text-clean-anchor) 
51     ("ul"         . html2text-clean-ul)
52     ("ol"         . html2text-clean-ol)
53     ("dl"         . html2text-clean-dl)
54     ("center"     . html2text-clean-center))
55   "This is an alist where each dotted pair consists of a tag, and then the name
56 of a function to be called when this tag is found. The function is called with
57 the arguments p1, p2, p3 and p4. These are demontrated below:
58
59 \"<b> This is bold text </b>\"
60  ^   ^                 ^    ^  
61  |   |                 |    |
62 p1  p2                p3   p4
63
64 Then the called function will typically format the text somewhat and remove the tags.")
65
66
67
68 (defvar html2text-remove-tag-list2  '("li" "dt" "dd" "meta")
69   "This is a list of tags which are removed similarly to the list
70 \"html2text-remove-tag-list\" - but these tags are retained for the
71 formatting, and then moved afterwards.")
72
73
74 ;;
75 ;; </Global variables>
76 ;;
77
78
79 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
82
83 ;;
84 ;; <Utility functions>
85 ;;
86
87
88 (defun html2text-buffer-head ()
89   (if (string= mode-name "Article")
90       (beginning-of-buffer)
91     (beginning-of-buffer)
92     )
93   )
94
95
96 (defun html2text-replace-string (from-string to-string p1 p2)
97   (goto-char p1)
98   (let ((delta (- (string-width to-string) (string-width from-string)))
99         (change 0))
100     (while (search-forward from-string p2 t)
101       (replace-match to-string)
102       (setq change (+ change delta))
103       )
104     change
105     )
106   )
107
108 ;;
109 ;; </Utility functions>
110 ;;
111
112
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115
116
117 ;;
118 ;; <Functions related to attributes> i.e. <font size=+3>
119 ;;
120
121 (defun html2text-attr-value (attr-list attr)
122   (nth 1 (assoc attr attr-list))
123 )
124
125
126
127 (defun html2text-get-attr (p1 p2 tag)
128   (goto-char p1)
129   (re-search-forward " +[^ ]" p2 t)
130   (let* ((attr-string (buffer-substring-no-properties (1- (point)) (1- p2)))
131          (tmp-list (split-string attr-string))
132          (attr-list)
133          (counter 0)
134          (prev (car tmp-list))
135          (this (nth 1 tmp-list))
136          (next (nth 2 tmp-list))
137          (index 1))
138     
139     (cond
140      ;; size=3
141      ((string-match "[^ ]=[^ ]" prev)
142       (let ((attr  (nth 0 (split-string prev "=")))
143             (value (nth 1 (split-string prev "="))))
144         (setq attr-list (cons (list attr value) attr-list))
145         )
146       )
147      ;; size= 3
148      ((string-match "[^ ]=\\'" prev)
149       (setq attr-list (cons (list (substring prev 0 -1) this) attr-list))
150       )
151      )
152
153     (while (< index (length tmp-list))
154       (cond 
155        ;; size=3
156        ((string-match "[^ ]=[^ ]" this)
157         (let ((attr  (nth 0 (split-string this "=")))
158               (value (nth 1 (split-string this "="))))
159           (setq attr-list (cons (list attr value) attr-list))
160           )
161         )
162        ;; size =3
163        ((string-match "\\`=[^ ]" this)
164         (setq attr-list (cons (list prev (substring this 1)) attr-list)))
165
166        ;; size= 3
167        ((string-match "[^ ]=\\'" this)
168         (setq attr-list (cons (list (substring this 0 -1) next) attr-list))
169         )
170   
171        ;; size = 3
172        ((string= "=" this)
173         (setq attr-list (cons (list prev next) attr-list))
174        )
175       )
176       (setq index (1+ index))
177       (setq prev this)
178       (setq this next)
179       (setq next (nth (1+ index) tmp-list))
180       )
181
182
183     ;;
184     ;; Tags with no accompanying "=" i.e. value=nil
185     ;;
186     (setq prev (car tmp-list))
187     (setq this (nth 1 tmp-list))
188     (setq next (nth 2 tmp-list))
189     (setq index 1)
190
191     (if (not (string-match "=" prev))
192         (progn
193           (if (not (string= (substring this 0 1) "="))
194               (setq attr-list (cons (list prev nil) attr-list))
195             )
196           )
197       )
198
199     (while (< index (1- (length tmp-list)))
200       (if (not (string-match "=" this))
201           (if (not (or (string= (substring next 0 1) "=") (string= (substring prev -1) "=")))
202               (setq attr-list (cons (list this nil) attr-list))
203             )
204         )
205       (setq index (1+ index))
206       (setq prev this)
207       (setq this next)
208       (setq next (nth (1+ index) tmp-list))
209       )
210
211     (if this 
212         (progn
213           (if (not (string-match "=" this))
214               (progn
215                 (if (not (string= (substring prev -1) "="))
216                     (setq attr-list (cons (list this nil) attr-list))
217                   )
218                 )
219             )
220           )
221       )
222     attr-list  ;; return - value
223     )
224 )
225
226 ;;
227 ;; </Functions related to attributes>
228 ;;
229
230
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233
234
235 ;;
236 ;; <Functions to be called to format a tag-pair>
237 ;;
238 (defun html2text-clean-list-items (p1 p2 list-type)
239   (goto-char p1)
240   (let ((item-nr 0)
241         (items   0))
242     (while (re-search-forward "<li>" p2 t)
243       (setq items (1+ items)))
244     (goto-char p1)
245     (while (< item-nr items)
246       (setq item-nr (1+ item-nr))
247       (re-search-forward "<li>" (point-max) t)
248       (cond
249        ((string= list-type "ul") (insert " o "))
250        ((string= list-type "ol") (insert (format " %s: " item-nr)))
251        (insert " x "))
252       )
253     )
254   )
255
256 (defun html2text-clean-dtdd (p1 p2)
257   (goto-char p1)
258   (let ((items   0)
259         (item-nr 0))
260     (while (re-search-forward "<dt>" 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 "<dt>\\([ ]*\\)" (point-max) t)
266       (if (match-string 1)
267           (kill-region (point) (- (point) (string-width (match-string 1))))
268         )
269       (let ((def-p1 (point))
270             (def-p2 0))
271         (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
272         (if (match-string 1)
273             (progn
274               (let* ((mw1 (string-width (match-string 1)))
275                      (mw2 (string-width (match-string 2)))
276                      (mw  (+ mw1 mw2)))
277                 (goto-char (- (point) mw))
278                 (kill-region (point) (+ (point) mw1))
279                 (setq def-p2 (point))
280                 )
281               )
282           (setq def-p2 (- (point) (string-width (match-string 2)))))
283         (put-text-property def-p1 def-p2 'face 'bold)
284         )
285       )
286     )
287   )
288       
289
290
291
292
293 (defun html2text-delete-tags (p1 p2 p3 p4)
294   (kill-region p1 p2)
295   (kill-region (- p3 (- p2 p1)) (- p4 (- p2 p1)))
296 )
297
298 (defun html2text-delete-single-tag (p1 p2)
299   (kill-region p1 p2)
300   )
301
302 (defun html2text-clean-hr (p1 p2)
303   (html2text-delete-single-tag p1 p2)
304   (goto-char p1)
305   (newline 1)
306   (insert (make-string fill-column ?-))
307   )
308
309
310
311 (defun html2text-clean-ul (p1 p2 p3 p4)
312   (html2text-delete-tags p1 p2 p3 p4)
313   (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul")
314   )
315
316 (defun html2text-clean-ol (p1 p2 p3 p4)
317   (html2text-delete-tags p1 p2 p3 p4)
318   (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol")
319   )
320
321 (defun html2text-clean-dl (p1 p2 p3 p4)
322   (html2text-delete-tags p1 p2 p3 p4)
323   (html2text-clean-dtdd p1 (- p3 (- p1 p2)))
324 )
325
326 (defun html2text-clean-center (p1 p2 p3 p4)
327   (html2text-delete-tags p1 p2 p3 p4)
328   (center-region p1 (- p3 (- p2 p1)))
329   )
330   
331
332 (defun html2text-clean-bold (p1 p2 p3 p4)
333   (put-text-property p2 p3 'face 'bold)
334   (html2text-delete-tags p1 p2 p3 p4)
335 )
336
337 (defun html2text-clean-title (p1 p2 p3 p4)
338   (put-text-property p2 p3 'face 'bold)
339   (html2text-delete-tags p1 p2 p3 p4)
340 )
341
342 (defun html2text-clean-underline (p1 p2 p3 p4)
343   (put-text-property p2 p3 'face 'underline)
344   (html2text-delete-tags p1 p2 p3 p4)
345 )
346
347 (defun html2text-clean-italic (p1 p2 p3 p4)
348   (put-text-property p2 p3 'face 'italic)
349   (html2text-delete-tags p1 p2 p3 p4)
350 )
351
352 (defun html2text-clean-font (p1 p2 p3 p4)
353   (html2text-delete-tags p1 p2 p3 p4)
354   )
355
356 (defun html2text-clean-blockquote (p1 p2 p3 p4)
357   (html2text-delete-tags p1 p2 p3 p4)
358 )
359
360
361 (defun html2text-clean-anchor (p1 p2 p3 p4)
362   ;; If someone can explain how to make the URL clickable 
363   ;; I will surely improve upon this.
364   (let* ((attr-list (html2text-get-attr p1 p2 "a"))
365          (href (html2text-attr-value attr-list "href")))
366     (kill-region p1 p4)
367     (goto-char p1)
368     (insert (substring href 1 -1 ))
369     (put-text-property p1 (point) 'face 'bold)
370     )
371   )
372
373 ;;
374 ;; </Functions to be called to format a tag-pair>
375 ;;
376
377
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380
381
382 ;;
383 ;; <Functions to be called to fix up paragraphs>
384 ;;
385
386 (defun html2text-fix-paragraph (p1 p2)
387   (goto-char p1)
388   (let ((has-br-line)
389         (refill-start)
390         (refill-stop))
391     (if (re-search-forward "<br>$" p2 t)
392         (setq has-br-line t)
393       )
394     (if has-br-line
395         (progn
396           (goto-char p1)
397           (if (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
398               (progn
399                 (beginning-of-line)
400                 (setq refill-start (point))
401                 (goto-char p2)
402                 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
403                 (next-line 1)
404                 (end-of-line)
405                 ;; refill-stop should ideally be adjusted to accomodate the "<br>" strings which are removed
406                 ;; between refill-start and refill-stop.
407                 ;; Can simply be returned from my-replace-string
408                 (setq refill-stop (+ (point) (html2text-replace-string "<br>" "" refill-start (point))))
409                 ;;(message "Point = %s  refill-stop = %s" (point) refill-stop) (sleep-for 4)
410                 (fill-region refill-start refill-stop)
411                 )
412             )
413           )
414       )
415     )
416   (html2text-replace-string "<br>" "" p1 p2)
417   )
418               
419
420 ;;
421 ;; This one is interactive ...
422 ;;
423 (defun html2text-fix-paragraphs ()
424   "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
425 fashion, quite close to pure guess-work. It does work in some cases though."
426   (interactive)
427   (html2text-buffer-head)
428   (replace-regexp "^<br>$" "") 
429 ;; Removing lonely <br> on a single line, if they are left
430 ;; intact we dont have any paragraphs at all.
431   (html2text-buffer-head)
432   (while (< (point) (point-max))
433     (let ((p1 (point)))
434       (forward-paragraph 1)
435       ;;(message "Kaller fix med p1=%s  p2=%s " p1 (1- (point))) (sleep-for 5)
436       (html2text-fix-paragraph p1 (1- (point)))
437       (goto-char p1)
438       (if (< (point) (point-max)) 
439           (forward-paragraph 1))
440       )
441     )
442   )
443
444 ;;
445 ;; </Functions to be called to fix up paragraphs>
446 ;;
447
448
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451
452
453 ;;
454 ;; <Interactive functions>
455 ;;
456
457 (defun html2text-remove-tags (tag-list)
458   "Removes the tags listed in the list \"html2text-remove-tag-list\".
459 See the documentation for that variable."
460   (interactive)
461   (dolist (tag tag-list)
462     (html2text-buffer-head)
463     (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
464       (let ((p1 (point)))
465         (search-backward "<")
466         (kill-region (point) p1)
467         )
468       )
469     )
470   )
471
472
473
474 (defun html2text-format-tags ()
475   "See the variable \"html2text-format-tag-list\" for documentation"
476   (interactive)
477   (dolist (tag-and-function html2text-format-tag-list)
478     (let ((tag      (car tag-and-function))
479           (function (cdr tag-and-function)))
480       (html2text-buffer-head)
481       (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) (point-max) t)
482         (let ((p1)
483               (p2 (point))
484               (p3) (p4) 
485               (attr (match-string 1)))
486           (search-backward "<" (point-min) t)
487           (setq p1 (point))
488           (re-search-forward (format "</%s>" tag) (point-max) t)
489           (setq p4 (point))
490           (search-backward "</" (point-min) t)
491           (setq p3 (point))
492           (funcall function p1 p2 p3 p4)
493           (goto-char p1)
494           )
495         )
496       )
497     )
498   )
499
500 (defun html2text-substitute ()
501   "See the variable \"html2text-replace-list\" for documentation"
502   (interactive)
503   (dolist (e html2text-replace-list)
504     (html2text-buffer-head)
505     (let ((old-string (car e))
506           (new-string (cdr e)))
507       (html2text-replace-string old-string new-string (point-min) (point-max))
508       )
509     )
510   )
511
512 (defun html2text-format-single-elements ()
513   ""
514   (interactive)
515   (dolist (tag-and-function html2text-format-single-element-list)
516     (let ((tag      (car tag-and-function))
517           (function (cdr tag-and-function)))
518       (html2text-buffer-head)
519       (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) (point-max) t)
520         (let ((p1)
521               (p2 (point)))
522           (search-backward "<" (point-min) t)
523           (setq p1 (point))
524           (funcall function p1 p2)
525           )
526         )
527       )
528     )
529   )
530
531 ;;
532 ;; Main function
533 ;;        
534 (defun html2text ()
535   (interactive)
536   (save-excursion
537     (let ((org-case-fold-search case-fold-search))
538       (setq case-fold-search 't)
539       (if (string= mode-name "Article")
540           (toggle-read-only -1)
541         )
542       
543       (html2text-remove-tags html2text-remove-tag-list)
544       (html2text-format-tags)
545       (html2text-remove-tags html2text-remove-tag-list2)
546       (html2text-substitute)
547       (html2text-format-single-elements)
548       (html2text-fix-paragraphs)
549
550       
551       (if (string= mode-name "Article")
552           (toggle-read-only  1)
553         )
554       (setq case-fold-search org-case-fold-search)
555
556       )
557     )
558   )
559
560 ;;
561 ;; </Interactive functions>
562 ;; 
563
564
565
566
567
568
569