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,32 +2957,25 @@ 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
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
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.
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=.
#+name: lst:define-org-yt-link-type
#+begin_src emacs-lisp -n :results silent
(when (ensure-package-installation 'emms)
;; https://bitspook.in/blog/extending-org-mode-to-handle-youtube-paths/
(declare-function emms-add-url "emms-source-file" (url))
(declare-function emms-playlist-last "emms" ())
(declare-function emms-playlist-mode-play-current-track "emms-playlist-mode" ())
(declare-function with-current-emms-playlist "emms" (&rest body))
(defun org-yt-emms-open (path)
"Open an \"YouTube\" PATH link with `emms' using \"mpv\"."
(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)
(emms-add-url url)
(emms-add-url link)
(with-current-emms-playlist
(save-excursion
(emms-playlist-last)
@ -2990,8 +2983,8 @@ The following posts provide programming information:
(defun org-yt-url-browse-open (path)
"Open an \"YouTube\" PATH link with `url-browse'."
(let ((url (format "https://www.youtube.com/watch?v=%s" path)))
(browse-url url)))
(let ((link (format "https://www.youtube.com/watch?v=%s" path)))
(browse-url link)))
(defun org-yt-export (path desc backend _)
"Export an \"YouTube\" PATH link according to DESC and BACKEND."
@ -2999,18 +2992,14 @@ The following posts provide programming information:
(`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))
(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)))
(with-eval-after-load 'ol
(org-link-set-parameters
"yt" :follow #'org-yt-emms-open :export #'org-yt-export))
"yt" :follow #'org-yt-emms-open :export #'org-yt-export)))
#+end_src
*** [[https://tecosaur.github.io/emacs-config/#translate-capital-keywords][Translate capital keywords (old) to lower case (new)]]