From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?UTF-8?Q?R=C3=BCdiger?= Sonderfeld Newsgroups: gmane.emacs.bugs Subject: bug#15709: [PATCH 7/8] shr: Handle tag for video/audio elements. Date: Fri, 25 Oct 2013 01:44:28 +0200 Message-ID: <5299998.LUXqVN3kMR@descartes> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1382658355 9141 80.91.229.3 (24 Oct 2013 23:45:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 24 Oct 2013 23:45:55 +0000 (UTC) Cc: Lars Magne Ingebrigtsen To: 15709@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Oct 25 01:45:57 2013 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VZUbD-0006uC-2O for geb-bug-gnu-emacs@m.gmane.org; Fri, 25 Oct 2013 01:45:55 +0200 Original-Received: from localhost ([::1]:56675 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUbC-0006sK-Nm for geb-bug-gnu-emacs@m.gmane.org; Thu, 24 Oct 2013 19:45:54 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUab-0006QD-2z for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:45:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VZUaU-0007ZO-3r for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:45:17 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:56310) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUaU-0007ZK-0e for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:45:10 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1VZUaT-0005yD-GN for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:45:09 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?R=C3=BCdiger?= Sonderfeld Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 24 Oct 2013 23:45:09 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 15709 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.138265830622920 (code B ref -1); Thu, 24 Oct 2013 23:45:09 +0000 Original-Received: (at submit) by debbugs.gnu.org; 24 Oct 2013 23:45:06 +0000 Original-Received: from localhost ([127.0.0.1]:42089 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1VZUaO-0005xO-Pu for submit@debbugs.gnu.org; Thu, 24 Oct 2013 19:45:05 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:59751) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1VZUaL-0005wU-Hz for submit@debbugs.gnu.org; Thu, 24 Oct 2013 19:45:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VZUa8-0007HL-L7 for submit@debbugs.gnu.org; Thu, 24 Oct 2013 19:44:56 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:37783) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUa8-0007HH-Hm for submit@debbugs.gnu.org; Thu, 24 Oct 2013 19:44:48 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33197) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUa2-0006Jl-FK for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:44:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VZUZw-0007F7-Ey for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:44:42 -0400 Original-Received: from ptmx.org ([178.63.28.110]:41474) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VZUZw-0007F2-4N for bug-gnu-emacs@gnu.org; Thu, 24 Oct 2013 19:44:36 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by ptmx.org (Postfix) with ESMTP id 6399E2B155; Fri, 25 Oct 2013 01:44:35 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at ptmx.org Original-Received: from ptmx.org ([127.0.0.1]) by localhost (ptmx.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jOv5jy-TkJVI; Fri, 25 Oct 2013 01:44:32 +0200 (CEST) Original-Received: from descartes.localnet (chello080108246092.7.14.vie.surfer.at [80.108.246.92]) by ptmx.org (Postfix) with ESMTPSA id 98B1E2AEE8; Fri, 25 Oct 2013 01:44:32 +0200 (CEST) User-Agent: KMail/4.11.2 (Linux/3.11.0-12-generic; KDE/4.11.2; x86_64; ; ) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:79614 Archived-At: * 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=C3=BCdiger Sonderfeld --- 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))) =20 +(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 sou= rce 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 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 terminate= d + ;; 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))) -=09(url (cdr (assq :src cont))) -=09(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)))) =20 --=20 1.8.4.1