[英]How to replace all occurrences in emacs?
我正在使用 org-mode publish 構建一個博客。 但我想編輯 org-mode 給出的默認樣式,該版本意味着將一些標簽包裝在其他標簽中(如 div 標簽)。 所以我構建它的方式是基於一些想法(查看下面的鏈接)。
例如下面的 function 它在 body 標簽中添加了一個 class 屬性,它起作用了! (這個想法是基於這個問題)
(defun my-html-body-onload-filter (output backend info)
"Add class to<body> tag, if any."
(when (and (eq backend 'html)
(string-match "<body>\n" output))
(replace-match "<body class='uk-container-small uk-align-center uk-text-justify'> \n" nil nil output))
)
所以我現在想做的是將 img 標簽包裝在其他標簽中。 我不是 elisp 程序員,但我正在嘗試構建一個 function 來找到一些解決方案。 所以實際的問題是:我有這個:
<img src="images/photo.jpg" alt="">
我需要包裝 img 標簽,這樣我才能看到下一個 output:
<div uk-lightbox="animation: slide">
<a href="images/photo.jpg">
<img src="images/photo.jpg" alt="">
</a>
</div>
到目前為止,我創建了這個小 function 來提取 src 值:
(defun xe ()
(interactive)
(search-forward "<img src=\"")
(defvar x-start (point))
(search-forward "\"")
(backward-char)
(defvar x-end (point))
(kill-ring-save x-start x-end)
)
但我感覺迷路了,因為我對 elisp 了解不多......所以如果有人知道如何繼續(或有解決方案)來解決這個問題,我會很高興:)
你好,這是我的最終解決方案:
我創建了這個函數(注意:如你所見,這個 function 沒有優化但有效,也許我會編輯這個答案以發布最終優化的解決方案):
(defun wrap-img-tags ()
(setq text-to-search-1 "<img src=\"")
(goto-char (point-min))
(setq ntimes (count-matches text-to-search-1))
(if (> ntimes 0)
(cl-loop repeat ntimes do ;; this is like a for loop
(search-forward "<img src=\"" nil t)
(setq x-start (point))
(search-forward "\"")
(backward-char)
(setq x-end (point))
(kill-ring-save x-start x-end)
(beginning-of-line)
(insert (format "\n<div uk-lightbox=\"animation: slide\">
<a href=\"%s\">\n" (car kill-ring)))
(indent-for-tab-command)
(forward-line)
(insert (format "</a>\n</div>"))
)
nil
)
)
另外我想在一些標簽上添加 class 屬性,所以我創建了下一個 function :
(defun add-class-to-tag (tag class)
"Add class attribute with the class variable value.
TAG: Tag to modify.
CLASS: Class in string form to add."
; (interactive "sTag:\nsClass:")
(setq text-to-search (format "<%s" tag))
(goto-char (point-min))
(setq does-it-have-class-attribute t)
(cl-loop repeat (how-many text-to-search) do ;; this is like a for loop
(search-forward text-to-search)
(setq x-start (point))
(setq does-it-have-class-attribute (search-forward
"class=\""
(line-end-position)
t ; if fails return nil
))
(if (not does-it-have-class-attribute)
(progn
(insert (format " class=\"%s\"" class))
(setq does-it-have-class-attribute nil)
)
(progn ; else
(search-forward "\"")
(backward-char)
(insert (format " %s" class))
))
)
)
最后,我在創建的 html 文件的預最終處理器上執行它:(以下 function 想法取自此博客)
(defun org-blog-publish-to-html (plist filename pub-dir)
"Same as `org-html-publish-to-html' but modifies html before finishing."
(let ((file-path (org-html-publish-to-html plist filename pub-dir)))
(with-current-buffer (find-file-noselect file-path)
(wrap-img-tags);; Here I'm executing the first solution
(add-class-to-tag "h2" "uk-heading-bullet")
(add-class-to-tag "section" "uk-card uk-card-body uk-align-center uk-text-justify")
(add-class-to-tag "h1" "uk-h2 uk-panel uk-padding uk-background-secondary uk-light uk-margin-left uk-margin-right")
(save-buffer)
(kill-buffer))
file-path))
就是這樣:它有效:)
此外,我會修改wrap-img-tags function 以將任何類型的標簽與其他所需標簽一起包裝!
PD :如果這個回答對你有幫助,請點贊。 我花了大約三天的時間研究和測試以找到這個解決方案。 謝謝!
我認為你不應該保存文本。 只需插入您想要的東西。
(while (search-forward "<img src=\"" nil t nil)
(goto-char (match-beginning 0))
(insert "<div uk-lightbox=\"animation: slide\"><a href=\"images/photo.jpg\">")
(search-forward ">")
(insert "</a></div>"))
如果您想了解更多關於 function 的信息,請使用 Ch f(描述函數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.