SiteMap / AllPages / Out

HtmlShowToc

This is a little elisp function to display a table of contents for large HTML files. It's kind of like M-x occur RET </h[1-6]> RET, only nicer. This is for the times I write HTML directly with TheOneAndOnly editor instead of using WikiMode.

    (defun html-show-toc ()
      "Shows a TOC based on headings tags &lt;H[1-6]&gt;"
      (interactive)
      (if sgml-tags-invisible
          (error "SGML tags are invisible")
        (occur "&lt;/h[1-6]&gt;")
        (pop-to-buffer "*Occur*")
        (let ((inhibit-read-only t))
          (goto-char (point-min))
          (replace-regexp "&lt;h[1-6]&gt;" "")
          (goto-char (point-min))
          (replace-regexp "&lt;a name.*&gt;&lt;/a&gt;" "")
          (goto-char (point-min))
          (while (search-forward-regexp "^ *[0-9]*:\\(.*\\)&lt;/h\\([1-6]\\)&gt;" nil t)
            (replace-match (concat
                            (make-string (* 2 (string-to-number (match-string 2))) ? )
                            (match-string 1))))
          (goto-char (point-min))
          (let ((last (point-min))
                (pt))
            (while (search-forward-regexp "\n" nil t)
              (setq pt (1- (point)))
              (put-text-property (1+ last) pt 'occur
                                 (get-text-property pt 'occur))
              (setq last pt)))
          (goto-char (point-min)))))

The reason for choosing the closing tags instead of the opening tags is that in my own HTML files, there are often several lines of name tags between the opening header tag and the actual header text; the closing tag, on the other hand, is usually on the same line as the actual header text.


SiteMap / AllPages / Out / kensanata@gmail.com / Last change: 2001-03-05