1 ;;; semantic/html.el --- Semantic details for html files
3 ;; Copyright (C) 2004-2005, 2007-2016 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.
35 (require 'semantic/format)
38 (defvar semantic-command-separation-character)
40 (defvar semantic-html-super-regex
41 "<\\(h[1-9]\\|title\\|script\\|body\\|a +href\\)\\>"
42 "Regular expression used to find special sections in an HTML file.")
44 (defvar semantic-html-section-list
59 "Alist of sectioning commands and their relative level.")
61 (define-mode-local-override semantic-parse-region
62 html-mode (&rest ignore)
63 "Parse the current html buffer for semantic tags.
64 IGNORE any arguments. Always parse the whole buffer.
65 Each tag returned is of the form:
66 (\"NAME\" section (:members CHILDREN))
69 (mapcar 'semantic-html-expand-tag
70 (semantic-html-parse-headings)))
72 (define-mode-local-override semantic-parse-changes
74 "We can't parse changes for HTML mode right now."
75 (semantic-parse-tree-set-needs-rebuild))
77 (defun semantic-html-expand-tag (tag)
78 "Expand the HTML tag TAG."
79 (let ((chil (semantic-html-components tag)))
81 (semantic-tag-put-attribute
82 tag :members (mapcar 'semantic-html-expand-tag chil)))
83 (car (semantic--tag-expand tag))))
85 (defun semantic-html-components (tag)
86 "Return components belonging to TAG."
87 (semantic-tag-get-attribute tag :members))
89 (defun semantic-html-parse-headings ()
90 "Parse the current html buffer for all semantic tags."
92 ;; First search and snarf.
94 (goto-char (point-min))
96 (let ((semantic--progress-reporter
97 (make-progress-reporter
98 (format "Parsing %s..."
99 (file-name-nondirectory buffer-file-name))
100 (point-min) (point-max))))
101 (while (re-search-forward semantic-html-super-regex nil t)
102 (setq pass1 (cons (match-beginning 0) pass1))
103 (progress-reporter-update semantic--progress-reporter (point)))
104 (progress-reporter-done semantic--progress-reporter)))
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 level 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 LEVEL is the leveling level.
130 START and END define the location of data described by the tag."
131 (let ((anchorp (eq level 11)))
132 (append (semantic-tag name
133 (cond (anchorp 'anchor)
136 (list start (if anchorp (point) end)) )))
138 (defun semantic-html-extract-section-name ()
139 "Extract a section name from the current buffer and point.
140 Assume the cursor is in the tag representing the section we
143 ; Skip over the HTML tag.
147 (skip-chars-forward "\n\t ")
148 (while (looking-at "<")
150 (skip-chars-forward "\n\t ")
152 (let ((start (point))
154 (if (re-search-forward "</" nil t)
156 (goto-char (match-beginning 0))
157 (skip-chars-backward " \n\t")
159 (buffer-substring-no-properties start end))
163 (defun semantic-html-recursive-combobulate-list (sectionlist level)
164 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
165 Return the rearranged new list, with all remaining tags from
166 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
167 tag with greater section value than LEVEL is found."
176 (goto-char (car oldl))
177 (if (looking-at "<\\(\\w+\\)")
178 (let* ((word (match-string 1))
179 (levelmatch (assoc-string
180 word semantic-html-section-list t))
183 (when (not levelmatch)
184 (error "Tag %s matched in regexp but is not in list"
186 ;; Set begin to the right location
188 ;; Get out of here if there if we made it that far.
189 (if (and levelmatch (<= (car (cdr levelmatch)) level))
192 (semantic-html-set-endpoint newl begin))
193 (throw 'level-jump t)))
194 ;; When there is a match, the descriptive text
195 ;; consists of the rest of the line.
196 (goto-char (match-end 1))
197 (skip-chars-forward " \t")
198 (setq text (semantic-html-extract-section-name))
199 ;; Next, recurse into the body to find the end.
200 (setq tmp (semantic-html-recursive-combobulate-list
201 (cdr oldl) (car (cdr levelmatch))))
203 (setq tag (semantic-html-new-section-tag
204 text (car tmp) (car (cdr levelmatch)) begin (point-max)))
205 ;; Before appending the newtag, update the previous tag
206 ;; if it is a section tag.
208 (semantic-html-set-endpoint newl begin))
209 ;; Append new tag to our master list.
210 (setq newl (cons tag newl))
212 (setq oldl (cdr tmp))
214 (error "Problem finding section in semantic/html parser"))
215 ;; (setq oldl (cdr oldl))
218 (cons (nreverse newl) oldl)))
220 (define-mode-local-override semantic-sb-tag-children-to-expand
222 "The children TAG expands to."
223 (semantic-html-components tag))
225 ;; In semantic/imenu.el, not part of Emacs.
226 (defvar semantic-imenu-expandable-tag-classes)
227 (defvar semantic-imenu-bucketize-file)
228 (defvar semantic-imenu-bucketize-type-members)
231 (defun semantic-default-html-setup ()
232 "Set up a buffer for parsing of HTML files."
233 ;; This will use our parser.
234 (setq semantic-parser-name "HTML"
235 semantic--parse-table t
236 imenu-create-index-function 'semantic-create-imenu-index
237 semantic-command-separation-character ">"
238 semantic-type-relation-separator-character '(":")
239 semantic-symbol->name-assoc-list '((section . "Section")
242 semantic-imenu-expandable-tag-classes '(section)
243 semantic-imenu-bucketize-file nil
244 semantic-imenu-bucketize-type-members nil
245 senator-step-at-start-end-tag-classes '(section)
246 senator-step-at-tag-classes '(section)
247 semantic-stickyfunc-sticky-classes '(section)
249 (semantic-install-function-overrides
250 '((tag-components . semantic-html-components)
255 (define-child-mode html-helper-mode html-mode
256 "`html-helper-mode' needs the same semantic support as `html-mode'.")
258 (provide 'semantic/html)
261 ;; generated-autoload-file: "loaddefs.el"
262 ;; generated-autoload-load-name: "semantic/html"
265 ;;; semantic/html.el ends here