Xah Talk Show 2024-09-17 Ep586, Emacs Lisp, Rename All Links in Web Root Dir

ai chinese goddess emacs 2024-09-17 fYxvK
ai chinese goddess emacs 2024-09-17 fYxvK
(defun xah-html-rename-title-all ()
  "Rename all links, for all html files of the file path cursor is on, on current web root dir.
Created: 2024-09-17
Version: 2024-09-18"
  (interactive)
  (let (xbeg xend (xcurrent-file-path buffer-file-name) xrelativepath xfullpath xlinktext)
    (let ((xboundary (bounds-of-thing-at-point 'filename)))
      ;; (cons 411 712)
      (setq xbeg (car xboundary))
      (setq xend (cdr xboundary)))
    (setq xrelativepath
          (buffer-substring-no-properties xbeg xend))
    (setq xfullpath
          (expand-file-name
           xrelativepath
           (file-name-directory xcurrent-file-path)))
    (let (xbeg xend)
      ;; get link text, e.g. <a href="../x.html">something</a>
      (search-forward ">")
      (setq xbeg (point))
      (search-forward "<")
      (setq xend (match-beginning 0))
      (setq xlinktext (buffer-substring-no-properties xbeg xend)))
    (let (xfind-str xrep-str xnew-title
                    (xrootdir "c:/Users/xah/web/xahlee_org/")
                    xfilelist)
      ;; now do find replace for all web root dir
      ;; get find string, typically like this
      ;; "...name.html">link text</a>
      ;; create replace string
      ;; get the web root dir
      ;; assume .html files, skip dot dir
      (setq xnew-title (read-string "New title:"))
      (setq xfind-str
            (concat (file-name-nondirectory xrelativepath)
                    "\">" xlinktext "</a>"))
      (setq xrep-str
            (concat (file-name-nondirectory xrelativepath)
                    "\">" xnew-title "</a>"))
      ;; now do find and replace, for all in root dir
      ;; get list of files in root dir
      (setq xfilelist
            (directory-files-recursively
             xrootdir "\\.html$"
             nil
             (lambda (x) (not (string-match-p "/\\." x)))))
      (mapc
       (lambda (x)
         (let (xchanged)
           (with-temp-buffer
             (insert-file-contents x)
             ;; do find replace
             (goto-char (point-min))
             (while (search-forward xfind-str nil t)
               (replace-match xrep-str t t)
               (setq xchanged t))
             (when xchanged (message "changed, path %s" x))
             (when xchanged (write-region (point-min) (point-max) x)))))
       xfilelist))))

xts 2024-09-17 ai bot fsharp
xts 2024-09-17 ai bot fsharp

draw a chinese goddess holding a sign that says emacs

ai chinese goddess emacs 2024-09-17 ZnjCT
ai chinese goddess emacs 2024-09-17 ZnjCT