Avoid segfaults when some display vector is an empty string
[emacs.git] / lisp / obsolete / html2text.el
blob27560a70c632137e71ead38a930d37ea7fde535b
1 ;;; html2text.el --- a simple html to plain text converter -*- coding: utf-8 -*-
3 ;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 ;; Author: Joakim Hove <hove@phys.ntnu.no>
6 ;; Obsolete-since: 26.1
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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".
31 ;; The main function is `html2text'.
33 ;; This package was obsoleted by shr.el.
35 ;;; Code:
38 ;; <Global variables>
41 (eval-when-compile
42 (require 'cl))
44 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
46 (defvar html2text-replace-list
47 '(("&acute;" . "`")
48 ("&amp;" . "&")
49 ("&apos;" . "'")
50 ("&brvbar;" . "|")
51 ("&cent;" . "c")
52 ("&circ;" . "^")
53 ("&copy;" . "(C)")
54 ("&curren;" . "(#)")
55 ("&deg;" . "degree")
56 ("&divide;" . "/")
57 ("&euro;" . "e")
58 ("&frac12;" . "1/2")
59 ("&gt;" . ">")
60 ("&iquest;" . "?")
61 ("&laquo;" . "<<")
62 ("&ldquo" . "\"")
63 ("&lsaquo;" . "(")
64 ("&lsquo;" . "`")
65 ("&lt;" . "<")
66 ("&mdash;" . "--")
67 ("&nbsp;" . " ")
68 ("&ndash;" . "-")
69 ("&permil;" . "%%")
70 ("&plusmn;" . "+-")
71 ("&pound;" . "£")
72 ("&quot;" . "\"")
73 ("&raquo;" . ">>")
74 ("&rdquo" . "\"")
75 ("&reg;" . "(R)")
76 ("&rsaquo;" . ")")
77 ("&rsquo;" . "'")
78 ("&sect;" . "§")
79 ("&sup1;" . "^1")
80 ("&sup2;" . "^2")
81 ("&sup3;" . "^3")
82 ("&tilde;" . "~"))
83 "The map of entity to text.
85 This is an alist were each element is a dotted pair consisting of an
86 old string, and a replacement string. This replacement is done by the
87 function `html2text-substitute' which basically performs a
88 `replace-string' operation for every element in the list. This is
89 completely verbatim - without any use of REGEXP.")
91 (defvar html2text-remove-tag-list
92 '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta")
93 "A list of removable tags.
95 This is a list of tags which should be removed, without any
96 formatting. Note that tags in the list are presented *without*
97 any \"<\" or \">\". All occurrences of a tag appearing in this
98 list are removed, irrespective of whether it is a closing or
99 opening tag, or if the tag has additional attributes. The
100 deletion is done by the function `html2text-remove-tags'.
102 For instance the text:
104 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
106 will be reduced to:
108 \"Here comes something big.\"
110 If this list contains the element \"font\".")
112 (defvar html2text-format-tag-list
113 '(("b" . html2text-clean-bold)
114 ("strong" . html2text-clean-bold)
115 ("u" . html2text-clean-underline)
116 ("i" . html2text-clean-italic)
117 ("em" . html2text-clean-italic)
118 ("blockquote" . html2text-clean-blockquote)
119 ("a" . html2text-clean-anchor)
120 ("ul" . html2text-clean-ul)
121 ("ol" . html2text-clean-ol)
122 ("dl" . html2text-clean-dl)
123 ("center" . html2text-clean-center))
124 "An alist of tags and processing functions.
126 This is an alist where each dotted pair consists of a tag, and then
127 the name of a function to be called when this tag is found. The
128 function is called with the arguments p1, p2, p3 and p4. These are
129 demonstrated below:
131 \"<b> This is bold text </b>\"
132 ^ ^ ^ ^
133 | | | |
134 p1 p2 p3 p4
136 Then the called function will typically format the text somewhat and
137 remove the tags.")
139 (defvar html2text-remove-tag-list2 '("li" "dt" "dd" "meta")
140 "Another list of removable tags.
142 This is a list of tags which are removed similarly to the list
143 `html2text-remove-tag-list' - but these tags are retained for the
144 formatting, and then moved afterward.")
147 ;; </Global variables>
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; <Utility functions>
158 (defun html2text-replace-string (from-string to-string min max)
159 "Replace FROM-STRING with TO-STRING in region from MIN to MAX."
160 (goto-char min)
161 (let ((delta (- (string-width to-string) (string-width from-string)))
162 (change 0))
163 (while (search-forward from-string max t)
164 (replace-match to-string)
165 (setq change (+ change delta)))
166 change))
169 ;; </Utility functions>
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;; <Functions related to attributes> i.e. <font size=+3>
179 (defun html2text-attr-value (list attribute)
180 "Get value of ATTRIBUTE from LIST."
181 (nth 1 (assoc attribute list)))
183 (defun html2text-get-attr (p1 p2)
184 (goto-char p1)
185 (re-search-forward "\\s-+" p2 t)
186 (let (attr-list)
187 (while (re-search-forward "[-a-z0-9._]+" p2 t)
188 (setq attr-list
189 (cons
190 (list (match-string 0)
191 (when (looking-at "\\s-*=")
192 (goto-char (match-end 0))
193 (skip-chars-forward "[:space:]")
194 (when (or (looking-at "\"[^\"]*\"\\|'[^']*'")
195 (looking-at "[-a-z0-9._:]+"))
196 (goto-char (match-end 0))
197 (match-string 0))))
198 attr-list)))
199 attr-list))
202 ;; </Functions related to attributes>
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 ;; <Functions to be called to format a tag-pair>
211 (defun html2text-clean-list-items (p1 p2 list-type)
212 (goto-char p1)
213 (let ((item-nr 0)
214 (items 0))
215 (while (search-forward "<li>" p2 t)
216 (setq items (1+ items)))
217 (goto-char p1)
218 (while (< item-nr items)
219 (setq item-nr (1+ item-nr))
220 (search-forward "<li>" (point-max) t)
221 (cond
222 ((string= list-type "ul") (insert " o "))
223 ((string= list-type "ol") (insert (format " %s: " item-nr)))
224 (t (insert " x "))))))
226 (defun html2text-clean-dtdd (p1 p2)
227 (goto-char p1)
228 (let ((items 0)
229 (item-nr 0))
230 (while (search-forward "<dt>" p2 t)
231 (setq items (1+ items)))
232 (goto-char p1)
233 (while (< item-nr items)
234 (setq item-nr (1+ item-nr))
235 (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
236 (when (match-string 1)
237 (delete-region (point) (- (point) (string-width (match-string 1)))))
238 (let ((def-p1 (point))
239 (def-p2 0))
240 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
241 (if (match-string 1)
242 (progn
243 (let* ((mw1 (string-width (match-string 1)))
244 (mw2 (string-width (match-string 2)))
245 (mw (+ mw1 mw2)))
246 (goto-char (- (point) mw))
247 (delete-region (point) (+ (point) mw1))
248 (setq def-p2 (point))))
249 (setq def-p2 (- (point) (string-width (match-string 2)))))
250 (put-text-property def-p1 def-p2 'face 'bold)))))
252 (defun html2text-delete-tags (p1 p2 p3 p4)
253 (delete-region p1 p2)
254 (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
256 (defun html2text-delete-single-tag (p1 p2)
257 (delete-region p1 p2))
259 (defun html2text-clean-hr (p1 p2)
260 (html2text-delete-single-tag p1 p2)
261 (goto-char p1)
262 (newline 1)
263 (insert (make-string fill-column ?-)))
265 (defun html2text-clean-ul (p1 p2 p3 p4)
266 (html2text-delete-tags p1 p2 p3 p4)
267 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul"))
269 (defun html2text-clean-ol (p1 p2 p3 p4)
270 (html2text-delete-tags p1 p2 p3 p4)
271 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol"))
273 (defun html2text-clean-dl (p1 p2 p3 p4)
274 (html2text-delete-tags p1 p2 p3 p4)
275 (html2text-clean-dtdd p1 (- p3 (- p1 p2))))
277 (defun html2text-clean-center (p1 p2 p3 p4)
278 (html2text-delete-tags p1 p2 p3 p4)
279 (center-region p1 (- p3 (- p2 p1))))
281 (defun html2text-clean-bold (p1 p2 p3 p4)
282 (put-text-property p2 p3 'face 'bold)
283 (html2text-delete-tags p1 p2 p3 p4))
285 (defun html2text-clean-title (p1 p2 p3 p4)
286 (put-text-property p2 p3 'face 'bold)
287 (html2text-delete-tags p1 p2 p3 p4))
289 (defun html2text-clean-underline (p1 p2 p3 p4)
290 (put-text-property p2 p3 'face 'underline)
291 (html2text-delete-tags p1 p2 p3 p4))
293 (defun html2text-clean-italic (p1 p2 p3 p4)
294 (put-text-property p2 p3 'face 'italic)
295 (html2text-delete-tags p1 p2 p3 p4))
297 (defun html2text-clean-font (p1 p2 p3 p4)
298 (html2text-delete-tags p1 p2 p3 p4))
300 (defun html2text-clean-blockquote (p1 p2 p3 p4)
301 (html2text-delete-tags p1 p2 p3 p4))
303 (defun html2text-clean-anchor (p1 p2 p3 p4)
304 ;; If someone can explain how to make the URL clickable I will surely
305 ;; improve upon this.
306 ;; Maybe `goto-addr.el' can be used here.
307 (let* ((attr-list (html2text-get-attr p1 p2))
308 (href (html2text-attr-value attr-list "href")))
309 (delete-region p1 p4)
310 (when href
311 (goto-char p1)
312 (insert (if (string-match "\\`['\"].*['\"]\\'" href)
313 (substring href 1 -1) href))
314 (put-text-property p1 (point) 'face 'bold))))
317 ;; </Functions to be called to format a tag-pair>
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;; <Functions to be called to fix up paragraphs>
327 (defun html2text-fix-paragraph (p1 p2)
328 (goto-char p1)
329 (let ((refill-start)
330 (refill-stop))
331 (when (re-search-forward "<br>$" p2 t)
332 (goto-char p1)
333 (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
334 (beginning-of-line)
335 (setq refill-start (point))
336 (goto-char p2)
337 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
338 (forward-line 1)
339 (end-of-line)
340 ;; refill-stop should ideally be adjusted to
341 ;; accommodate the "<br>" strings which are removed
342 ;; between refill-start and refill-stop. Can simply
343 ;; be returned from my-replace-string
344 (setq refill-stop (+ (point)
345 (html2text-replace-string
346 "<br>" ""
347 refill-start (point))))
348 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
349 ;; (sleep-for 4)
350 (fill-region refill-start refill-stop))))
351 (html2text-replace-string "<br>" "" p1 p2))
354 ;; This one is interactive ...
356 (defun html2text-fix-paragraphs ()
357 "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
358 fashion, quite close to pure guess-work. It does work in some cases though."
359 (interactive)
360 (goto-char (point-min))
361 (while (re-search-forward "^<br>$" nil t)
362 (delete-region (match-beginning 0) (match-end 0)))
363 ;; Removing lonely <br> on a single line, if they are left intact we
364 ;; don't have any paragraphs at all.
365 (goto-char (point-min))
366 (while (not (eobp))
367 (let ((p1 (point)))
368 (forward-paragraph 1)
369 ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5)
370 (html2text-fix-paragraph p1 (1- (point)))
371 (goto-char p1)
372 (when (not (eobp))
373 (forward-paragraph 1)))))
376 ;; </Functions to be called to fix up paragraphs>
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;; <Interactive functions>
386 (defun html2text-remove-tags (tag-list)
387 "Removes the tags listed in the list `html2text-remove-tag-list'.
388 See the documentation for that variable."
389 (interactive)
390 (dolist (tag tag-list)
391 (goto-char (point-min))
392 (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
393 (delete-region (match-beginning 0) (match-end 0)))))
395 (defun html2text-format-tags ()
396 "See the variable `html2text-format-tag-list' for documentation."
397 (interactive)
398 (dolist (tag-and-function html2text-format-tag-list)
399 (let ((tag (car tag-and-function))
400 (function (cdr tag-and-function)))
401 (goto-char (point-min))
402 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
403 (point-max) t)
404 (let ((p1)
405 (p2 (point))
406 (p3) (p4))
407 (search-backward "<" (point-min) t)
408 (setq p1 (point))
409 (unless (search-forward (format "</%s>" tag) (point-max) t)
410 (goto-char p2)
411 (insert (format "</%s>" tag)))
412 (setq p4 (point))
413 (search-backward "</" (point-min) t)
414 (setq p3 (point))
415 (funcall function p1 p2 p3 p4)
416 (goto-char p1))))))
418 (defun html2text-substitute ()
419 "See the variable `html2text-replace-list' for documentation."
420 (interactive)
421 (dolist (e html2text-replace-list)
422 (goto-char (point-min))
423 (let ((old-string (car e))
424 (new-string (cdr e)))
425 (html2text-replace-string old-string new-string (point-min) (point-max)))))
427 (defun html2text-format-single-elements ()
428 (interactive)
429 (dolist (tag-and-function html2text-format-single-element-list)
430 (let ((tag (car tag-and-function))
431 (function (cdr tag-and-function)))
432 (goto-char (point-min))
433 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
434 (point-max) t)
435 (let ((p1)
436 (p2 (point)))
437 (search-backward "<" (point-min) t)
438 (setq p1 (point))
439 (funcall function p1 p2))))))
442 ;; Main function
445 ;;;###autoload
446 (defun html2text ()
447 "Convert HTML to plain text in the current buffer."
448 (interactive)
449 (save-excursion
450 (let ((case-fold-search t)
451 (buffer-read-only))
452 (html2text-remove-tags html2text-remove-tag-list)
453 (html2text-format-tags)
454 (html2text-remove-tags html2text-remove-tag-list2)
455 (html2text-substitute)
456 (html2text-format-single-elements)
457 (html2text-fix-paragraphs))))
460 ;; </Interactive functions>
462 (provide 'html2text)
464 ;;; html2text.el ends here