如何替换emacs中的所有事件



我正在用组织模式发布构建一个博客。但我想编辑组织模式给出的默认样式,该版本意味着将一些标签包装在其他标签中(如divs标签(。所以我构建这个的方式是基于一些想法(查看下面的链接(。

例如,下面的函数是在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程序员,但我正在尝试构建一个函数来获得一些解决方案。所以实际的问题是:我有这个:

<img src="images/photo.jpg" alt="">

我需要包装img标签,这样我就可以看到下一个输出:

<div uk-lightbox="animation: slide">
<a href="images/photo.jpg">
<img src="images/photo.jpg" alt="">
</a>
</div>

到目前为止,我创建了一个小函数来提取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了解不多。。。因此,如果有人知道如何继续(或有解决方案(来解决这个问题,我会很高兴:(

你好,这是我的最终解决方案:

我创建了这个功能(注意:正如你所看到的,这个功能没有优化,但有效,也许我会编辑这个答案来发布最终的优化解决方案(:

(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
)
)

此外,我想在一些标签上添加一个类属性,所以我创建了下一个函数

(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文件的预最终处理器上执行它:(以下功能想法取自本博客(

(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))

就这样!它起作用:(

此外,我会修改包装img标签函数,以将任何类型的标签包装成其他所需的标签!

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>"))

如果您想了解更多关于函数的信息,请使用C-hf(描述函数(。

最新更新