From e5b801c287295e0df6ecde6c91ae715ef8bd4f0a Mon Sep 17 00:00:00 2001 From: ShengHuo ZHU Date: Sat, 23 Feb 2002 21:17:19 +0000 Subject: [PATCH] * mm-view.el (mm-text-html-renderer-alist): Add html2text. (mm-text-html-washer-alist): Ditto. * mm-decode.el (mm-text-html-renderer): Add html2text. * html2text.el: Face lift. --- lisp/ChangeLog | 7 ++ lisp/html2text.el | 234 +++++++++++++++++++++++----------------------- lisp/mm-decode.el | 5 +- lisp/mm-view.el | 7 +- 4 files changed, 133 insertions(+), 120 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index aed652999..8549bc6c9 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,12 @@ 2002-02-23 ShengHuo ZHU + * mm-view.el (mm-text-html-renderer-alist): Add html2text. + (mm-text-html-washer-alist): Ditto. + + * mm-decode.el (mm-text-html-renderer): Add html2text. + + * html2text.el: Face lift. + * html2text.el: New file from Joakim Hove . 2002-02-22 ShengHuo ZHU diff --git a/lisp/html2text.el b/lisp/html2text.el index df2ec0042..f76cb3056 100644 --- a/lisp/html2text.el +++ b/lisp/html2text.el @@ -1,36 +1,67 @@ -;; These functions provide a simple way to wash/clean html infected mails. -;; Definitely do not work in all cases, but some improvement in readability -;; is generally obtained. Formatting is only done in the buffer, so the next -;; time you enter the article it will be "re-htmlized". +;;; html2text.el --- a simple html to plain text converter + +;; Copyright (C) 2002 Free Software Foundation, Inc. + +;; Author: Joakim Hove + +;; This file is part of GNU Emacs. + +;; 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. + +;; 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 +;; 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. + +;;; Commentary: + +;; These functions provide a simple way to wash/clean html infected +;; mails. Definitely do not work in all cases, but some improvement +;; in readability is generally obtained. Formatting is only done in +;; the buffer, so the next time you enter the article it will be +;; "re-htmlized". ;; ;; The main function is "html2text" -;; -;; Joakim Hove - +;;; Code: ;; ;; ;; +(eval-when-compile + (require 'cl)) + (defvar html2text-format-single-element-list '(("hr" . html2text-clean-hr))) -(defvar html2text-replace-list - '((" " . " ") (">" . ">") ("<" . "<") (""" . "\"")) - "This is a alist(?) were each element is a dotted pair consisting of -an old string, and a replacement string. This replacement is done by -the function \"html2text-substitute\" which basically performs a -replace-string operation for every element in the list. This is completely -verbatim - without any use of REGEXP") +(defvar html2text-replace-list + '((" " . " ") (">" . ">") ("<" . "<") (""" . "\"")) + "The map of entity to text. + +This is an alist were each element is a dotted pair consisting of an +old string, and a replacement string. This replacement is done by the +function \"html2text-substitute\" which basically performs a +replace-string operation for every element in the list. This is +completely verbatim - without any use of REGEXP.") -(defvar html2text-remove-tag-list +(defvar html2text-remove-tag-list '("html" "body" "p" "img" "dir" "head" "div" "br" "font" "title" "meta") - "This is a list of tags which should be removed, without any formatting. -Observe that if you the tags in the list are presented *without* any -\"<\" or \">\". All occurences 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 actual deletion is done by the function -\"html2text-remove-tags\". + "A list of removable tags. + +This is a list of tags which should be removed, without any +formatting. Observe that if you the tags in the list are presented +*without* any \"<\" or \">\". All occurences 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 actual +deletion is done by the function \"html2text-remove-tags\". For instance the text: @@ -42,49 +73,49 @@ will be reduced to: If this list contains the element \"font\".") -(defvar html2text-format-tag-list - '(("b" . html2text-clean-bold) - ("u" . html2text-clean-underline) - ("i" . html2text-clean-italic) +(defvar html2text-format-tag-list + '(("b" . html2text-clean-bold) + ("u" . html2text-clean-underline) + ("i" . html2text-clean-italic) ("blockquote" . html2text-clean-blockquote) - ("a" . html2text-clean-anchor) + ("a" . html2text-clean-anchor) ("ul" . html2text-clean-ul) ("ol" . html2text-clean-ol) ("dl" . html2text-clean-dl) ("center" . html2text-clean-center)) - "This is an alist where each dotted pair consists of a tag, and then the name -of a function to be called when this tag is found. The function is called with -the arguments p1, p2, p3 and p4. These are demontrated below: + "An alist of tags and processing functions. + +This is an alist where each dotted pair consists of a tag, and then +the name of a function to be called when this tag is found. The +function is called with the arguments p1, p2, p3 and p4. These are +demontrated below: \" This is bold text \" - ^ ^ ^ ^ + ^ ^ ^ ^ | | | | p1 p2 p3 p4 -Then the called function will typically format the text somewhat and remove the tags.") - - +Then the called function will typically format the text somewhat and +remove the tags.") (defvar html2text-remove-tag-list2 '("li" "dt" "dd" "meta") - "This is a list of tags which are removed similarly to the list -\"html2text-remove-tag-list\" - but these tags are retained for the -formatting, and then moved afterwards.") + "Another list of removable tags. +This is a list of tags which are removed similarly to the list +`html2text-remove-tag-list' - but these tags are retained for the +formatting, and then moved afterward.") ;; ;; ;; - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; ;; ;; - (defun html2text-buffer-head () (if (string= mode-name "Article") (beginning-of-buffer) @@ -92,7 +123,6 @@ formatting, and then moved afterwards.") ) ) - (defun html2text-replace-string (from-string to-string p1 p2) (goto-char p1) (let ((delta (- (string-width to-string) (string-width from-string))) @@ -109,20 +139,16 @@ formatting, and then moved afterwards.") ;; ;; - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; ;; i.e. ;; (defun html2text-attr-value (attr-list attr) (nth 1 (assoc attr attr-list)) -) - - + ) (defun html2text-get-attr (p1 p2 tag) (goto-char p1) @@ -135,7 +161,7 @@ formatting, and then moved afterwards.") (this (nth 1 tmp-list)) (next (nth 2 tmp-list)) (index 1)) - + (cond ;; size=3 ((string-match "[^ ]=[^ ]" prev) @@ -151,7 +177,7 @@ formatting, and then moved afterwards.") ) (while (< index (length tmp-list)) - (cond + (cond ;; size=3 ((string-match "[^ ]=[^ ]" this) (let ((attr (nth 0 (split-string this "="))) @@ -167,19 +193,18 @@ formatting, and then moved afterwards.") ((string-match "[^ ]=\\'" this) (setq attr-list (cons (list (substring this 0 -1) next) attr-list)) ) - + ;; size = 3 ((string= "=" this) (setq attr-list (cons (list prev next) attr-list)) + ) ) - ) (setq index (1+ index)) (setq prev this) (setq this next) (setq next (nth (1+ index) tmp-list)) ) - ;; ;; Tags with no accompanying "=" i.e. value=nil ;; @@ -198,7 +223,8 @@ formatting, and then moved afterwards.") (while (< index (1- (length tmp-list))) (if (not (string-match "=" this)) - (if (not (or (string= (substring next 0 1) "=") (string= (substring prev -1) "="))) + (if (not (or (string= (substring next 0 1) "=") + (string= (substring prev -1) "="))) (setq attr-list (cons (list this nil) attr-list)) ) ) @@ -208,7 +234,7 @@ formatting, and then moved afterwards.") (setq next (nth (1+ index) tmp-list)) ) - (if this + (if this (progn (if (not (string-match "=" this)) (progn @@ -219,19 +245,17 @@ formatting, and then moved afterwards.") ) ) ) - attr-list ;; return - value + attr-list ;; return - value ) -) + ) ;; ;; ;; - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; ;; ;; @@ -248,7 +272,7 @@ formatting, and then moved afterwards.") (cond ((string= list-type "ul") (insert " o ")) ((string= list-type "ol") (insert (format " %s: " item-nr))) - (insert " x ")) + (t (insert " x "))) ) ) ) @@ -285,15 +309,11 @@ formatting, and then moved afterwards.") ) ) ) - - - - (defun html2text-delete-tags (p1 p2 p3 p4) (kill-region p1 p2) (kill-region (- p3 (- p2 p1)) (- p4 (- p2 p1))) -) + ) (defun html2text-delete-single-tag (p1 p2) (kill-region p1 p2) @@ -306,8 +326,6 @@ formatting, and then moved afterwards.") (insert (make-string fill-column ?-)) ) - - (defun html2text-clean-ul (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) (html2text-clean-list-items p1 (- p3 (- p1 p2)) "ul") @@ -321,33 +339,32 @@ formatting, and then moved afterwards.") (defun html2text-clean-dl (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) (html2text-clean-dtdd p1 (- p3 (- p1 p2))) -) + ) (defun html2text-clean-center (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) (center-region p1 (- p3 (- p2 p1))) ) - (defun html2text-clean-bold (p1 p2 p3 p4) (put-text-property p2 p3 'face 'bold) (html2text-delete-tags p1 p2 p3 p4) -) + ) (defun html2text-clean-title (p1 p2 p3 p4) (put-text-property p2 p3 'face 'bold) (html2text-delete-tags p1 p2 p3 p4) -) + ) (defun html2text-clean-underline (p1 p2 p3 p4) (put-text-property p2 p3 'face 'underline) (html2text-delete-tags p1 p2 p3 p4) -) + ) (defun html2text-clean-italic (p1 p2 p3 p4) (put-text-property p2 p3 'face 'italic) (html2text-delete-tags p1 p2 p3 p4) -) + ) (defun html2text-clean-font (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) @@ -355,11 +372,10 @@ formatting, and then moved afterwards.") (defun html2text-clean-blockquote (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) -) - + ) (defun html2text-clean-anchor (p1 p2 p3 p4) - ;; If someone can explain how to make the URL clickable + ;; If someone can explain how to make the URL clickable ;; I will surely improve upon this. (let* ((attr-list (html2text-get-attr p1 p2 "a")) (href (html2text-attr-value attr-list "href"))) @@ -374,11 +390,9 @@ formatting, and then moved afterwards.") ;; ;; - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; ;; ;; @@ -402,11 +416,16 @@ formatting, and then moved afterwards.") (re-search-backward ".+[^<][^b][^r][^>]$" refill-start t) (next-line 1) (end-of-line) - ;; refill-stop should ideally be adjusted to accomodate the "
" strings which are removed - ;; between refill-start and refill-stop. - ;; Can simply be returned from my-replace-string - (setq refill-stop (+ (point) (html2text-replace-string "
" "" refill-start (point)))) - ;;(message "Point = %s refill-stop = %s" (point) refill-stop) (sleep-for 4) + ;; refill-stop should ideally be adjusted to + ;; accomodate the "
" strings which are removed + ;; between refill-start and refill-stop. Can simply + ;; be returned from my-replace-string + (setq refill-stop (+ (point) + (html2text-replace-string + "
" "" + refill-start (point)))) + ;; (message "Point = %s refill-stop = %s" (point) refill-stop) + ;; (sleep-for 4) (fill-region refill-start refill-stop) ) ) @@ -415,7 +434,6 @@ formatting, and then moved afterwards.") ) (html2text-replace-string "
" "" p1 p2) ) - ;; ;; This one is interactive ... @@ -425,9 +443,9 @@ formatting, and then moved afterwards.") fashion, quite close to pure guess-work. It does work in some cases though." (interactive) (html2text-buffer-head) - (replace-regexp "^
$" "") -;; Removing lonely
on a single line, if they are left -;; intact we dont have any paragraphs at all. + (replace-regexp "^
$" "") + ;; Removing lonely
on a single line, if they are left intact we + ;; dont have any paragraphs at all. (html2text-buffer-head) (while (< (point) (point-max)) (let ((p1 (point))) @@ -435,7 +453,7 @@ fashion, quite close to pure guess-work. It does work in some cases though." ;;(message "Kaller fix med p1=%s p2=%s " p1 (1- (point))) (sleep-for 5) (html2text-fix-paragraph p1 (1- (point))) (goto-char p1) - (if (< (point) (point-max)) + (if (< (point) (point-max)) (forward-paragraph 1)) ) ) @@ -445,11 +463,9 @@ fashion, quite close to pure guess-work. It does work in some cases though." ;;
;; - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; ;; ;; @@ -469,8 +485,6 @@ See the documentation for that variable." ) ) - - (defun html2text-format-tags () "See the variable \"html2text-format-tag-list\" for documentation" (interactive) @@ -478,10 +492,11 @@ See the documentation for that variable." (let ((tag (car tag-and-function)) (function (cdr tag-and-function))) (html2text-buffer-head) - (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) (point-max) t) + (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) + (point-max) t) (let ((p1) (p2 (point)) - (p3) (p4) + (p3) (p4) (attr (match-string 1))) (search-backward "<" (point-min) t) (setq p1 (point)) @@ -516,7 +531,8 @@ See the documentation for that variable." (let ((tag (car tag-and-function)) (function (cdr tag-and-function))) (html2text-buffer-head) - (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) (point-max) t) + (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) + (point-max) t) (let ((p1) (p2 (point))) (search-backward "<" (point-min) t) @@ -530,40 +546,24 @@ See the documentation for that variable." ;; ;; Main function -;; +;; + +;;;###autoload (defun html2text () + "Convert HTML to plain text in the current buffer." (interactive) (save-excursion - (let ((org-case-fold-search case-fold-search)) - (setq case-fold-search 't) - (if (string= mode-name "Article") - (toggle-read-only -1) - ) - + (let ((case-fold-search t) + (buffer-read-only)) (html2text-remove-tags html2text-remove-tag-list) (html2text-format-tags) (html2text-remove-tags html2text-remove-tag-list2) (html2text-substitute) (html2text-format-single-elements) - (html2text-fix-paragraphs) - - - (if (string= mode-name "Article") - (toggle-read-only 1) - ) - (setq case-fold-search org-case-fold-search) - - ) - ) - ) + (html2text-fix-paragraphs)))) ;; ;; -;; - - - - - - +;; +;;; html2text.el ends here diff --git a/lisp/mm-decode.el b/lisp/mm-decode.el index be353e827..fdb91319a 100644 --- a/lisp/mm-decode.el +++ b/lisp/mm-decode.el @@ -100,7 +100,8 @@ (cond ((locate-library "w3") 'w3) ((locate-library "w3m") 'w3m) ((executable-find "links") 'links) - ((executable-find "lynx") 'lynx)) + ((executable-find "lynx") 'lynx) + (t 'html2text)) "Render of HTML contents. It is one of defined renderer types, or a rendering function. The defined renderer types are: @@ -108,11 +109,13 @@ The defined renderer types are: `w3m' : using emacs-w3m; `links': using links; `lynx' : using lynx; +`html2text' : using html2text; `nil' : using external viewer." :type '(choice (symbol w3) (symbol w3m) (symbol links) (symbol lynx) + (symbol html2text) (symbol nil) (function)) :version "21.3" diff --git a/lisp/mm-view.el b/lisp/mm-view.el index e2c732ed9..7ba9115b9 100644 --- a/lisp/mm-view.el +++ b/lisp/mm-view.el @@ -34,6 +34,7 @@ (autoload 'vcard-parse-string "vcard") (autoload 'vcard-format-string "vcard") (autoload 'fill-flowed "flow-fill") + (autoload 'html2text "html2text") (unless (fboundp 'diff-mode) (autoload 'diff-mode "diff-mode" "" t nil))) @@ -44,7 +45,8 @@ mm-links-remove-leading-blank "links" "-dump" file) (lynx mm-inline-render-with-stdin nil - "lynx" "-dump" "-force_html" "-stdin")) + "lynx" "-dump" "-force_html" "-stdin") + (html2text mm-inline-render-with-function html2text)) "The attributes of renderer types for text/html.") (defvar mm-text-html-washer-alist @@ -54,7 +56,8 @@ mm-links-remove-leading-blank "links" "-dump" file) (lynx mm-inline-wash-with-stdin nil - "lynx" "-dump" "-force_html" "-stdin")) + "lynx" "-dump" "-force_html" "-stdin") + (html2text html2text)) "The attributes of washer types for text/html.") ;;; Internal variables. -- 2.25.1