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/>.
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'.
33 ;; This package was obsoleted by shr.el.
44 (defvar html2text-format-single-element-list
'(("hr" . html2text-clean-hr
)))
46 (defvar html2text-replace-list
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>.\"
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
131 \"<b> This is bold text </b>\"
136 Then the called function will typically format the text somewhat and
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."
161 (let ((delta (- (string-width to-string
) (string-width from-string
)))
163 (while (search-forward from-string max t
)
164 (replace-match to-string
)
165 (setq change
(+ change delta
)))
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
)
185 (re-search-forward "\\s-+" p2 t
)
187 (while (re-search-forward "[-a-z0-9._]+" p2 t
)
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))
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
)
215 (while (search-forward "<li>" p2 t
)
216 (setq items
(1+ items
)))
218 (while (< item-nr items
)
219 (setq item-nr
(1+ item-nr
))
220 (search-forward "<li>" (point-max) t
)
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
)
230 (while (search-forward "<dt>" p2 t
)
231 (setq items
(1+ items
)))
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))
240 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t
)
243 (let* ((mw1 (string-width (match-string 1)))
244 (mw2 (string-width (match-string 2)))
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
)
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
)
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
)
331 (when (re-search-forward "<br>$" p2 t
)
333 (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t
)
335 (setq refill-start
(point))
337 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t
)
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
347 refill-start
(point))))
348 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
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."
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))
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)))
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."
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."
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
)
407 (search-backward "<" (point-min) t
)
409 (unless (search-forward (format "</%s>" tag
) (point-max) t
)
411 (insert (format "</%s>" tag
)))
413 (search-backward "</" (point-min) t
)
415 (funcall function p1 p2 p3 p4
)
418 (defun html2text-substitute ()
419 "See the variable `html2text-replace-list' for documentation."
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 ()
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
)
437 (search-backward "<" (point-min) t
)
439 (funcall function p1 p2
))))))
447 "Convert HTML to plain text in the current buffer."
450 (let ((case-fold-search t
)
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>
464 ;;; html2text.el ends here