html2text.el 14 KB

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