diff options
| author | Lars Magne Ingebrigtsen | 2014-11-27 16:57:22 +0100 |
|---|---|---|
| committer | Lars Magne Ingebrigtsen | 2014-11-27 16:57:22 +0100 |
| commit | 2d431afee4061515a593da1f0a29bcd5fb152f07 (patch) | |
| tree | 003974df1a3052789ea6f7a7a4f747e9a2973fe6 | |
| parent | 2f5134c2766be5dcc3eb32b391183a229ee57e19 (diff) | |
| download | emacs-2d431afee4061515a593da1f0a29bcd5fb152f07.tar.gz emacs-2d431afee4061515a593da1f0a29bcd5fb152f07.zip | |
Add a DOM pretty-printing function
* doc/lispref/text.texi (Document Object Model): Mention `dom-pp'.
* lisp/dom.el (dom-pp): New function.
| -rw-r--r-- | doc/lispref/ChangeLog | 4 | ||||
| -rw-r--r-- | doc/lispref/text.texi | 34 | ||||
| -rw-r--r-- | lisp/ChangeLog | 4 | ||||
| -rw-r--r-- | lisp/dom.el | 38 |
4 files changed, 67 insertions, 13 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 37f16a132c3..74966431a45 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2014-11-27 Lars Magne Ingebrigtsen <larsi@gnus.org> | ||
| 2 | |||
| 3 | * text.texi (Document Object Model): Mention `dom-pp'. | ||
| 4 | |||
| 1 | 2014-11-26 Lars Magne Ingebrigtsen <larsi@gnus.org> | 5 | 2014-11-26 Lars Magne Ingebrigtsen <larsi@gnus.org> |
| 2 | 6 | ||
| 3 | * text.texi (Document Object Model): New node to document dom.el. | 7 | * text.texi (Document Object Model): New node to document dom.el. |
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 3d9451a708f..9c878a00c94 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi | |||
| @@ -4353,13 +4353,13 @@ A call to @code{libxml-parse-html-region} returns this @acronym{DOM} | |||
| 4353 | (document object model): | 4353 | (document object model): |
| 4354 | 4354 | ||
| 4355 | @example | 4355 | @example |
| 4356 | (html () | 4356 | (html nil |
| 4357 | (head ()) | 4357 | (head nil) |
| 4358 | (body ((width . "101")) | 4358 | (body ((width . "101")) |
| 4359 | (div ((class . "thing")) | 4359 | (div ((class . "thing")) |
| 4360 | "Foo" | 4360 | "Foo" |
| 4361 | (div () | 4361 | (div nil |
| 4362 | "Yes")))) | 4362 | "Yes")))) |
| 4363 | @end example | 4363 | @end example |
| 4364 | @end defun | 4364 | @end defun |
| 4365 | 4365 | ||
| @@ -4396,13 +4396,10 @@ node has a node name (called a @dfn{tag}), and optional key/value | |||
| 4396 | nodes are either strings or @acronym{DOM} objects. | 4396 | nodes are either strings or @acronym{DOM} objects. |
| 4397 | 4397 | ||
| 4398 | @example | 4398 | @example |
| 4399 | (body | 4399 | (body ((width . "101")) |
| 4400 | ((width . "101")) | 4400 | (div ((class . "thing")) |
| 4401 | (div | ||
| 4402 | ((class . "thing")) | ||
| 4403 | "Foo" | 4401 | "Foo" |
| 4404 | (div | 4402 | (div nil |
| 4405 | nil | ||
| 4406 | "Yes"))) | 4403 | "Yes"))) |
| 4407 | @end example | 4404 | @end example |
| 4408 | 4405 | ||
| @@ -4434,6 +4431,9 @@ would be: | |||
| 4434 | @item dom-children @var{node} | 4431 | @item dom-children @var{node} |
| 4435 | Return all the children of the node. | 4432 | Return all the children of the node. |
| 4436 | 4433 | ||
| 4434 | @item dom-non-text-children @var{node} | ||
| 4435 | Return all the non-string children of the node. | ||
| 4436 | |||
| 4437 | @item dom-attributes @var{node} | 4437 | @item dom-attributes @var{node} |
| 4438 | Return the key/value pair list of attributes of the node. | 4438 | Return the key/value pair list of attributes of the node. |
| 4439 | 4439 | ||
| @@ -4494,6 +4494,14 @@ which is a regular expression. | |||
| 4494 | 4494 | ||
| 4495 | @end table | 4495 | @end table |
| 4496 | 4496 | ||
| 4497 | Utility functions: | ||
| 4498 | |||
| 4499 | @table @code | ||
| 4500 | @item dom-pp @var{dom} &optional @var{remove-empty} | ||
| 4501 | Pretty-print @var{dom} at point. If @var{remove-empty}, don't print | ||
| 4502 | textual nodes that just contain white-space. | ||
| 4503 | @end table | ||
| 4504 | |||
| 4497 | 4505 | ||
| 4498 | @node Atomic Changes | 4506 | @node Atomic Changes |
| 4499 | @section Atomic Change Groups | 4507 | @section Atomic Change Groups |
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 92b50d98880..85748e60208 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2014-11-27 Lars Magne Ingebrigtsen <larsi@gnus.org> | ||
| 2 | |||
| 3 | * dom.el (dom-pp): New function. | ||
| 4 | |||
| 1 | 2014-11-17 Eli Zaretskii <eliz@gnu.org> | 5 | 2014-11-17 Eli Zaretskii <eliz@gnu.org> |
| 2 | 6 | ||
| 3 | * vc/vc-bzr.el (vc-bzr-print-log, vc-bzr-expanded-log-entry): | 7 | * vc/vc-bzr.el (vc-bzr-print-log, vc-bzr-expanded-log-entry): |
diff --git a/lisp/dom.el b/lisp/dom.el index 04d6c219ec0..6b24e4ffa91 100644 --- a/lisp/dom.el +++ b/lisp/dom.el | |||
| @@ -179,6 +179,44 @@ If BEFORE is nil, make CHILD NODE's first child." | |||
| 179 | (setcdr node (list nil))) | 179 | (setcdr node (list nil))) |
| 180 | node) | 180 | node) |
| 181 | 181 | ||
| 182 | (defun dom-pp (dom &optional remove-empty) | ||
| 183 | "Pretty-print DOM at point. | ||
| 184 | If REMOVE-EMPTY, ignore textual nodes that contain just | ||
| 185 | white-space." | ||
| 186 | (let ((column (current-column))) | ||
| 187 | (insert (format "(%S " (dom-tag dom))) | ||
| 188 | (let* ((attr (dom-attributes dom)) | ||
| 189 | (times (length attr)) | ||
| 190 | (column (1+ (current-column)))) | ||
| 191 | (if (null attr) | ||
| 192 | (insert "nil") | ||
| 193 | (insert "(") | ||
| 194 | (dolist (elem attr) | ||
| 195 | (insert (format "(%S . %S)" (car elem) (cdr elem))) | ||
| 196 | (if (zerop (cl-decf times)) | ||
| 197 | (insert ")") | ||
| 198 | (insert "\n" (make-string column ? )))))) | ||
| 199 | (let* ((children (if remove-empty | ||
| 200 | (cl-remove-if | ||
| 201 | (lambda (child) | ||
| 202 | (and (stringp child) | ||
| 203 | (string-match "\\`[\n\r\t ]*\\'" child))) | ||
| 204 | (dom-children dom)) | ||
| 205 | (dom-children dom))) | ||
| 206 | (times (length children))) | ||
| 207 | (if (null children) | ||
| 208 | (insert ")") | ||
| 209 | (insert "\n" (make-string (1+ column) ? )) | ||
| 210 | (dolist (child children) | ||
| 211 | (if (stringp child) | ||
| 212 | (if (or (not remove-empty) | ||
| 213 | (not (string-match "\\`[\n\r\t ]*\\'" child))) | ||
| 214 | (insert (format "%S" child))) | ||
| 215 | (dom-pp child remove-empty)) | ||
| 216 | (if (zerop (cl-decf times)) | ||
| 217 | (insert ")") | ||
| 218 | (insert "\n" (make-string (1+ column) ? )))))))) | ||
| 219 | |||
| 182 | (provide 'dom) | 220 | (provide 'dom) |
| 183 | 221 | ||
| 184 | ;;; dom.el ends here | 222 | ;;; dom.el ends here |