This is a follow-up question from a previous thread

The function org-html--wrap-image adds a <p> tag around its contents, as you can see here:

ELISP> (org-html--wrap-image "bla" t)
"\n<div class=\"figure\">\n<p>bla</p>\n</div>"

I want to remove those <p> tags around "bla". My current workaround consists of redefining the function org-html--wrap-image completely, removing the tags, but this seems to be a bit of an overkill.

I acknowledge that if I wrap the function with a call to replace-regexp-in-string, I can achieve the same effect.

ELISP> (replace-regexp-in-string
        (rx (or "<p>" "</p>"))
        (org-html--wrap-image "bla" t))

"\n<div class=\"figure\">\nbla\n</div>"

How can I write an "advice" to do that? I've never written one before, and the documentation for "advices" is kind of harsh on Elisp beginners.

For completeness, here's the function code:

(defun org-html--wrap-image (contents info &optional caption label)
  "Wrap CONTENTS string within an appropriate environment for images.
INFO is a plist used as a communication channel.  When optional
arguments CAPTION and LABEL are given, use them for caption and
\"id\" attribute."
  (let ((html5-fancy (org-html--html5-fancy-p info)))
    (format (if html5-fancy "\n<figure%s>%s%s\n</figure>"
          "\n<div%s class=\"figure\">%s%s\n</div>")
        ;; ID.
        (if (org-string-nw-p label) (format " id=\"%s\"" label) "")
        ;; Contents.
        (format "\n<p>%s</p>" contents)
        ;; Caption.
        (if (not (org-string-nw-p caption)) ""
          (format (if html5-fancy "\n<figcaption>%s</figcaption>"

Since Org offers export filters (see (info "(org) Advanced configuration")) for this very purpose, I would refrain from advising an internal function. The following will unwrap img tags in the final output of an HTML export:

(defun org-html-export-img-without-p-tag (s backend info)
  (when (org-export-derived-backend-p backend 'html)
    (replace-regexp-in-string "class=\"figure\">\n<p>\\(?1:<img[^<]+\\)</p>"
                              "class=\"figure\"\n\\1" s)))

(add-to-list 'org-export-filter-final-output-functions
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.