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".
6 ;; The main function is "html2text"
8 ;; Joakim Hove <hove@phys.ntnu.no>
16 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
18 (defvar html2text-replace-list
19 '((" " . " ") (">" . ">") ("<" . "<") (""" . "\""))
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")
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\".
35 For instance the text:
37 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
41 \"Here comes something big.\"
43 If this list contains the element \"font\".")
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:
59 \"<b> This is bold text </b>\"
64 Then the called function will typically format the text somewhat and remove the tags.")
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.")
75 ;; </Global variables>
79 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; <Utility functions>
88 (defun html2text-buffer-head ()
89 (if (string= mode-name "Article")
96 (defun html2text-replace-string (from-string to-string p1 p2)
98 (let ((delta (- (string-width to-string) (string-width from-string)))
100 (while (search-forward from-string p2 t)
101 (replace-match to-string)
102 (setq change (+ change delta))
109 ;; </Utility functions>
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;; <Functions related to attributes> i.e. <font size=+3>
121 (defun html2text-attr-value (attr-list attr)
122 (nth 1 (assoc attr attr-list))
127 (defun html2text-get-attr (p1 p2 tag)
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))
134 (prev (car tmp-list))
135 (this (nth 1 tmp-list))
136 (next (nth 2 tmp-list))
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))
148 ((string-match "[^ ]=\\'" prev)
149 (setq attr-list (cons (list (substring prev 0 -1) this) attr-list))
153 (while (< index (length tmp-list))
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))
163 ((string-match "\\`=[^ ]" this)
164 (setq attr-list (cons (list prev (substring this 1)) attr-list)))
167 ((string-match "[^ ]=\\'" this)
168 (setq attr-list (cons (list (substring this 0 -1) next) attr-list))
173 (setq attr-list (cons (list prev next) attr-list))
176 (setq index (1+ index))
179 (setq next (nth (1+ index) tmp-list))
184 ;; Tags with no accompanying "=" i.e. value=nil
186 (setq prev (car tmp-list))
187 (setq this (nth 1 tmp-list))
188 (setq next (nth 2 tmp-list))
191 (if (not (string-match "=" prev))
193 (if (not (string= (substring this 0 1) "="))
194 (setq attr-list (cons (list prev nil) attr-list))
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))
205 (setq index (1+ index))
208 (setq next (nth (1+ index) tmp-list))
213 (if (not (string-match "=" this))
215 (if (not (string= (substring prev -1) "="))
216 (setq attr-list (cons (list this nil) attr-list))
222 attr-list ;; return - value
227 ;; </Functions related to attributes>
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;; <Functions to be called to format a tag-pair>
238 (defun html2text-clean-list-items (p1 p2 list-type)
242 (while (re-search-forward "<li>" p2 t)
243 (setq items (1+ items)))
245 (while (< item-nr items)
246 (setq item-nr (1+ item-nr))
247 (re-search-forward "<li>" (point-max) t)
249 ((string= list-type "ul") (insert " o "))
250 ((string= list-type "ol") (insert (format " %s: " item-nr)))
256 (defun html2text-clean-dtdd (p1 p2)
260 (while (re-search-forward "<dt>" p2 t)
261 (setq items (1+ items)))
263 (while (< item-nr items)
264 (setq item-nr (1+ item-nr))
265 (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
267 (kill-region (point) (- (point) (string-width (match-string 1))))
269 (let ((def-p1 (point))
271 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
274 (let* ((mw1 (string-width (match-string 1)))
275 (mw2 (string-width (match-string 2)))
277 (goto-char (- (point) mw))
278 (kill-region (point) (+ (point) mw1))
279 (setq def-p2 (point))
282 (setq def-p2 (- (point) (string-width (match-string 2)))))
283 (put-text-property def-p1 def-p2 'face 'bold)
293 (defun html2text-delete-tags (p1 p2 p3 p4)
295 (kill-region (- p3 (- p2 p1)) (- p4 (- p2 p1)))
298 (defun html2text-delete-single-tag (p1 p2)
302 (defun html2text-clean-hr (p1 p2)
303 (html2text-delete-single-tag p1 p2)
306 (insert (make-string fill-column ?-))
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")
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")
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)))
326 (defun html2text-clean-center (p1 p2 p3 p4)
327 (html2text-delete-tags p1 p2 p3 p4)
328 (center-region p1 (- p3 (- p2 p1)))
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)
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)
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)
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)
352 (defun html2text-clean-font (p1 p2 p3 p4)
353 (html2text-delete-tags p1 p2 p3 p4)
356 (defun html2text-clean-blockquote (p1 p2 p3 p4)
357 (html2text-delete-tags p1 p2 p3 p4)
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")))
368 (insert (substring href 1 -1 ))
369 (put-text-property p1 (point) 'face 'bold)
374 ;; </Functions to be called to format a tag-pair>
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;; <Functions to be called to fix up paragraphs>
386 (defun html2text-fix-paragraph (p1 p2)
391 (if (re-search-forward "<br>$" p2 t)
397 (if (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
400 (setq refill-start (point))
402 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
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)
416 (html2text-replace-string "<br>" "" p1 p2)
421 ;; This one is interactive ...
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."
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))
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)))
438 (if (< (point) (point-max))
439 (forward-paragraph 1))
445 ;; </Functions to be called to fix up paragraphs>
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 ;; <Interactive functions>
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."
461 (dolist (tag tag-list)
462 (html2text-buffer-head)
463 (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
465 (search-backward "<")
466 (kill-region (point) p1)
474 (defun html2text-format-tags ()
475 "See the variable \"html2text-format-tag-list\" for documentation"
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)
485 (attr (match-string 1)))
486 (search-backward "<" (point-min) t)
488 (re-search-forward (format "</%s>" tag) (point-max) t)
490 (search-backward "</" (point-min) t)
492 (funcall function p1 p2 p3 p4)
500 (defun html2text-substitute ()
501 "See the variable \"html2text-replace-list\" for documentation"
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))
512 (defun html2text-format-single-elements ()
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)
522 (search-backward "<" (point-min) t)
524 (funcall function p1 p2)
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)
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)
551 (if (string= mode-name "Article")
554 (setq case-fold-search org-case-fold-search)
561 ;; </Interactive functions>