Xah Talk Show 2024-07-24 Ep566, Emacs Lisp, add twitter cards to 10 thousand files

vidthumb yLBywdmSLy4


;; given a dir
;; go thru each html file
;; grab all the link with thumbnail
;; you got two parts
;; one is a file path
;; the other is a image file path
;; <li><a href="glove80_keyboard.html"><img src="i2/glove80/glove80_keyboard_20230126_599-s250.jpg" alt="thumbnail" />Glove80</a>📶 👍</li>

;; build a hashtable, key is the file path, value is image path

;; then, go thru hashtable, for each key, open it, insert the image path as meta tag, aka twitter card

(defvar xah-thumbnail-links-table
  nil
  "Each key is a file path, and value is a image file path. both are full path.")
(setq xah-thumbnail-links-table (make-hash-table :test 'equal))
;; (clrhash xah-thumbnail-links-table)

(defun xah-walk-dir-x (BaseDir)
  "Walk a dir BaseDir, and for each html file function Xfunc on it.
Created: 2024-07-24
Version: 2024-07-24"
  (let (xpaths xfilepath)
    (setq xpaths (directory-files-recursively BaseDir ".+\.html$"))
    (mapc 'xah-build-hashtable xpaths)))

(defun xah-build-hashtable (Fpath)
  "for each thumbnail link in Fpath, push it to hashtable `xah-thumbnail-links-table'.
Created: 2024-07-24
Version: 2024-07-24"
  (let ()
    ;; open the file, goto top, search for a pattern like this
    ;; <li><a href="glove80_keyboard.html"><img src="i2/glove80/glove80_keyboard_20230126_599-s250.jpg" alt="thumbnail" />Glove80</a>
    ;; then, grab both links
    ;; make relative link to full path
    ;; add file path to hash, with image path as value
    (with-temp-buffer
      (insert-file-contents Fpath)
      (goto-char (point-min))
      (while
          ;; <li><a href="glove80_keyboard.html"><img src="i2/glove80/glove80_keyboard_20230126_599-s250.jpg" alt="thumbnail" />Glove80</a>
          (re-search-forward
           "<li><a href=\"\\([^\"]+\\)\"><img src=\"\\([^\"]+\\)\" alt=\"thumbnail\" />\\([^<]+\\)</a>"
           nil t)
        (puthash
         (expand-file-name
          (match-string 1) (file-name-directory Fpath))
         (expand-file-name
          (match-string 2) (file-name-directory Fpath))

         xah-thumbnail-links-table)))))

;; HHHH---------------------------------------------------

;; (xah-walk-dir-x "c:/Users/xah/web/xahlee_info/kbd/")

;; (hash-table-count xah-thumbnail-links-table)
;; (xah-print-hash xah-thumbnail-links-table)

(xah-build-hashtable "C:/Users/xah/web/xahlee_info/kbd/ergonomic_keyboards_index.html")

;; HHHH---------------------------------------------------
;; now we have built a hashtable.
;; now, go thru the hashtable,
;; for each key, open the file
;; insert the value, to the top of the page

(maphash
 (lambda (xkey xval)
   (with-temp-buffer
     (insert-file-contents xkey)
     (goto-char (point-min))
     (search-forward "<title>")
     (goto-char (match-beginning 0))

     (insert xval "\n")

     ;; <meta name="twitter:card" content="summary" />
     ;; <meta name="twitter:title" content="something" />
     ;; <meta name="twitter:image" content="https://xahlee.info/bfS8V.jpg" />
     ;; <meta name="xah:thumbnail" content="https://xahlee.info/bfS8V.jpg" />

     ))
 xah-thumbnail-links-table)