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/>.
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.
42 (defvar html2text-format-single-element-list
'(("hr" . html2text-clean-hr
)))
44 (defvar html2text-replace-list
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>.\"
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
129 \"<b> This is bold text </b>\"
134 Then the called function will typically format the text somewhat and
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."
159 (let ((delta (- (string-width to-string
) (string-width from-string
)))
161 (while (search-forward from-string max t
)
162 (replace-match to-string
)
163 (setq change
(+ change delta
)))
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
)
183 (re-search-forward "\\s-+" p2 t
)
185 (while (re-search-forward "[-a-z0-9._]+" p2 t
)
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))
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
)
213 (while (search-forward "<li>" p2 t
)
214 (setq items
(1+ items
)))
216 (while (< item-nr items
)
217 (setq item-nr
(1+ item-nr
))
218 (search-forward "<li>" (point-max) t
)
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
)
228 (while (search-forward "<dt>" p2 t
)
229 (setq items
(1+ items
)))
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))
238 (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t
)
241 (let* ((mw1 (string-width (match-string 1)))
242 (mw2 (string-width (match-string 2)))
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
)
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
)
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
)
329 (when (re-search-forward "<br>$" p2 t
)
331 (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t
)
333 (setq refill-start
(point))
335 (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t
)
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
345 refill-start
(point))))
346 ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
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."
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))
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)))
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."
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."
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
)
405 (search-backward "<" (point-min) t
)
407 (unless (search-forward (format "</%s>" tag
) (point-max) t
)
409 (insert (format "</%s>" tag
)))
411 (search-backward "</" (point-min) t
)
413 (funcall function p1 p2 p3 p4
)
416 (defun html2text-substitute ()
417 "See the variable `html2text-replace-list' for documentation."
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 ()
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
)
435 (search-backward "<" (point-min) t
)
437 (funcall function p1 p2
))))))
445 "Convert HTML to plain text in the current buffer."
448 (let ((case-fold-search t
)
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>
462 ;;; html2text.el ends here