Rework the "Extending org-mode to handle YouTube links" section

This commit is contained in:
Gerard Vermeulen 2024-06-15 19:09:53 +02:00
parent c72fe6c71a
commit 6bbf500ee5

View File

@ -2957,60 +2957,49 @@ See `org-link-parameters' for details about PATH, DESC and FORMAT."
Listing [[lst:define-org-yt-link-type][define org-yt-link type]] implements code to open the link to the Listing [[lst:define-org-yt-link-type][define org-yt-link type]] implements code to open the link to the
following YouTube video [[yt:eaZUZCzaIgw][Extending org-mode to handle YouTube links]] and the following YouTube video [[yt:eaZUZCzaIgw][Extending org-mode to handle YouTube links]] and the
following [[https://raw.githubusercontent.com/bitspook/spookmax.d/master/readme.org][Emacs setup]]. following [[https://raw.githubusercontent.com/bitspook/spookmax.d/master/readme.org][Emacs setup]]. Note: Listing [[lst:define-org-yt-link-type][define org-yt-link type]] does only
implement normal links, but no embedded frames.
Opening [[yt:eaZUZCzaIgw][Extending org-mode to handle YouTube links]] may fail due to a bug in the Opening [[yt:eaZUZCzaIgw][Extending org-mode to handle YouTube links]] may fail due to a bug in the
interface between =mpv= and =yt-dlp=. Listing [[lst:set-emms-options][set EMMS options]] indicates how to interface between =mpv= and =yt-dlp=. Listing [[lst:set-emms-options][set EMMS options]] indicates how to
work around this bug at the cost of making the user interface less clean. work around this bug at the cost of making the user interface less clean.
However, the link [[yt:48JlgiBpw_I][Absolute Beginner's Guide to Emacs]] works always. However, the link [[yt:48JlgiBpw_I][Absolute Beginner's Guide to Emacs]] works always.
The following posts provide programming information:
1. [[https://developers.google.com/youtube/v3/docs][YouTube API reference]].
2. [[https://developers.google.com/youtube/iframe_api_reference][YouTube Player API reference for iframe embeds]].
#+caption[Define an =org-link= type for =YouTube=]: #+caption[Define an =org-link= type for =YouTube=]:
#+caption: Define an =org-link= type for =YouTube=. #+caption: Define an =org-link= type for =YouTube=.
#+name: lst:define-org-yt-link-type #+name: lst:define-org-yt-link-type
#+begin_src emacs-lisp -n :results silent #+begin_src emacs-lisp -n :results silent
;; https://bitspook.in/blog/extending-org-mode-to-handle-youtube-paths/ (when (ensure-package-installation 'emms)
(declare-function emms-add-url "emms-source-file" (url)) ;; https://bitspook.in/blog/extending-org-mode-to-handle-youtube-paths/
(declare-function emms-playlist-last "emms" ()) (defun org-yt-emms-open (path)
(declare-function emms-playlist-mode-play-current-track "emms-playlist-mode" ()) "Open an \"YouTube\" PATH link with `emms' using \"mpv\"."
(declare-function with-current-emms-playlist "emms" (&rest body)) (let ((link (format "https://www.youtube.com/watch?v=%s" path)))
(require 'emms nil 'noerror)
(emms-add-url link)
(with-current-emms-playlist
(save-excursion
(emms-playlist-last)
(emms-playlist-mode-play-current-track)))))
(defun org-yt-emms-open (path) (defun org-yt-url-browse-open (path)
"Open an \"YouTube\" PATH link with `emms' using \"mpv\"." "Open an \"YouTube\" PATH link with `url-browse'."
(let ((url (format "https://www.youtube.com/watch?v=%s" path))) (let ((link (format "https://www.youtube.com/watch?v=%s" path)))
(require 'emms nil 'noerror) (browse-url link)))
(emms-add-url url)
(with-current-emms-playlist
(save-excursion
(emms-playlist-last)
(emms-playlist-mode-play-current-track)))))
(defun org-yt-url-browse-open (path) (defun org-yt-export (path desc backend _)
"Open an \"YouTube\" PATH link with `url-browse'." "Export an \"YouTube\" PATH link according to DESC and BACKEND."
(let ((url (format "https://www.youtube.com/watch?v=%s" path))) (pcase backend
(browse-url url))) (`ascii
(format "[%s] <https://www.youtube.com/watch?v=%s>" desc path))
(`html
(format "<a href=\"https://www.youtube.com/watch?v=%s\">%s</a>" path desc))
(`latex
(format "\\href{https://www.youtube.com/watch?v=%s}{%s}" path desc))
(_ path)))
(defun org-yt-export (path desc backend _) (with-eval-after-load 'ol
"Export an \"YouTube\" PATH link according to DESC and BACKEND." (org-link-set-parameters
(pcase backend "yt" :follow #'org-yt-emms-open :export #'org-yt-export)))
(`ascii
(format "[%s] <https://www.youtube.com/watch?v=%s>" desc path))
(`html
(format "<iframe
frameborder=\"0\" width=\"600\" height=\"450\"
src=\"https://www.youtube.com/embed/%s\"
title=\"%s\"
allowfullscreen></iframe>" path desc))
(`latex
(format "\\href{https://www.youtube.com/watch?v=%s}{%s}" path desc))
(_ path)))
(with-eval-after-load 'ol
(org-link-set-parameters
"yt" :follow #'org-yt-emms-open :export #'org-yt-export))
#+end_src #+end_src
*** [[https://tecosaur.github.io/emacs-config/#translate-capital-keywords][Translate capital keywords (old) to lower case (new)]] *** [[https://tecosaur.github.io/emacs-config/#translate-capital-keywords][Translate capital keywords (old) to lower case (new)]]