1 ;;; html.el --- Semantic details for html files
3 ;;; Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Parse HTML files and organize them in a nice way.
25 ;; Pay attention to anchors, including them in the tag list.
27 ;; Copied from the original semantic-texi.el.
29 ;; ToDo: Find <script> tags, and parse the contents in other
30 ;; parsers, such as javascript, php, shtml, or others.
33 (require 'semantic/format)
35 ;; This is not installed in all versions of Emacs.
36 (require 'sgml-mode) ;; html-mode is in here.
38 (require 'psgml-mode) ;; XEmacs uses psgml, and html-mode is in here.
43 (require 'semantic/ctxt)
44 (require 'semantic/imenu)
47 (defvar semantic-html-super-regex
48 "<\\(h[1-9]\\|title\\|script\\|body\\|a +href\\)\\>"
49 "Regular expression used to find special sections in an HTML file.")
51 (defvar semantic-html-section-list
66 "Alist of sectioning commands and their relative level.")
68 (define-mode-local-override semantic-parse-region
69 html-mode (&rest ignore)
70 "Parse the current html buffer for semantic tags.
71 INGNORE any arguments. Always parse the whole buffer.
72 Each tag returned is of the form:
73 (\"NAME\" section (:members CHILDREN))
76 (mapcar 'semantic-html-expand-tag
77 (semantic-html-parse-headings)))
79 (define-mode-local-override semantic-parse-changes
81 "We can't parse changes for HTML mode right now."
82 (semantic-parse-tree-set-needs-rebuild))
84 (defun semantic-html-expand-tag (tag)
85 "Expand the HTML tag TAG."
86 (let ((chil (semantic-html-components tag)))
88 (semantic-tag-put-attribute
89 tag :members (mapcar 'semantic-html-expand-tag chil)))
90 (car (semantic--tag-expand tag))))
92 (defun semantic-html-components (tag)
93 "Return components belonging to TAG."
94 (semantic-tag-get-attribute tag :members))
96 (defun semantic-html-parse-headings ()
97 "Parse the current html buffer for all semantic tags."
99 ;; First search and snarf.
101 (goto-char (point-min))
103 (let ((semantic--progress-reporter
104 (make-progress-reporter
105 (format "Parsing %s..."
106 (file-name-nondirectory buffer-file-name))
107 (point-min) (point-max))))
108 (while (re-search-forward semantic-html-super-regex nil t)
109 (setq pass1 (cons (match-beginning 0) pass1))
110 (progress-reporter-update semantic--progress-reporter (point)))
111 (progress-reporter-done semantic--progress-reporter)))
113 (setq pass1 (nreverse pass1))
114 ;; Now, make some tags while creating a set of children.
115 (car (semantic-html-recursive-combobulate-list pass1 0))
118 (defun semantic-html-set-endpoint (metataglist pnt)
119 "Set the end point of the first section tag in METATAGLIST to PNT.
120 METATAGLIST is a list of tags in the intermediate tag format used by the
121 html parser. PNT is the new point to set."
123 (while (and metataglist
124 (not (eq (semantic-tag-class (car metataglist)) 'section)))
125 (setq metataglist (cdr metataglist)))
126 (setq metatag (car metataglist))
128 (setcar (nthcdr (1- (length metatag)) metatag) pnt)
131 (defsubst semantic-html-new-section-tag (name members level start end)
132 "Create a semantic tag of class section.
133 NAME is the name of this section.
134 MEMBERS is a list of semantic tags representing the elements that make
136 LEVEL is the levelling level.
137 START and END define the location of data described by the tag."
138 (let ((anchorp (eq level 11)))
139 (append (semantic-tag name
140 (cond (anchorp 'anchor)
143 (list start (if anchorp (point) end)) )))
145 (defun semantic-html-extract-section-name ()
146 "Extract a section name from the current buffer and point.
147 Assume the cursor is in the tag representing the section we
150 ; Skip over the HTML tag.
154 (skip-chars-forward "\n\t ")
155 (while (looking-at "<")
157 (skip-chars-forward "\n\t ")
159 (let ((start (point))
161 (if (re-search-forward "</" nil t)
163 (goto-char (match-beginning 0))
164 (skip-chars-backward " \n\t")
166 (buffer-substring-no-properties start end))
170 (defun semantic-html-recursive-combobulate-list (sectionlist level)
171 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
172 Return the rearranged new list, with all remaining tags from
173 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
174 tag with greater section value than LEVEL is found."
183 (goto-char (car oldl))
184 (if (looking-at "<\\(\\w+\\)")
185 (let* ((word (match-string 1))
186 (levelmatch (assoc-ignore-case
187 word semantic-html-section-list))
190 (when (not levelmatch)
191 (error "Tag %s matched in regexp but is not in list"
193 ;; Set begin to the right location
195 ;; Get out of here if there if we made it that far.
196 (if (and levelmatch (<= (car (cdr levelmatch)) level))
199 (semantic-html-set-endpoint newl begin))
200 (throw 'level-jump t)))
201 ;; When there is a match, the descriptive text
202 ;; consists of the rest of the line.
203 (goto-char (match-end 1))
204 (skip-chars-forward " \t")
205 (setq text (semantic-html-extract-section-name))
206 ;; Next, recurse into the body to find the end.
207 (setq tmp (semantic-html-recursive-combobulate-list
208 (cdr oldl) (car (cdr levelmatch))))
210 (setq tag (semantic-html-new-section-tag
211 text (car tmp) (car (cdr levelmatch)) begin (point-max)))
212 ;; Before appending the newtag, update the previous tag
213 ;; if it is a section tag.
215 (semantic-html-set-endpoint newl begin))
216 ;; Append new tag to our master list.
217 (setq newl (cons tag newl))
219 (setq oldl (cdr tmp))
221 (error "Problem finding section in semantic/html parser"))
222 ;; (setq oldl (cdr oldl))
225 (cons (nreverse newl) oldl)))
227 (define-mode-local-override semantic-sb-tag-children-to-expand
229 "The children TAG expands to."
230 (semantic-html-components tag))
232 (defun semantic-default-html-setup ()
233 "Set up a buffer for parsing of HTML files."
234 ;; This will use our parser.
235 (setq semantic-parser-name "HTML"
236 semantic--parse-table t
237 imenu-create-index-function 'semantic-create-imenu-index
238 semantic-command-separation-character ">"
239 semantic-type-relation-separator-character '(":")
240 semantic-symbol->name-assoc-list '((section . "Section")
243 semantic-imenu-expandable-tag-classes '(section)
244 semantic-imenu-bucketize-file nil
245 semantic-imenu-bucketize-type-members nil
246 senator-step-at-start-end-tag-classes '(section)
247 semantic-stickyfunc-sticky-classes '(section)
249 (semantic-install-function-overrides
250 '((tag-components . semantic-html-components)
255 (add-hook 'html-mode-hook 'semantic-default-html-setup)
257 (define-child-mode html-helper-mode html-mode
258 "`html-helper-mode' needs the same semantic support as `html-mode'.")
260 (provide 'semantic/html)
262 ;;; semantic-html.el ends here