shr: Render td content with shr-descend
[gnus] / lisp / html2text.el
index f33b125..6411eb6 100644 (file)
@@ -1,24 +1,23 @@
 ;;; html2text.el --- a simple html to plain text converter
-;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+
+;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: Joakim Hove <hove@phys.ntnu.no>
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr)))
 
 (defvar html2text-replace-list
-  '(("&nbsp;" . " ") ("&gt;" . ">") ("&lt;" . "<") ("&quot;" . "\"")
-    ("&amp;" . "&") ("&apos;" . "'"))
+  '(("&acute;" . "`")
+    ("&amp;" . "&")
+    ("&apos;" . "'")
+    ("&brvbar;" . "|")
+    ("&cent;" . "c")
+    ("&circ;" . "^")
+    ("&copy;" . "(C)")
+    ("&curren;" . "(#)")
+    ("&deg;" . "degree")
+    ("&divide;" . "/")
+    ("&euro;" . "e")
+    ("&frac12;" . "1/2")
+    ("&gt;" . ">")
+    ("&iquest;" . "?")
+    ("&laquo;" . "<<")
+    ("&ldquo" . "\"")
+    ("&lsaquo;" . "(")
+    ("&lsquo;" . "`")
+    ("&lt;" . "<")
+    ("&mdash;" . "--")
+    ("&nbsp;" . " ")
+    ("&ndash;" . "-")
+    ("&permil;" . "%%")
+    ("&plusmn;" . "+-")
+    ("&pound;" . "£")
+    ("&quot;" . "\"")
+    ("&raquo;" . ">>")
+    ("&rdquo" . "\"")
+    ("&reg;" . "(R)")
+    ("&rsaquo;" . ")")
+    ("&rsquo;" . "'")
+    ("&sect;" . "§")
+    ("&sup1;" . "^1")
+    ("&sup2;" . "^2")
+    ("&sup3;" . "^3")
+    ("&tilde;" . "~"))
   "The map of entity to text.
 
 This is an alist were each element is a dotted pair consisting of an
@@ -58,7 +91,7 @@ completely verbatim - without any use of REGEXP.")
 
 This is a list of tags which should be removed, without any
 formatting.  Note that tags in the list are presented *without*
-any \"<\" or \">\".  All occurences of a tag appearing in this
+any \"<\" or \">\".  All occurrences of a tag appearing in this
 list are removed, irrespective of whether it is a closing or
 opening tag, or if the tag has additional attributes.  The
 deletion is done by the function `html2text-remove-tags'.
@@ -144,7 +177,7 @@ formatting, and then moved afterward.")
   "Get value of ATTRIBUTE from LIST."
   (nth 1 (assoc attribute list)))
 
-(defun html2text-get-attr (p1 p2 tag)
+(defun html2text-get-attr (p1 p2)
   (goto-char p1)
   (re-search-forward " +[^ ]" p2 t)
   (let* ((attr-string (buffer-substring-no-properties (1- (point)) (1- p2)))
@@ -214,24 +247,6 @@ formatting, and then moved afterward.")
     ;; return - value
     attr-list))
 
-(defun html2text-get-attr (p1 p2)
-  (save-restriction
-    (narrow-to-region p1 p2)
-    (let (result)
-      (goto-char (point-min))
-      (while (not (eobp))
-       (when (re-search-forward "[^= ]+" nil t)
-         (push
-          (list
-           (match-string 0)
-           (when (looking-at " *= *")
-             (goto-char (match-end 0))
-             (buffer-substring 
-              (point)
-              (goto-char (or (ignore-errors (scan-sexps (point) 1))
-                             (point-max))))))
-          result)))
-      result)))
 ;;
 ;; </Functions related to attributes>
 ;;
@@ -338,7 +353,7 @@ formatting, and then moved afterward.")
   ;; If someone can explain how to make the URL clickable I will surely
   ;; improve upon this.
   ;; Maybe `goto-addr.el' can be used here.
-  (let* ((attr-list (html2text-get-attr p1 p2 "a"))
+  (let* ((attr-list (html2text-get-attr p1 p2))
         (href (html2text-attr-value attr-list "href")))
     (delete-region p1 p4)
     (when href
@@ -368,10 +383,10 @@ formatting, and then moved afterward.")
        (setq refill-start (point))
        (goto-char p2)
        (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t)
-       (next-line 1)
+       (forward-line 1)
        (end-of-line)
        ;; refill-stop should ideally be adjusted to
-       ;; accomodate the "<br>" strings which are removed
+       ;; accommodate the "<br>" strings which are removed
        ;; between refill-start and refill-stop.  Can simply
        ;; be returned from my-replace-string
        (setq refill-stop (+ (point)
@@ -391,7 +406,8 @@ formatting, and then moved afterward.")
 fashion, quite close to pure guess-work. It does work in some cases though."
   (interactive)
   (goto-char (point-min))
-  (replace-regexp "^<br>$" "")
+  (while (re-search-forward "^<br>$" nil t)
+    (delete-region (match-beginning 0) (match-end 0)))
   ;; Removing lonely <br> on a single line, if they are left intact we
   ;; dont have any paragraphs at all.
   (goto-char (point-min))
@@ -435,11 +451,12 @@ See the documentation for that variable."
                                (point-max) t)
        (let ((p1)
              (p2 (point))
-             (p3) (p4)
-             (attr (match-string 0)))
+             (p3) (p4))
          (search-backward "<" (point-min) t)
          (setq p1 (point))
-         (search-forward (format "</%s>" tag) (point-max) t)
+         (unless (search-forward (format "</%s>" tag) (point-max) t)
+           (goto-char p2)
+           (insert (format "</%s>" tag)))
          (setq p4 (point))
          (search-backward "</" (point-min) t)
          (setq p3 (point))
@@ -491,5 +508,5 @@ See the documentation for that variable."
 ;; </Interactive functions>
 ;;
 (provide 'html2text)
-;;; arch-tag: e9e57b79-35d4-4de1-a647-e7e01fe56d1e
+
 ;;; html2text.el ends here