unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Rüdiger Sonderfeld" <ruediger@c-plusplus.de>
To: 15709@debbugs.gnu.org
Cc: Lars Magne Ingebrigtsen <larsi@gnus.org>
Subject: bug#15709: [PATCH 7/8] shr: Handle <source> tag for video/audio elements.
Date: Fri, 25 Oct 2013 01:44:28 +0200	[thread overview]
Message-ID: <5299998.LUXqVN3kMR@descartes> (raw)
In-Reply-To: <cover.1382658061.git.ruediger@c-plusplus.de>

* lisp/net/shr.el (shr-prefer-media-type-alist): New customizable
  variable.
  (shr--get-media-pref): New function.
  (shr--extract-best-source): New function.
  (shr-tag-video, shr-tag-audio):  Use `shr--extract-best-source' when
  no :src tag was specified.

Signed-off-by: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
---
 lisp/net/shr.el | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 2 deletions(-)

diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 3ea2829..0015bd4 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -1097,10 +1097,67 @@ (defun shr-tag-object (cont)
       (shr-urlify start (shr-expand-url url)))
     (shr-generic cont)))
 
+(defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
+                                         ("ogv"  . 1.0)
+                                         ("ogg"  . 1.0)
+                                         ("opus" . 1.0)
+                                         ("flac" . 0.9)
+                                         ("wav"  . 0.5))
+  "Preferences for media types.
+The key element should be a regexp matched against the type of the source or
+url if no type is specified.  The value should be a float in the range 0.0 to
+1.0.  Media elements with higher value are preferred."
+  :version "24.4"
+  :group 'shr
+  :type '(alist :key-type regexp :value-type float))
+
+(defun shr--get-media-pref (elem)
+  "Determine the preference for ELEM.
+The preference is a float determined from `shr-prefer-media-type'."
+  (let ((type (cdr (assq :type elem)))
+        (p 0.0))
+    (unless type
+      (setq type (cdr (assq :src elem))))
+    (when type
+      (dolist (pref shr-prefer-media-type-alist)
+        (when (and
+               (> (cdr pref) p)
+               (string-match-p (car pref) type))
+          (setq p (cdr pref)))))
+    p))
+
+(defun shr--extract-best-source (cont &optional url pref)
+  "Extract the best `:src' property from <source> blocks in CONT."
+  (setq pref (or pref -1.0))
+  (let (new-pref)
+    (dolist (elem cont)
+      (when (and (listp elem)
+                 (not (keywordp (car elem)))) ;; skip attributes
+        (when (and (eq (car elem) 'source)
+                   (< pref
+                      (setq new-pref
+                            (shr--get-media-pref elem))))
+          (setq pref new-pref
+                url (cdr (assq :src elem)))
+          (message "new %s %s" url pref))
+        ;; libxml's html parser isn't HML5 compliant and non terminated
+        ;; source tags might end up as children.  So recursion it is...
+        (dolist (child (cdr elem))
+          (when (and (listp child)
+                     (not (keywordp (car child)))  ;; skip attributes
+                     (eq (car child) 'source))
+            (let ((ret (shr--extract-best-source (list child) url pref)))
+              (when (< pref (cdr ret))
+                (setq url (car ret)
+                      pref (cdr ret)))))))))
+  (cons url pref))
+
 (defun shr-tag-video (cont)
   (let ((image (cdr (assq :poster cont)))
-	(url (cdr (assq :src cont)))
-	(start (point)))
+        (url (cdr (assq :src cont)))
+        (start (point)))
+    (unless url
+      (setq url (car (shr--extract-best-source cont))))
     (if image
         (shr-tag-img nil image)
       (shr-insert " [video] "))
@@ -1109,6 +1166,8 @@ (defun shr-tag-video (cont)
 (defun shr-tag-audio (cont)
   (let ((url (cdr (assq :src cont)))
         (start (point)))
+    (unless url
+      (setq url (car (shr--extract-best-source cont))))
     (shr-insert " [audio] ")
     (shr-urlify start (shr-expand-url url))))
 
-- 
1.8.4.1







  parent reply	other threads:[~2013-10-24 23:44 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1382658061.git.ruediger@c-plusplus.de>
2013-10-24 23:43 ` bug#15703: [PATCH 1/8] EWW: Support user defined representation for checkbox Rüdiger Sonderfeld
2013-10-25 15:00   ` Ted Zlatanov
2013-10-25 17:39     ` Rüdiger Sonderfeld
2013-10-25 18:04       ` Ted Zlatanov
2013-10-26  1:54         ` Stefan Monnier
2013-11-03 11:40           ` Ted Zlatanov
2013-11-04  1:50             ` Stefan Monnier
2013-11-04 16:23               ` Ted Zlatanov
2013-11-04 17:51                 ` Stefan Monnier
2013-12-01 15:53   ` Lars Magne Ingebrigtsen
2013-10-24 23:43 ` bug#15704: [PATCH 2/8] EWW: Handle HTML5 input types as text input Rüdiger Sonderfeld
2013-12-01 15:38   ` Lars Magne Ingebrigtsen
2013-10-24 23:43 ` bug#15705: [PATCH 3/8] shr: Display content for video if no poster is available Rüdiger Sonderfeld
2013-12-01 15:46   ` bug#15708: " Lars Magne Ingebrigtsen
2013-10-24 23:43 ` bug#15706: [PATCH 4/8] shr: Add support for <audio> tag Rüdiger Sonderfeld
2013-12-01 15:46   ` bug#15708: " Lars Magne Ingebrigtsen
2013-10-24 23:44 ` bug#15707: [PATCH 5/8] EWW: Option to always use external-browser for certain content Rüdiger Sonderfeld
2013-12-01 15:44   ` Lars Magne Ingebrigtsen
2013-10-24 23:44 ` bug#15708: [PATCH 6/8] EWW: Erase old title Rüdiger Sonderfeld
2013-10-26  1:17   ` Rüdiger Sonderfeld
2013-10-26 11:04   ` bug#15708: [PATCH] " Rüdiger Sonderfeld
2013-12-01 15:47   ` bug#15708: [PATCH 6/8] " Lars Magne Ingebrigtsen
2013-10-24 23:44 ` Rüdiger Sonderfeld [this message]
2013-10-24 23:44 ` bug#15710: [PATCH 8/8] * lisp/net/shr.el: Fix typo Rüdiger Sonderfeld
2013-12-01 15:36   ` bug#15708: " Lars Magne Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5299998.LUXqVN3kMR@descartes \
    --to=ruediger@c-plusplus.de \
    --cc=15709@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).