Newsgroups: gnu.emacs.sources Subject: text-to-html.el 0.1.0 (alpha) From: Marco Parrone Organization: individual Message-ID: <87el0pm4gc.fsf@autistici.org> Date: Thu, 17 Jul 2003 17:49:54 GMT ;;; text-to-html.el --- Plain-text to HTML converter. ;; Copyright (C) 2003 Marco Parrone. ;; All rights reserved. ;; Filename: text-to-html.el ;; Version: 0.1.0 (alpha) ;; Updated: 2003-07-17 ;; Keywords: text2html, plain-text, html, converter ;; Author: Marco Parrone ;; Maintainer: Marco Parrone ;; Description: Plain-text to HTML converter. ;; Language: Emacs Lisp ;; Compatibility: Emacs 21 ;; Location: http://www.autistici.org/marc0/elisp/text-to-html.el ;; Redistribution and use in source and binary forms, with or without ;; modification, are permitted provided that the following conditions ;; are met: ;; 1. Redistributions of source code must retain the above copyright ;; notice, this list of conditions and the following disclaimer. ;; 2. Redistributions in binary form must reproduce the above copyright ;; notice, this list of conditions and the following disclaimer in the ;; documentation and/or other materials provided with the distribution. ;; 3. Neither the name of the author nor the names of its contributors ;; may be used to endorse or promote products derived from this software ;; without specific prior written permission. ;; ;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ;; SUCH DAMAGE. ;;; Commentary: ;; text-to-html.el is a script to convert a portion of text from ;; plain-text to HTML. ;; ;; The conversion consists in matching the URLs and replacing them ;; with HTML links. ;; ;; Another conversion is to start a paragraph where there are two ;; consecutive newlines without non-white characters in the middle. ;; ;; The rest is to add the tag `
' to newlines, and to make words ;; tagged for *bold* in bold, and the ones tagged for being ;; _underlined_ will be made underlined. ;; ;; In addition, using `text-to-html-on-buffer-with-template', two ;; template snip of HTML will be added, one at the top of the page and ;; one at the bottom, to make a valid HTML page. ;; ;; This is for texts that were not intended to be an HTML page, so it ;; does not supports special markups. ;; ;; The code is very generalized, and using customization the program ;; can be used to do any number of replacements using regular ;; expressions, and the top and bottom templates can be changed too, ;; so it's possible to use the program for conversions not related to ;; the text-to-html one. ;;; Code: (defgroup text-to-html nil "Plain-text to HTML converter." :prefix "text-to-html-" :group 'editing) (defcustom text-to-html-top-template " \n text-to-html output\n\n\n" "*Template to add at the top of the buffer, used by `text-to-html-on-buffer-with-template'." :type '(string) :group 'text-to-html) (defcustom text-to-html-bottom-template "\n\n" "*Template to add at the bottom of the buffer, used by `text-to-html-on-buffer-with-template'." :type '(string) :group 'text-to-html) (defcustom text-to-html-replacements-table '(("\\(\\(http\\|ftp\\|https\\|ftps\\|mailto\\|gopher\\)://\\([[:alnum:]]\\|\\.\\|_\\|-\\|/\\|\\?\\|=\\|~\\|%\\|\\)*\\)" . "\\1") ("\n[ \t\n]*\n" . "\n

") ("\n" . "
\n") ("\\*\\([^ \t\n]*\\)\\*" . "\\1") ("_\\([^ \t\n]*\\)_" . "\\1")) "*Matching and replacing rules (list of pairs of strings, use regular expressions)." :type '(sexp) :group 'text-to-html) (defun text-to-html-on-region (start end) "Convert the plain-text in a region to HTML format." (interactive "r") (dolist (replacement-description text-to-html-replacements-table) (goto-char (if start start 0)) (while (re-search-forward (car replacement-description) nil t) (replace-match (cdr replacement-description))))) (defun text-to-html-on-buffer () "Convert the plain-text in the buffer to HTML format." (interactive) (goto-char 0) (text-to-html-on-region nil nil)) (defun text-to-html-on-buffer-with-template () "Convert the plain-text in the buffer to HTML format, adding some template HTML markup at the top and at the bottom of the page, so to make the buffer a valid HTML page." (interactive) (goto-char 0) (text-to-html-on-region nil nil) (goto-char 0) (insert text-to-html-top-template) (goto-char (point-max)) (insert text-to-html-bottom-template)) ;; Local Variables: ;; mode: emacs-lisp ;; mode: auto-fill ;; fill-column: 70 ;; comment-column: 32 ;; End: ;;;; text-to-html.el ends here.