From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.emacs.gnus.general/34492 Path: main.gmane.org!not-for-mail From: Joakim Hove Newsgroups: gmane.emacs.gnus.general Subject: htmlified mail - a simple lisp "solution" Date: 31 Jan 2001 15:39:32 +0100 Sender: owner-ding@hpc.uh.edu Message-ID: NNTP-Posting-Host: coloc-standby.netfonds.no Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: main.gmane.org 1035170410 30718 80.91.224.250 (21 Oct 2002 03:20:10 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 21 Oct 2002 03:20:10 +0000 (UTC) Return-Path: Original-Received: from karazm.math.uh.edu (karazm.math.uh.edu [129.7.128.1]) by mailhost.sclp.com (Postfix) with ESMTP id 46248D049D for ; Wed, 31 Jan 2001 09:42:27 -0500 (EST) Original-Received: from sina.hpc.uh.edu (lists@Sina.HPC.UH.EDU [129.7.3.5]) by karazm.math.uh.edu (8.9.3/8.9.3) with ESMTP id IAC29068; Wed, 31 Jan 2001 08:40:05 -0600 (CST) Original-Received: by sina.hpc.uh.edu (TLB v0.09a (1.20 tibbs 1996/10/09 22:03:07)); Wed, 31 Jan 2001 08:39:24 -0600 (CST) Original-Received: from mailhost.sclp.com (postfix@66-209.196.61.interliant.com [209.196.61.66] (may be forged)) by sina.hpc.uh.edu (8.9.3/8.9.3) with ESMTP id IAA19514 for ; Wed, 31 Jan 2001 08:39:09 -0600 (CST) Original-Received: from solan.itea.ntnu.no (solan.itea.ntnu.no [129.241.190.100]) by mailhost.sclp.com (Postfix) with ESMTP id BABCCD049D for ; Wed, 31 Jan 2001 09:39:34 -0500 (EST) Original-Received: from metropolis.phys.ntnu.no.phys.ntnu.nu (IDENT:hove@metropolis.phys.ntnu.no [129.241.48.32]) by solan.itea.ntnu.no (8.11.1/8.11.1) with ESMTP id f0VEdXm05529 for ; Wed, 31 Jan 2001 15:39:33 +0100 (MET) Original-To: ding@gnus.org User-Agent: Gnus/5.0807 (Gnus v5.8.7) Emacs/20.5 Precedence: list X-Majordomo: 1.94.jlt7 Original-Lines: 31 Xref: main.gmane.org gmane.emacs.gnus.general:34492 X-Report-Spam: http://spam.gmane.org/gmane.emacs.gnus.general:34492 --=-=-= Hello, I have written a very simple solution to convert html formatted mails to pure text, presevering *some* of the markup. It does of course not handle general html, but the readability of the typical eudora mail I receive is greatly enhanced. It works by modifying the buffer content, not the actual message, so by pressing "g" you will get back the original view. [Might be a wise thing to do before you take any serious action based on the output from this program!] Usage: ------ 1. Load the file (with autoload or manually) 2. When you get one of these htmlified mails, type M-x html2text. 3. It is quite simple to alter the behaviour by providing new functions, and modifying the lists of tags to react upon. Changes and modifications are most welcome - Joakim Hove --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=html2text-0.1.el Content-Description: html2text ;; ;; 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 hove@phys.ntnu.no ;; ;; ;; ;; (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-remove-tag-list '("html" "body") "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: \"Here comes something big .\" will be reduced to: \"Here comes something big.\" If this list contains the element \"font\".") (defvar html2text-format-tag-list '(("b" . html2text-clean-bold) ("u" . html2text-clean-underline) ("i" . html2text-clean-italic) ("font" . html2text-clean-font) ("blockquote" . html2text-clean-blockquote) ("a" . html2text-clean-anchor)) "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.") ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; ;; (defun html2text-buffer-head () (if (string= mode-name "Article") (beginning-of-buffer) ;;(message-goto-body) (beginning-of-buffer) ) ) (defun html2text-replace-string (from-string to-string p1 p2) (goto-char p1) (let ((delta (- (string-width to-string) (string-width from-string))) (change 0)) (while (search-forward from-string p2 t) (replace-match to-string) (setq change (+ change delta)) ) change ) ) ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; 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) (re-search-forward " +[^ ]" p2 t) (let* ((attr-string (buffer-substring-no-properties (1- (point)) (1- p2))) (tmp-list (split-string attr-string)) (attr-list) (counter 0) (prev (car tmp-list)) (this (nth 1 tmp-list)) (next (nth 2 tmp-list)) (index 1)) (cond ;; size=3 ((string-match "[^ ]=[^ ]" prev) (let ((attr (nth 0 (split-string prev "="))) (value (nth 1 (split-string prev "=")))) (setq attr-list (cons (list attr value) attr-list)) ) ) ;; size= 3 ((string-match "[^ ]=\\'" prev) (setq attr-list (cons (list (substring prev 0 -1) this) attr-list)) ) ) (while (< index (length tmp-list)) (cond ;; size=3 ((string-match "[^ ]=[^ ]" this) (let ((attr (nth 0 (split-string this "="))) (value (nth 1 (split-string this "=")))) (setq attr-list (cons (list attr value) attr-list)) ) ) ;; size =3 ((string-match "\\`=[^ ]" this) (setq attr-list (cons (list prev (substring this 1)) attr-list))) ;; size= 3 ((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 ;; (setq prev (car tmp-list)) (setq this (nth 1 tmp-list)) (setq next (nth 2 tmp-list)) (setq index 1) (if (not (string-match "=" prev)) (progn (if (not (string= (substring this 0 1) "=")) (setq attr-list (cons (list prev nil) attr-list)) ) ) ) (while (< index (1- (length tmp-list))) (if (not (string-match "=" this)) (if (not (or (string= (substring next 0 1) "=") (string= (substring prev -1) "="))) (setq attr-list (cons (list this nil) attr-list)) ) ) (setq index (1+ index)) (setq prev this) (setq this next) (setq next (nth (1+ index) tmp-list)) ) (if this (progn (if (not (string-match "=" this)) (progn (if (not (string= (substring prev -1) "=")) (setq attr-list (cons (list this nil) attr-list)) ) ) ) ) ) attr-list ;; return - value ) ) ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; ;; (defun html2text-delete-tags (p1 p2 p3 p4) (kill-region p1 p2) (kill-region (- p3 (- p2 p1)) (- p4 (- 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-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) ) (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 ;; I will surely improve upon this. (let* ((attr-list (html2text-get-attr p1 p2 "a")) (href (html2text-attr-value attr-list "href"))) (kill-region p1 p4) (goto-char p1) (insert (substring href 1 -1 )) (put-text-property p1 (point) 'face 'bold) ) ) ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; ;; (defun html2text-fix-paragraph (p1 p2) (goto-char p1) (let ((has-br-line) (refill-start) (refill-stop)) (if (re-search-forward "
$" p2 t) (setq has-br-line t) ) (if has-br-line (progn (goto-char p1) (if (re-search-forward ".+[^<][^b][^r][^>]$" p2 t) (progn (beginning-of-line) (setq refill-start (point)) (goto-char p2) (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) (fill-region refill-start refill-stop) ) ) ) ) ) (html2text-replace-string "
" "" p1 p2) ) ;; ;; This one is interactive ... ;; (defun html2text-fix-paragraphs () "This _tries_ to fix up the paragraphs - this is done in quite a ad-hook 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. (html2text-buffer-head) (while (< (point) (point-max)) (let ((p1 (point))) (forward-paragraph 1) ;;(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)) (forward-paragraph 1)) ) ) ) ;; ;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; ;; (defun html2text-remove-tags () "Removes the tags listed in the list \"html2text-remove-tag-list\". See the documentation for that variable." (interactive) (dolist (tag html2text-remove-tag-list) (html2text-buffer-head) (while (re-search-forward (format "\\(]*>\\)" tag) (point-max) t) (let ((p1 (point))) (search-backward "<") (kill-region (point) p1) ) ) ) ) (defun html2text-format-tags () "See the variable \"html2text-format-tag-list\" for documentation" (interactive) (dolist (tag-and-function html2text-format-tag-list) (let ((tag (car tag-and-function)) (function (cdr tag-and-function))) (html2text-buffer-head) (while (re-search-forward (format "\\(<%s\\( [^>]*\\)?>\\)" tag) (point-max) t) (let ((p1) (p2 (point)) (p3) (p4) (attr (match-string 1))) (search-backward "<" (point-min) t) (setq p1 (point)) (re-search-forward (format "" tag) (point-max) t) (setq p4 (point)) (search-backward " ;; --=-=-= Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable --=20 =3D=3D=3D=3D Joakim Hove www.phys.ntnu.no/~hove/ =3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D || Institutt for fysikk (735) 93637 / E3-166 | Sk=F8yensgate 10D || || N - 7491 Trondheim hove@phys.ntnu.no | N - 7030 Trondheim || =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 73= 93 31 68 =3D=3D=3D=3D=3D=3D=3D=3D=3D --=-=-=--