Function: eww-readable-dom

eww-readable-dom is a byte-compiled function defined in eww.el.gz.

Signature

(eww-readable-dom DOM)

Documentation

Return a readable version of DOM.

If EWW can't create a readable version, return nil instead.

Source Code

;; Defined in /usr/src/emacs/lisp/net/eww.el.gz
(defun eww-readable-dom (dom)
  "Return a readable version of DOM.
If EWW can't create a readable version, return nil instead."
  (let ((head-nodes nil)
        (best-node nil)
        (best-score most-negative-fixnum))
    (eww--walk-readability
     dom
     (lambda (node score)
       (when (consp node)
         (when (and score (> score best-score)
                    ;; We set a lower bound to how long we accept that
                    ;; the readable portion of the page is going to be.
                    (> (eww--dom-count-words node) 100))
           (setq best-score score
                 best-node node))
         ;; Keep track of any <title> and <link> tags we find to include
         ;; in the final document.  EWW uses them for various features,
         ;; like renaming the buffer or navigating to "next" and
         ;; "previous" pages.  NOTE: We could probably filter out
         ;; stylesheet <link> tags here, though it doesn't really matter
         ;; since we don't *do* anything with stylesheets...
         (when (memq (dom-tag node) '(title link base))
           ;; Copy the node, but not any of its (non-text) children.
           ;; This way, we can ensure that we don't include a node
           ;; directly in our list in addition to as a child of some
           ;; other node in the list.  This is ok for <title> and <link>
           ;; tags, but might need changed if supporting other tags.
           (let* ((inner-text (dom-inner-text node))
                  (new-node `(,(dom-tag node)
                              ,(dom-attributes node)
                              ,@(when (length> inner-text 0)
                                  (list inner-text)))))
             (push new-node head-nodes))))))
    (when (and best-node (not (eq best-node dom)))
      `(html nil
             (head nil ,@head-nodes)
             (body nil ,best-node)))))