;;; html-pagetoc.el --- Insert/rebuild table of contents for html page ;; ;; Author: Lennart Borgman (lennart O borgman A gmail O com) ;; Created: 2005-08-03 ;; Last-Updated: Sat Apr 21 14:11:13 2007 (7200 +0200) (defconst html-pagetoc:version "0.85") ;; Version: ;; Keywords: tools hypermedia html ;; Features that might be required by this library: ;; ;; None ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; This file is not part of Emacs ;; This program 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. ;; This program 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 this program; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;;; Commentary: ;; html-pagetoc.el has functions for building (and rebuilding) a ;; simple table of contents for a single html file. It is supposed to ;; be a quick tool for this. The table of contents are made from the ;; header tags (H1, H2, H3 etc). If you have ID attributes on the ;; header the table of contents will have links to those. Otherwise it ;; is just text. ;; To use this module put it in emacs load-path and enter the line ;; below in your .emacs: ;; ;; (require 'html-pagetoc) ;; ;; When editing a html file put your cursor where you want the table ;; of contents and do M-x html-pagetoc-insert-toc. ;; ;; To rebuild the table of contents use M-x html-pagetoc-rebuild-toc. ;; If you want to add styles to it you can use M-x ;; html-pagetoc-insert-style-guide. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;;; Code: ;;(define-key global-map [f2] 'eval-buffer) ;;(define-key global-map [f3] 'html-pagetoc-insert-toc) ;;;###autoload (defgroup html-pagetoc nil "Html page local table of contents settings" :group 'nxhtml :group 'hypermedia) (defcustom html-pagetoc-tocheads '( ("" . "On THIS Page:") ) "Head titles for table of contents. The titles are put above the table of contents. The value of this variable should be a list of cons cells where the car is a regexp to match against file names and the cdr is the head title to use. The first match in the list is used. If there is no match then no head title is inserted." :type '(repeat (cons regexp string)) :group 'html-pagetoc) (defcustom html-pagetoc-min 1 "Default for min header level" :type 'integer :group 'html-pagetoc) (make-variable-buffer-local 'html-pagetoc-min) (defcustom html-pagetoc-max 3 "Default for max header level" :type 'integer :group 'html-pagetoc) (make-variable-buffer-local 'html-pagetoc-max) (defconst html-pagetoc-begin-cmnt "\n") (defconst html-pagetoc-end-cmnt "\n") (defconst html-pagetoc-maxmin-cmnt "\n") ;;(defconst html-pagetoc-buffers nil) (defun html-pagetoc-get-title (filename) "Find the head title for filename. See `html-pagetoc-tocheads'." (when filename (let ((ths html-pagetoc-tocheads) th re header) (while (and ths (not header)) (setq th (car ths)) (setq ths (cdr ths)) (setq re (car th)) (when (string-match re filename) (setq header (cdr th)))) header))) ;;;###autoload (defun html-pagetoc-insert-toc (&optional min-level max-level) "Inserts a table of contents for the current html file. The html header tags h1-h6 found in the file are inserted into this table. MIN-LEVEL and MAX-LEVEL specifies the minimum and maximum level of h1-h6 to include. They should be integers." (interactive (let* ((maxstr) (max 0) (min 1) (prmax (format "Max header level (%s): " html-pagetoc-max)) (prmax2 (concat "Please give an integer 1-5. " prmax)) (prmin "Include header level 1? ") ) (while (= max 0) (setq maxstr (read-string prmax)) (if (equal maxstr "") (setq max html-pagetoc-max) (when (not (string-match "\\." maxstr)) (setq max (string-to-number maxstr)) )) (when (> max 5) (setq max 0)) (when (< max 0) (setq max 0)) (setq prmax prmax2) ) (when (> max 1) (when (not (y-or-n-p prmin)) (setq min 2))) (list min max))) (let* ((curr-buffer (current-buffer)) (header (html-pagetoc-get-title (buffer-file-name))) (toc-buffer (get-buffer-create "*html-pagetoc*")) (toc) (buffer-val (cons (buffer-file-name) (list min-level max-level))) ) (setq html-pagetoc-min min-level) (setq html-pagetoc-max max-level) (with-current-buffer toc-buffer (erase-buffer)) (with-temp-buffer (insert-buffer-substring curr-buffer) ;;(replace-regexp "" "") (save-excursion (goto-char (point-min)) (while (re-search-forward "" nil t) (replace-match "" nil nil)) (goto-char (point-min)) (let ((b (current-buffer)) (standard-output toc-buffer) (level (- min-level 1)) (skip-level (- min-level 1)) (prev-level) ) (princ html-pagetoc-begin-cmnt) (princ (format html-pagetoc-maxmin-cmnt min-level max-level)) (princ "
\n")
(when header
(princ "")
(princ header)
(princ "\n"))
(while (re-search-forward
(concat "\\(?:
|