1 ;;; semantic-html.el --- Semantic details for html files
3 ;;; Copyright (C) 2004, 2005, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; X-RCS: $Id: semantic-html.el,v 1.1 2007-11-26 15:10:38 michaels Exp $
8 ;; This file is not part of GNU Emacs.
10 ;; This is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This software is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Parse HTML files and organize them in a nice way.
28 ;; Pay attention to anchors, including them in the tag list.
30 ;; Copied from the original semantic-texi.el.
32 ;; ToDo: Find <script> tags, and parse the contents in other
33 ;; parsers, such as javascript, php, shtml, or others.
36 (require 'semantic-format)
37 (require 'sgml-mode) ;; html-mode is in here.
41 (require 'semantic-ctxt)
42 (require 'semantic-imenu)
45 (defvar semantic-html-super-regex
46 "<\\(h[1-9]\\|title\\|script\\|body\\|a +href\\)\\>"
47 "Regular expression used to find special sections in an HTML file.")
49 (defvar semantic-html-section-list
64 "Alist of sectioning commands and their relative level.")
66 (define-mode-local-override semantic-parse-region
67 html-mode (&rest ignore)
68 "Parse the current html buffer for semantic tags.
69 INGNORE any arguments. Always parse the whole buffer.
70 Each tag returned is of the form:
71 (\"NAME\" section (:members CHILDREN))
74 (mapcar 'semantic-html-expand-tag
75 (semantic-html-parse-headings)))
77 (define-mode-local-override semantic-parse-changes
79 "We can't parse changes for HTML mode right now."
80 (semantic-parse-tree-set-needs-rebuild))
82 (defun semantic-html-expand-tag (tag)
83 "Expand the HTML tag TAG."
84 (let ((chil (semantic-html-components tag)))
86 (semantic-tag-put-attribute
87 tag :members (mapcar 'semantic-html-expand-tag chil)))
88 (car (semantic--tag-expand tag))))
90 (defun semantic-html-components (tag)
91 "Return components belonging to TAG."
92 (semantic-tag-get-attribute tag :members))
94 (defun semantic-html-parse-headings ()
95 "Parse the current html buffer for all semantic tags."
97 ;; First search and snarf.
99 (goto-char (point-min))
100 (working-status-forms (file-name-nondirectory buffer-file-name) "done"
101 (while (re-search-forward semantic-html-super-regex nil t)
102 (setq pass1 (cons (match-beginning 0) pass1))
106 (setq pass1 (nreverse pass1))
107 ;; Now, make some tags while creating a set of children.
108 (car (semantic-html-recursive-combobulate-list pass1 0))
111 (defun semantic-html-set-endpoint (metataglist pnt)
112 "Set the end point of the first section tag in METATAGLIST to PNT.
113 METATAGLIST is a list of tags in the intermediate tag format used by the
114 html parser. PNT is the new point to set."
116 (while (and metataglist
117 (not (eq (semantic-tag-class (car metataglist)) 'section)))
118 (setq metataglist (cdr metataglist)))
119 (setq metatag (car metataglist))
121 (setcar (nthcdr (1- (length metatag)) metatag) pnt)
124 (defsubst semantic-html-new-section-tag (name members start end)
125 "Create a semantic tag of class section.
126 NAME is the name of this section.
127 MEMBERS is a list of semantic tags representing the elements that make
129 START and END define the location of data described by the tag."
130 (append (semantic-tag name 'section :members members)
133 (defun semantic-html-extract-section-name ()
134 "Extract a section name from the current buffer and point.
135 Assume the cursor is in the tag representing the section we
138 ; Skip over the HTML tag.
142 (skip-chars-forward "\n\t ")
143 (while (looking-at "<")
145 (skip-chars-forward "\n\t ")
147 (let ((start (point))
149 (if (re-search-forward "</" nil t)
151 (goto-char (match-beginning 0))
152 (skip-chars-backward " \n\t")
154 (buffer-substring-no-properties start end))
158 (defun semantic-html-recursive-combobulate-list (sectionlist level)
159 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
160 Return the rearranged new list, with all remaining tags from
161 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
162 tag with greater section value than LEVEL is found."
171 (goto-char (car oldl))
172 (if (looking-at "<\\(\\w+\\)")
173 (let* ((word (match-string 1))
174 (levelmatch (assoc-ignore-case
175 word semantic-html-section-list))
178 (when (not levelmatch)
179 (error "Tag %s matched in regexp but is not in list"
181 ;; Set begin to the right location
183 ;; Get out of here if there if we made it that far.
184 (if (and levelmatch (<= (car (cdr levelmatch)) level))
187 (semantic-html-set-endpoint newl begin))
188 (throw 'level-jump t)))
189 ;; When there is a match, the descriptive text
190 ;; consists of the rest of the line.
191 (goto-char (match-end 1))
192 (skip-chars-forward " \t")
193 (setq text (semantic-html-extract-section-name))
194 ;; Next, recurse into the body to find the end.
195 (setq tmp (semantic-html-recursive-combobulate-list
196 (cdr oldl) (car (cdr levelmatch))))
198 (setq tag (semantic-html-new-section-tag
199 text (car tmp) begin (point)))
200 ;; Before appending the newtag, update the previous tag
201 ;; if it is a section tag.
203 (semantic-html-set-endpoint newl begin))
204 ;; Append new tag to our master list.
205 (setq newl (cons tag newl))
207 (setq oldl (cdr tmp))
209 (error "Problem finding section in semantic/html parser"))
210 ;; (setq oldl (cdr oldl))
212 (cons (nreverse newl) oldl)))
214 (define-mode-local-override semantic-sb-tag-children-to-expand
216 "The children TAG expands to."
217 (semantic-html-components tag))
220 (defun semantic-default-html-setup ()
221 "Set up a buffer for parsing of HTML files."
222 ;; This will use our parser.
223 (setq semantic-parser-name "HTML"
224 semantic--parse-table t
225 imenu-create-index-function 'semantic-create-imenu-index
226 semantic-command-separation-character ">"
227 semantic-type-relation-separator-character '(":")
228 semantic-symbol->name-assoc-list '((section . "Section")
231 semantic-imenu-expandable-tag-classes 'section
232 semantic-imenu-bucketize-file nil
233 semantic-imenu-bucketize-type-members nil
234 senator-step-at-start-end-tag-classes '(section)
235 semantic-stickyfunc-sticky-classes '(section)
237 (semantic-install-function-overrides
238 '((tag-components . semantic-html-components)
244 (add-hook 'html-mode-hook 'semantic-default-html-setup)
246 (define-child-mode html-helper-mode html-mode
247 "`html-helper-mode' needs the same semantic support as `html-mode'.")
249 (provide 'semantic-html)
251 ;;; semantic-html.el ends here