1 ;;; html2text.el --- a simple html to plain text converter
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Joakim Hove <hove@phys.ntnu.no>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
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
31 ;; The main function is "html2text"
42 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
44 (defvar html2text-replace-list
45 '((" " . " ") (">" . ">") ("<" . "<") (""" . "\"")
46 ("&" . "&") ("'" . "'"))
47 "The map of entity to text.
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.")
55 (defvar html2text-remove-tag-list
56 '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta")
57 "A list of removable tags.
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\".
66 For instance the text:
68 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
72 \"Here comes something big.\"
74 If this list contains the element \"font\".")
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.
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
93 \"<b> This is bold text </b>\"
98 Then the called function will typically format the text somewhat and
101 (defvar html2text-remove-tag-list2 '("li" "dt" "dd" "meta")
102 "Another list of removable tags.
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.")
109 ;; </Global variables>
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 ;; <Utility functions>
119 (defun html2text-buffer-head ()
120 (if (string= mode-name "Article")
121 (beginning-of-buffer)
122 (beginning-of-buffer)))
124 (defun html2text-replace-string (from-string to-string p1 p2)
126 (let ((delta (- (string-width to-string) (string-width from-string)))
128 (while (search-forward from-string p2 t)
129 (replace-match to-string)
130 (setq change (+ change delta)))
134 ;; </Utility functions>
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; <Functions related to attributes> i.e. <font size=+3>
144 (defun html2text-attr-value (attr-list attr)
145 (nth 1 (assoc attr attr-list)))
147 (defun html2text-get-attr (p1 p2)
149 (narrow-to-region p1 p2)
151 (goto-char (point-min))
153 (when (re-search-forward "[^= ]+" nil t)
157 (when (looking-at " *= *")
158 (goto-char (match-end 0))
161 (goto-char (or (ignore-errors (scan-sexps (point) 1))
166 ;; </Functions related to attributes>
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ;; <Functions to be called to format a tag-pair>
175 (defun html2text-clean-list-items (p1 p2 list-type)
179 (while (search-forward "<li>" p2 t)
180 (setq items (1+ items)))
182 (while (< item-nr items)
183 (setq item-nr (1+ item-nr))
184 (search-forward "<li>" (point-max) t)
186 ((string= list-type "ul") (insert " o "))
187 ((string= list-type "ol") (insert (format " %s: " item-nr)))
188 (t (insert " x "))))))
190 (defun html2text-clean-dtdd (p1 p2)
194 (while (search-forward "<dt>" p2 t)
195 (setq items (1+ items)))
197 (while (< item-nr items)
198 (setq item-nr (1+ item-nr))
199 (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
200 (when (match-string 1)
201 (delete-region (point) (- (point) (string-width (match-string 1)))))
202 (let ((def-p1 (point))
204 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
207 (let* ((mw1 (string-width (match-string 1)))
208 (mw2 (string-width (match-string 2)))
210 (goto-char (- (point) mw))
211 (delete-region (point) (+ (point) mw1))
212 (setq def-p2 (point))))
213 (setq def-p2 (- (point) (string-width (match-string 2)))))
214 (put-text-property def-p1 def-p2 'face 'bold)))))
216 (defun html2text-delete-tags (p1 p2 p3 p4)
217 (delete-region p1 p2)
218 (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
220 (defun html2text-delete-single-tag (p1 p2)
221 (delete-region p1 p2))
223 (defun html2text-clean-hr (p1 p2)
224 (html2text-delete-single-tag p1 p2)
227 (insert (make-string fill-column ?-)))
229 (defun html2text-clean-ul (p1 p2 p3 p4)
230 (html2text-delete-tags p1 p2 p3 p4)
231 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul"))
233 (defun html2text-clean-ol (p1 p2 p3 p4)
234 (html2text-delete-tags p1 p2 p3 p4)
235 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol"))
237 (defun html2text-clean-dl (p1 p2 p3 p4)
238 (html2text-delete-tags p1 p2 p3 p4)
239 (html2text-clean-dtdd p1 (- p3 (- p1 p2))))
241 (defun html2text-clean-center (p1 p2 p3 p4)
242 (html2text-delete-tags p1 p2 p3 p4)
243 (center-region p1 (- p3 (- p2 p1))))
245 (defun html2text-clean-bold (p1 p2 p3 p4)
246 (put-text-property p2 p3 'face 'bold)
247 (html2text-delete-tags p1 p2 p3 p4))
249 (defun html2text-clean-title (p1 p2 p3 p4)
250 (put-text-property p2 p3 'face 'bold)
251 (html2text-delete-tags p1 p2 p3 p4))
253 (defun html2text-clean-underline (p1 p2 p3 p4)
254 (put-text-property p2 p3 'face 'underline)
255 (html2text-delete-tags p1 p2 p3 p4))
257 (defun html2text-clean-italic (p1 p2 p3 p4)
258 (put-text-property p2 p3 'face 'italic)
259 (html2text-delete-tags p1 p2 p3 p4))
261 (defun html2text-clean-font (p1 p2 p3 p4)
262 (html2text-delete-tags p1 p2 p3 p4))
264 (defun html2text-clean-blockquote (p1 p2 p3 p4)
265 (html2text-delete-tags p1 p2 p3 p4))
267 (defun html2text-clean-anchor (p1 p2 p3 p4)
268 ;; If someone can explain how to make the URL clickable I will
269 ;; surely improve upon this.
270 (let ((href (html2text-attr-value (html2text-get-attr p1 p2) "href")))
271 (delete-region p1 p4)
274 (insert (substring href 1 -1 ))
275 (put-text-property p1 (point) 'face 'bold))))
278 ;; </Functions to be called to format a tag-pair>
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; <Functions to be called to fix up paragraphs>
288 (defun html2text-fix-paragraph (p1 p2)
293 (when (re-search-forward "<br>$" p2 t)
294 (setq has-br-line t))
297 (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
299 (setq refill-start (point))
301 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
304 ;; refill-stop should ideally be adjusted to
305 ;; accomodate the "<br>" strings which are removed
306 ;; between refill-start and refill-stop. Can simply
307 ;; be returned from my-replace-string
308 (setq refill-stop (+ (point)
309 (html2text-replace-string
311 refill-start (point))))
312 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
314 (fill-region refill-start refill-stop))))
315 (html2text-replace-string "<br>" "" p1 p2))
318 ;; This one is interactive ...
320 (defun html2text-fix-paragraphs ()
321 "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
322 fashion, quite close to pure guess-work. It does work in some cases though."
324 (html2text-buffer-head)
325 (replace-regexp "^<br>$" "")
326 ;; Removing lonely <br> on a single line, if they are left intact we
327 ;; dont have any paragraphs at all.
328 (html2text-buffer-head)
331 (forward-paragraph 1)
332 ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5)
333 (html2text-fix-paragraph p1 (1- (point)))
336 (forward-paragraph 1)))))
339 ;; </Functions to be called to fix up paragraphs>
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;; <Interactive functions>
349 (defun html2text-remove-tags (tag-list)
350 "Removes the tags listed in the list \"html2text-remove-tag-list\".
351 See the documentation for that variable."
353 (dolist (tag tag-list)
354 (html2text-buffer-head)
355 (while (re-search-forward (format "</?%s[^>]*>" tag) (point-max) t)
356 (delete-region (match-beginning 0) (match-end 0)))))
358 (defun html2text-format-tags ()
359 "See the variable \"html2text-format-tag-list\" for documentation"
361 (dolist (tag-and-function html2text-format-tag-list)
362 (let ((tag (car tag-and-function))
363 (function (cdr tag-and-function)))
364 (html2text-buffer-head)
365 (while (re-search-forward (format "<%s\\( [^>]*\\)?>" tag)
370 (attr (match-string 0)))
371 (search-backward "<" (point-min) t)
373 (search-forward (format "</%s>" tag) (point-max) t)
375 (search-backward "</" (point-min) t)
377 (funcall function p1 p2 p3 p4)
381 (defun html2text-substitute ()
382 "See the variable \"html2text-replace-list\" for documentation"
384 (dolist (e html2text-replace-list)
385 (html2text-buffer-head)
386 (let ((old-string (car e))
387 (new-string (cdr e)))
388 (html2text-replace-string old-string new-string (point-min) (point-max)))
391 (defun html2text-format-single-elements ()
394 (dolist (tag-and-function html2text-format-single-element-list)
395 (let ((tag (car tag-and-function))
396 (function (cdr tag-and-function)))
397 (html2text-buffer-head)
398 (while (re-search-forward (format "<%s\\( [^>]*\\)?>" tag)
402 (search-backward "<" (point-min) t)
404 (funcall function p1 p2))))))
412 "Convert HTML to plain text in the current buffer."
415 (let ((case-fold-search t)
417 (html2text-remove-tags html2text-remove-tag-list)
418 (html2text-format-tags)
419 (html2text-remove-tags html2text-remove-tag-list2)
420 (html2text-substitute)
421 (html2text-format-single-elements)
422 (html2text-fix-paragraphs))))
425 ;; </Interactive functions>
428 ;;; html2text.el ends here