Xah Talk Show 2025-05-08 Ep654 emacs lisp, add id to div tag, part 2
continued from Xah Talk Show 2025-05-03 Ep651 emacs lisp, add id to div tag, part 1
;; -*- coding: utf-8; lexical-binding: t; -*- ;; • go thru the web dir, ten thousand html files ;; • open each, find ;; • 「<div class="topicxl">」 ;; • then, add an id attribute e.g. id="jYhjt" ;; • for each of the page linked in the topic box, same id must be used. (defvar x-table nil "hashtable, key is title, value is list of filepaths.") (setq x-table (make-hash-table :test 'equal)) (defun xah-do-one-file (Fpath) "Process the file at path FPATH Created: 2025-05-03 Version: 2025-05-03" (interactive) (with-temp-buffer (insert-file-contents Fpath) (goto-char (point-min)) (while (re-search-forward "<div class=\"topicXL\">\n<h4>\\([^<]+\\)</h4>" nil t) (let (xbeg xend xtitle divblock xkeyval) (setq xtitle (match-string 1)) (setq xbeg (match-beginning 0)) (goto-char xbeg) (forward-char) (sgml-skip-tag-forward 1) (setq xend (point)) (setq divblock (buffer-substring-no-properties xbeg xend)) ;; add to hashtable (setq xkeyval (gethash divblock x-table)) (if xkeyval (puthash divblock (push (vector Fpath xbeg xend) xkeyval) x-table) (puthash divblock (list (vector Fpath xbeg xend)) x-table)))))) ;; (xah-do-one-file "c:/Users/xah/web/xahlee_info/golang/goland_find_string.html") ;; (xah-do-one-file "c:/Users/xah/web/xahlee_info/js/js_array_map.html") (mapc 'xah-do-one-file (directory-files-recursively "c:/Users/xah/web/xahlee_info/js/" "\\.html$" nil (lambda (x) (not (string-match-p "/\\." x))))) (print (xah-format-hash-table x-table)) (maphash (lambda (divblock fileDataVector) (let (xid xfpath xbeg xeng) (setq xid (format "%x" (random #xfffffff))) (seq-setq (xfpath xbeg xend) fileDataVector) (mapc (lambda (x) (with-temp-buffer (insert-file-contents xfpath) (goto-char xbeg) (search-forward "class=\"topicXL\"") (insert (format " id=\"%s\"" xid)))) fpaths) ;; )) x-table)
