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 aed0935224

View File

@ -2956,61 +2956,50 @@ See `org-link-parameters' for details about PATH, DESC and FORMAT."
:END: :END:
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]] using 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" ())
(declare-function with-current-emms-playlist "emms" (&rest body))
(defun org-yt-emms-open (path)
"Open an \"YouTube\" PATH link with `emms' using \"mpv\"." "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) (require 'emms nil 'noerror)
(emms-add-url url) (emms-add-url link)
(with-current-emms-playlist (with-current-emms-playlist
(save-excursion (save-excursion
(emms-playlist-last) (emms-playlist-last)
(emms-playlist-mode-play-current-track))))) (emms-playlist-mode-play-current-track)))))
(defun org-yt-url-browse-open (path) (defun org-yt-url-browse-open (path)
"Open an \"YouTube\" PATH link with `url-browse'." "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)))
(browse-url url))) (browse-url link)))
(defun org-yt-export (path desc backend _) (defun org-yt-export (path desc backend _)
"Export an \"YouTube\" PATH link according to DESC and BACKEND." "Export an \"YouTube\" PATH link according to DESC and BACKEND."
(pcase backend (pcase backend
(`ascii (`ascii
(format "[%s] <https://www.youtube.com/watch?v=%s>" desc path)) (format "[%s] <https://www.youtube.com/watch?v=%s>" desc path))
(`html (`html
(format "<iframe (format "<a href=\"https://www.youtube.com/watch?v=%s\">%s</a>" path desc))
frameborder=\"0\" width=\"600\" height=\"450\"
src=\"https://www.youtube.com/embed/%s\"
title=\"%s\"
allowfullscreen></iframe>" path desc))
(`latex (`latex
(format "\\href{https://www.youtube.com/watch?v=%s}{%s}" path desc)) (format "\\href{https://www.youtube.com/watch?v=%s}{%s}" path desc))
(_ path))) (_ path)))
(with-eval-after-load 'ol (with-eval-after-load 'ol
(org-link-set-parameters (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 #+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)]]