Try and fix the more obvious sources of bug#30635
[emacs.git] / lisp / obsolete / html2text.el
blob3d97c903bae2b99e68db761e521900182dea4c81
1 ;;; html2text.el --- a simple html to plain text converter -*- coding: utf-8 -*-
3 ;; Copyright (C) 2002-2018 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 <https://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>
42 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
44 (defvar html2text-replace-list
45 '(("&acute;" . "`")
46 ("&amp;" . "&")
47 ("&apos;" . "'")
48 ("&brvbar;" . "|")
49 ("&cent;" . "c")
50 ("&circ;" . "^")
51 ("&copy;" . "(C)")
52 ("&curren;" . "(#)")
53 ("&deg;" . "degree")
54 ("&divide;" . "/")
55 ("&euro;" . "e")
56 ("&frac12;" . "1/2")
57 ("&gt;" . ">")
58 ("&iquest;" . "?")
59 ("&laquo;" . "<<")
60 ("&ldquo" . "\"")
61 ("&lsaquo;" . "(")
62 ("&lsquo;" . "`")
63 ("&lt;" . "<")
64 ("&mdash;" . "--")
65 ("&nbsp;" . " ")
66 ("&ndash;" . "-")
67 ("&permil;" . "%%")
68 ("&plusmn;" . "+-")
69 ("&pound;" . "£")
70 ("&quot;" . "\"")
71 ("&raquo;" . ">>")
72 ("&rdquo" . "\"")
73 ("&reg;" . "(R)")
74 ("&rsaquo;" . ")")
75 ("&rsquo;" . "'")
76 ("&sect;" . "§")
77 ("&sup1;" . "^1")
78 ("&sup2;" . "^2")
79 ("&sup3;" . "^3")
80 ("&tilde;" . "~"))
81 "The map of entity to text.
83 This is an alist were each element is a dotted pair consisting of an
84 old string, and a replacement string. This replacement is done by the
85 function `html2text-substitute' which basically performs a
86 `replace-string' operation for every element in the list. This is
87 completely verbatim - without any use of REGEXP.")
89 (defvar html2text-remove-tag-list
90 '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta")
91 "A list of removable tags.
93 This is a list of tags which should be removed, without any
94 formatting. Note that tags in the list are presented *without*
95 any \"<\" or \">\". All occurrences of a tag appearing in this
96 list are removed, irrespective of whether it is a closing or
97 opening tag, or if the tag has additional attributes. The
98 deletion is done by the function `html2text-remove-tags'.
100 For instance the text:
102 \"Here comes something <font size\"+3\" face=\"Helvetica\"> big </font>.\"
104 will be reduced to:
106 \"Here comes something big.\"
108 If this list contains the element \"font\".")
110 (defvar html2text-format-tag-list
111 '(("b" . html2text-clean-bold)
112 ("strong" . html2text-clean-bold)
113 ("u" . html2text-clean-underline)
114 ("i" . html2text-clean-italic)
115 ("em" . html2text-clean-italic)
116 ("blockquote" . html2text-clean-blockquote)
117 ("a" . html2text-clean-anchor)
118 ("ul" . html2text-clean-ul)
119 ("ol" . html2text-clean-ol)
120 ("dl" . html2text-clean-dl)
121 ("center" . html2text-clean-center))
122 "An alist of tags and processing functions.
124 This is an alist where each dotted pair consists of a tag, and then
125 the name of a function to be called when this tag is found. The
126 function is called with the arguments p1, p2, p3 and p4. These are
127 demonstrated below:
129 \"<b> This is bold text </b>\"
130 ^ ^ ^ ^
131 | | | |
132 p1 p2 p3 p4
134 Then the called function will typically format the text somewhat and
135 remove the tags.")
137 (defvar html2text-remove-tag-list2 '("li" "dt" "dd" "meta")
138 "Another list of removable tags.
140 This is a list of tags which are removed similarly to the list
141 `html2text-remove-tag-list' - but these tags are retained for the
142 formatting, and then moved afterward.")
145 ;; </Global variables>
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; <Utility functions>
156 (defun html2text-replace-string (from-string to-string min max)
157 "Replace FROM-STRING with TO-STRING in region from MIN to MAX."
158 (goto-char min)
159 (let ((delta (- (string-width to-string) (string-width from-string)))
160 (change 0))
161 (while (search-forward from-string max t)
162 (replace-match to-string)
163 (setq change (+ change delta)))
164 change))
167 ;; </Utility functions>
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 ;; <Functions related to attributes> i.e. <font size=+3>
177 (defun html2text-attr-value (list attribute)
178 "Get value of ATTRIBUTE from LIST."
179 (nth 1 (assoc attribute list)))
181 (defun html2text-get-attr (p1 p2)
182 (goto-char p1)
183 (re-search-forward "\\s-+" p2 t)
184 (let (attr-list)
185 (while (re-search-forward "[-a-z0-9._]+" p2 t)
186 (setq attr-list
187 (cons
188 (list (match-string 0)
189 (when (looking-at "\\s-*=")
190 (goto-char (match-end 0))
191 (skip-chars-forward "[:space:]")
192 (when (or (looking-at "\"[^\"]*\"\\|'[^']*'")
193 (looking-at "[-a-z0-9._:]+"))
194 (goto-char (match-end 0))
195 (match-string 0))))
196 attr-list)))
197 attr-list))
200 ;; </Functions related to attributes>
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; <Functions to be called to format a tag-pair>
209 (defun html2text-clean-list-items (p1 p2 list-type)
210 (goto-char p1)
211 (let ((item-nr 0)
212 (items 0))
213 (while (search-forward "<li>" p2 t)
214 (setq items (1+ items)))
215 (goto-char p1)
216 (while (< item-nr items)
217 (setq item-nr (1+ item-nr))
218 (search-forward "<li>" (point-max) t)
219 (cond
220 ((string= list-type "ul") (insert " o "))
221 ((string= list-type "ol") (insert (format " %s: " item-nr)))
222 (t (insert " x "))))))
224 (defun html2text-clean-dtdd (p1 p2)
225 (goto-char p1)
226 (let ((items 0)
227 (item-nr 0))
228 (while (search-forward "<dt>" p2 t)
229 (setq items (1+ items)))
230 (goto-char p1)
231 (while (< item-nr items)
232 (setq item-nr (1+ item-nr))
233 (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
234 (when (match-string 1)
235 (delete-region (point) (- (point) (string-width (match-string 1)))))
236 (let ((def-p1 (point))
237 (def-p2 0))
238 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
239 (if (match-string 1)
240 (progn
241 (let* ((mw1 (string-width (match-string 1)))
242 (mw2 (string-width (match-string 2)))
243 (mw (+ mw1 mw2)))
244 (goto-char (- (point) mw))
245 (delete-region (point) (+ (point) mw1))
246 (setq def-p2 (point))))
247 (setq def-p2 (- (point) (string-width (match-string 2)))))
248 (put-text-property def-p1 def-p2 'face 'bold)))))
250 (defun html2text-delete-tags (p1 p2 p3 p4)
251 (delete-region p1 p2)
252 (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
254 (defun html2text-delete-single-tag (p1 p2)
255 (delete-region p1 p2))
257 (defun html2text-clean-hr (p1 p2)
258 (html2text-delete-single-tag p1 p2)
259 (goto-char p1)
260 (newline 1)
261 (insert (make-string fill-column ?-)))
263 (defun html2text-clean-ul (p1 p2 p3 p4)
264 (html2text-delete-tags p1 p2 p3 p4)
265 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul"))
267 (defun html2text-clean-ol (p1 p2 p3 p4)
268 (html2text-delete-tags p1 p2 p3 p4)
269 (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ol"))
271 (defun html2text-clean-dl (p1 p2 p3 p4)
272 (html2text-delete-tags p1 p2 p3 p4)
273 (html2text-clean-dtdd p1 (- p3 (- p1 p2))))
275 (defun html2text-clean-center (p1 p2 p3 p4)
276 (html2text-delete-tags p1 p2 p3 p4)
277 (center-region p1 (- p3 (- p2 p1))))
279 (defun html2text-clean-bold (p1 p2 p3 p4)
280 (put-text-property p2 p3 'face 'bold)
281 (html2text-delete-tags p1 p2 p3 p4))
283 (defun html2text-clean-title (p1 p2 p3 p4)
284 (put-text-property p2 p3 'face 'bold)
285 (html2text-delete-tags p1 p2 p3 p4))
287 (defun html2text-clean-underline (p1 p2 p3 p4)
288 (put-text-property p2 p3 'face 'underline)
289 (html2text-delete-tags p1 p2 p3 p4))
291 (defun html2text-clean-italic (p1 p2 p3 p4)
292 (put-text-property p2 p3 'face 'italic)
293 (html2text-delete-tags p1 p2 p3 p4))
295 (defun html2text-clean-font (p1 p2 p3 p4)
296 (html2text-delete-tags p1 p2 p3 p4))
298 (defun html2text-clean-blockquote (p1 p2 p3 p4)
299 (html2text-delete-tags p1 p2 p3 p4))
301 (defun html2text-clean-anchor (p1 p2 _p3 p4)
302 ;; If someone can explain how to make the URL clickable I will surely
303 ;; improve upon this.
304 ;; Maybe `goto-addr.el' can be used here.
305 (let* ((attr-list (html2text-get-attr p1 p2))
306 (href (html2text-attr-value attr-list "href")))
307 (delete-region p1 p4)
308 (when href
309 (goto-char p1)
310 (insert (if (string-match "\\`['\"].*['\"]\\'" href)
311 (substring href 1 -1) href))
312 (put-text-property p1 (point) 'face 'bold))))
315 ;; </Functions to be called to format a tag-pair>
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;; <Functions to be called to fix up paragraphs>
325 (defun html2text-fix-paragraph (p1 p2)
326 (goto-char p1)
327 (let ((refill-start)
328 (refill-stop))
329 (when (re-search-forward "<br>$" p2 t)
330 (goto-char p1)
331 (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
332 (beginning-of-line)
333 (setq refill-start (point))
334 (goto-char p2)
335 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
336 (forward-line 1)
337 (end-of-line)
338 ;; refill-stop should ideally be adjusted to
339 ;; accommodate the "<br>" strings which are removed
340 ;; between refill-start and refill-stop. Can simply
341 ;; be returned from my-replace-string
342 (setq refill-stop (+ (point)
343 (html2text-replace-string
344 "<br>" ""
345 refill-start (point))))
346 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
347 ;; (sleep-for 4)
348 (fill-region refill-start refill-stop))))
349 (html2text-replace-string "<br>" "" p1 p2))
352 ;; This one is interactive ...
354 (defun html2text-fix-paragraphs ()
355 "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
356 fashion, quite close to pure guess-work. It does work in some cases though."
357 (interactive)
358 (goto-char (point-min))
359 (while (re-search-forward "^<br>$" nil t)
360 (delete-region (match-beginning 0) (match-end 0)))
361 ;; Removing lonely <br> on a single line, if they are left intact we
362 ;; don't have any paragraphs at all.
363 (goto-char (point-min))
364 (while (not (eobp))
365 (let ((p1 (point)))
366 (forward-paragraph 1)
367 ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5)
368 (html2text-fix-paragraph p1 (1- (point)))
369 (goto-char p1)
370 (when (not (eobp))
371 (forward-paragraph 1)))))
374 ;; </Functions to be called to fix up paragraphs>
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;; <Interactive functions>
384 (defun html2text-remove-tags (tag-list)
385 "Removes the tags listed in the list `html2text-remove-tag-list'.
386 See the documentation for that variable."
387 (interactive)
388 (dolist (tag tag-list)
389 (goto-char (point-min))
390 (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
391 (delete-region (match-beginning 0) (match-end 0)))))
393 (defun html2text-format-tags ()
394 "See the variable `html2text-format-tag-list' for documentation."
395 (interactive)
396 (dolist (tag-and-function html2text-format-tag-list)
397 (let ((tag (car tag-and-function))
398 (function (cdr tag-and-function)))
399 (goto-char (point-min))
400 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
401 (point-max) t)
402 (let ((p1)
403 (p2 (point))
404 (p3) (p4))
405 (search-backward "<" (point-min) t)
406 (setq p1 (point))
407 (unless (search-forward (format "</%s>" tag) (point-max) t)
408 (goto-char p2)
409 (insert (format "</%s>" tag)))
410 (setq p4 (point))
411 (search-backward "</" (point-min) t)
412 (setq p3 (point))
413 (funcall function p1 p2 p3 p4)
414 (goto-char p1))))))
416 (defun html2text-substitute ()
417 "See the variable `html2text-replace-list' for documentation."
418 (interactive)
419 (dolist (e html2text-replace-list)
420 (goto-char (point-min))
421 (let ((old-string (car e))
422 (new-string (cdr e)))
423 (html2text-replace-string old-string new-string (point-min) (point-max)))))
425 (defun html2text-format-single-elements ()
426 (interactive)
427 (dolist (tag-and-function html2text-format-single-element-list)
428 (let ((tag (car tag-and-function))
429 (function (cdr tag-and-function)))
430 (goto-char (point-min))
431 (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
432 (point-max) t)
433 (let ((p1)
434 (p2 (point)))
435 (search-backward "<" (point-min) t)
436 (setq p1 (point))
437 (funcall function p1 p2))))))
440 ;; Main function
443 ;;;###autoload
444 (defun html2text ()
445 "Convert HTML to plain text in the current buffer."
446 (interactive)
447 (save-excursion
448 (let ((case-fold-search t)
449 (buffer-read-only))
450 (html2text-remove-tags html2text-remove-tag-list)
451 (html2text-format-tags)
452 (html2text-remove-tags html2text-remove-tag-list2)
453 (html2text-substitute)
454 (html2text-format-single-elements)
455 (html2text-fix-paragraphs))))
458 ;; </Interactive functions>
460 (provide 'html2text)
462 ;;; html2text.el ends here