html2text.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. ;;; html2text.el --- a simple html to plain text converter
  2. ;; Copyright (C) 2002-2012 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 " +[^ ]" p2 t)
  146. (let* ((attr-string (buffer-substring-no-properties (1- (point)) (1- p2)))
  147. (tmp-list (split-string attr-string))
  148. (attr-list)
  149. (counter 0)
  150. (prev (car tmp-list))
  151. (this (nth 1 tmp-list))
  152. (next (nth 2 tmp-list))
  153. (index 1))
  154. (cond
  155. ;; size=3
  156. ((string-match "[^ ]=[^ ]" prev)
  157. (let ((attr (nth 0 (split-string prev "=")))
  158. (value (substring prev (1+ (string-match "=" prev)))))
  159. (setq attr-list (cons (list attr value) attr-list))))
  160. ;; size= 3
  161. ((string-match "[^ ]=\\'" prev)
  162. (setq attr-list (cons (list (substring prev 0 -1) this) attr-list))))
  163. (while (< index (length tmp-list))
  164. (cond
  165. ;; size=3
  166. ((string-match "[^ ]=[^ ]" this)
  167. (let ((attr (nth 0 (split-string this "=")))
  168. (value (substring prev (1+ (string-match "=" this)))))
  169. (setq attr-list (cons (list attr value) attr-list))))
  170. ;; size =3
  171. ((string-match "\\`=[^ ]" this)
  172. (setq attr-list (cons (list prev (substring this 1)) attr-list)))
  173. ;; size= 3
  174. ((string-match "[^ ]=\\'" this)
  175. (setq attr-list (cons (list (substring this 0 -1) next) attr-list)))
  176. ;; size = 3
  177. ((string= "=" this)
  178. (setq attr-list (cons (list prev next) attr-list))))
  179. (setq index (1+ index))
  180. (setq prev this)
  181. (setq this next)
  182. (setq next (nth (1+ index) tmp-list)))
  183. ;;
  184. ;; Tags with no accompanying "=" i.e. value=nil
  185. ;;
  186. (setq prev (car tmp-list))
  187. (setq this (nth 1 tmp-list))
  188. (setq next (nth 2 tmp-list))
  189. (setq index 1)
  190. (when (and (not (string-match "=" prev))
  191. (not (string= (substring this 0 1) "=")))
  192. (setq attr-list (cons (list prev nil) attr-list)))
  193. (while (< index (1- (length tmp-list)))
  194. (when (and (not (string-match "=" this))
  195. (not (or (string= (substring next 0 1) "=")
  196. (string= (substring prev -1) "="))))
  197. (setq attr-list (cons (list this nil) attr-list)))
  198. (setq index (1+ index))
  199. (setq prev this)
  200. (setq this next)
  201. (setq next (nth (1+ index) tmp-list)))
  202. (when (and this
  203. (not (string-match "=" this))
  204. (not (string= (substring prev -1) "=")))
  205. (setq attr-list (cons (list this nil) attr-list)))
  206. ;; return - value
  207. attr-list))
  208. ;;
  209. ;; </Functions related to attributes>
  210. ;;
  211. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  212. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  213. ;;
  214. ;; <Functions to be called to format a tag-pair>
  215. ;;
  216. (defun html2text-clean-list-items (p1 p2 list-type)
  217. (goto-char p1)
  218. (let ((item-nr 0)
  219. (items 0))
  220. (while (search-forward "<li>" p2 t)
  221. (setq items (1+ items)))
  222. (goto-char p1)
  223. (while (< item-nr items)
  224. (setq item-nr (1+ item-nr))
  225. (search-forward "<li>" (point-max) t)
  226. (cond
  227. ((string= list-type "ul") (insert " o "))
  228. ((string= list-type "ol") (insert (format " %s: " item-nr)))
  229. (t (insert " x "))))))
  230. (defun html2text-clean-dtdd (p1 p2)
  231. (goto-char p1)
  232. (let ((items 0)
  233. (item-nr 0))
  234. (while (search-forward "<dt>" p2 t)
  235. (setq items (1+ items)))
  236. (goto-char p1)
  237. (while (< item-nr items)
  238. (setq item-nr (1+ item-nr))
  239. (re-search-forward "<dt>\\([ ]*\\)" (point-max) t)
  240. (when (match-string 1)
  241. (delete-region (point) (- (point) (string-width (match-string 1)))))
  242. (let ((def-p1 (point))
  243. (def-p2 0))
  244. (re-search-forward "\\([ ]*\\)\\(</dt>\\|<dd>\\)" (point-max) t)
  245. (if (match-string 1)
  246. (progn
  247. (let* ((mw1 (string-width (match-string 1)))
  248. (mw2 (string-width (match-string 2)))
  249. (mw (+ mw1 mw2)))
  250. (goto-char (- (point) mw))
  251. (delete-region (point) (+ (point) mw1))
  252. (setq def-p2 (point))))
  253. (setq def-p2 (- (point) (string-width (match-string 2)))))
  254. (put-text-property def-p1 def-p2 'face 'bold)))))
  255. (defun html2text-delete-tags (p1 p2 p3 p4)
  256. (delete-region p1 p2)
  257. (delete-region (- p3 (- p2 p1)) (- p4 (- p2 p1))))
  258. (defun html2text-delete-single-tag (p1 p2)
  259. (delete-region p1 p2))
  260. (defun html2text-clean-hr (p1 p2)
  261. (html2text-delete-single-tag p1 p2)
  262. (goto-char p1)
  263. (newline 1)
  264. (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"))
  268. (defun html2text-clean-ol (p1 p2 p3 p4)
  269. (html2text-delete-tags p1 p2 p3 p4)
  270. (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))))
  274. (defun html2text-clean-center (p1 p2 p3 p4)
  275. (html2text-delete-tags p1 p2 p3 p4)
  276. (center-region p1 (- p3 (- p2 p1))))
  277. (defun html2text-clean-bold (p1 p2 p3 p4)
  278. (put-text-property p2 p3 'face 'bold)
  279. (html2text-delete-tags p1 p2 p3 p4))
  280. (defun html2text-clean-title (p1 p2 p3 p4)
  281. (put-text-property p2 p3 'face 'bold)
  282. (html2text-delete-tags p1 p2 p3 p4))
  283. (defun html2text-clean-underline (p1 p2 p3 p4)
  284. (put-text-property p2 p3 'face 'underline)
  285. (html2text-delete-tags p1 p2 p3 p4))
  286. (defun html2text-clean-italic (p1 p2 p3 p4)
  287. (put-text-property p2 p3 'face 'italic)
  288. (html2text-delete-tags p1 p2 p3 p4))
  289. (defun html2text-clean-font (p1 p2 p3 p4)
  290. (html2text-delete-tags p1 p2 p3 p4))
  291. (defun html2text-clean-blockquote (p1 p2 p3 p4)
  292. (html2text-delete-tags p1 p2 p3 p4))
  293. (defun html2text-clean-anchor (p1 p2 p3 p4)
  294. ;; If someone can explain how to make the URL clickable I will surely
  295. ;; improve upon this.
  296. ;; Maybe `goto-addr.el' can be used here.
  297. (let* ((attr-list (html2text-get-attr p1 p2))
  298. (href (html2text-attr-value attr-list "href")))
  299. (delete-region p1 p4)
  300. (when href
  301. (goto-char p1)
  302. (insert (if (string-match "\\`['\"].*['\"]\\'" href)
  303. (substring href 1 -1) href))
  304. (put-text-property p1 (point) 'face 'bold))))
  305. ;;
  306. ;; </Functions to be called to format a tag-pair>
  307. ;;
  308. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  309. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  310. ;;
  311. ;; <Functions to be called to fix up paragraphs>
  312. ;;
  313. (defun html2text-fix-paragraph (p1 p2)
  314. (goto-char p1)
  315. (let ((refill-start)
  316. (refill-stop))
  317. (when (re-search-forward "<br>$" p2 t)
  318. (goto-char p1)
  319. (when (re-search-forward ".+[^<][^b][^r][^>]$" p2 t)
  320. (beginning-of-line)
  321. (setq refill-start (point))
  322. (goto-char p2)
  323. (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
  324. (forward-line 1)
  325. (end-of-line)
  326. ;; refill-stop should ideally be adjusted to
  327. ;; accommodate the "<br>" strings which are removed
  328. ;; between refill-start and refill-stop. Can simply
  329. ;; be returned from my-replace-string
  330. (setq refill-stop (+ (point)
  331. (html2text-replace-string
  332. "<br>" ""
  333. refill-start (point))))
  334. ;; (message "Point = %s refill-stop = %s" (point) refill-stop)
  335. ;; (sleep-for 4)
  336. (fill-region refill-start refill-stop))))
  337. (html2text-replace-string "<br>" "" p1 p2))
  338. ;;
  339. ;; This one is interactive ...
  340. ;;
  341. (defun html2text-fix-paragraphs ()
  342. "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook
  343. fashion, quite close to pure guess-work. It does work in some cases though."
  344. (interactive)
  345. (goto-char (point-min))
  346. (while (re-search-forward "^<br>$" nil t)
  347. (delete-region (match-beginning 0) (match-end 0)))
  348. ;; Removing lonely <br> on a single line, if they are left intact we
  349. ;; don't have any paragraphs at all.
  350. (goto-char (point-min))
  351. (while (not (eobp))
  352. (let ((p1 (point)))
  353. (forward-paragraph 1)
  354. ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5)
  355. (html2text-fix-paragraph p1 (1- (point)))
  356. (goto-char p1)
  357. (when (not (eobp))
  358. (forward-paragraph 1)))))
  359. ;;
  360. ;; </Functions to be called to fix up paragraphs>
  361. ;;
  362. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  363. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  364. ;;
  365. ;; <Interactive functions>
  366. ;;
  367. (defun html2text-remove-tags (tag-list)
  368. "Removes the tags listed in the list `html2text-remove-tag-list'.
  369. See the documentation for that variable."
  370. (interactive)
  371. (dolist (tag tag-list)
  372. (goto-char (point-min))
  373. (while (re-search-forward (format "\\(</?%s[^>]*>\\)" tag) (point-max) t)
  374. (delete-region (match-beginning 0) (match-end 0)))))
  375. (defun html2text-format-tags ()
  376. "See the variable `html2text-format-tag-list' for documentation."
  377. (interactive)
  378. (dolist (tag-and-function html2text-format-tag-list)
  379. (let ((tag (car tag-and-function))
  380. (function (cdr tag-and-function)))
  381. (goto-char (point-min))
  382. (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
  383. (point-max) t)
  384. (let ((p1)
  385. (p2 (point))
  386. (p3) (p4))
  387. (search-backward "<" (point-min) t)
  388. (setq p1 (point))
  389. (unless (search-forward (format "</%s>" tag) (point-max) t)
  390. (goto-char p2)
  391. (insert (format "</%s>" tag)))
  392. (setq p4 (point))
  393. (search-backward "</" (point-min) t)
  394. (setq p3 (point))
  395. (funcall function p1 p2 p3 p4)
  396. (goto-char p1))))))
  397. (defun html2text-substitute ()
  398. "See the variable `html2text-replace-list' for documentation."
  399. (interactive)
  400. (dolist (e html2text-replace-list)
  401. (goto-char (point-min))
  402. (let ((old-string (car e))
  403. (new-string (cdr e)))
  404. (html2text-replace-string old-string new-string (point-min) (point-max)))))
  405. (defun html2text-format-single-elements ()
  406. (interactive)
  407. (dolist (tag-and-function html2text-format-single-element-list)
  408. (let ((tag (car tag-and-function))
  409. (function (cdr tag-and-function)))
  410. (goto-char (point-min))
  411. (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag)
  412. (point-max) t)
  413. (let ((p1)
  414. (p2 (point)))
  415. (search-backward "<" (point-min) t)
  416. (setq p1 (point))
  417. (funcall function p1 p2))))))
  418. ;;
  419. ;; Main function
  420. ;;
  421. ;;;###autoload
  422. (defun html2text ()
  423. "Convert HTML to plain text in the current buffer."
  424. (interactive)
  425. (save-excursion
  426. (let ((case-fold-search t)
  427. (buffer-read-only))
  428. (html2text-remove-tags html2text-remove-tag-list)
  429. (html2text-format-tags)
  430. (html2text-remove-tags html2text-remove-tag-list2)
  431. (html2text-substitute)
  432. (html2text-format-single-elements)
  433. (html2text-fix-paragraphs))))
  434. ;;
  435. ;; </Interactive functions>
  436. ;;
  437. (provide 'html2text)
  438. ;;; html2text.el ends here