html2text.el 14 KB

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