0

I'm fairly new to Emacs and Elisp, and am tinkering around to customize to my liking. I'd like to be able to highlight some text in Emacs and also have it displayed when exporting to html, just like how emphasis such as bold italics etc. are exported.

For the same, when a text is preceded by !, I'd like it to be highlighted in red and when exporting to html, highlight it, perhaps using a <mark> tag. For eg. !sometext! would be converted to <mark>sometext</mark> in html.

I have been able to get highlighting within org-mode to work through a solution I found: apply the org-habits styles, to the regex expressions ("\\(!\\)\\([^\n\r\t]+\\)\\(!\\). The entire piece of code is:

(add-to-list 'org-font-lock-extra-keywords '("\\(!\\)\\([^\n\r\t]+\\)\\(!\\)" (1 '(face org-habit-overdue-face invisible t)) (2 'org-habit-overdue-face t) (3 '(face org-habit-overdue-face invisible t))) t))

This works well, and my text is highlighted in red whenever I precede it with !. But I've been unsuccessful in getting it to export to HTML.

Since I do not know enough Elisp for such a complicated task, (but have a good understanding of regexps with other programming languages), I've been trying to edit this block of code I found online

(defun my-html-mark-tag (text backend info)
  "Transcode :blah: into <mark>blah</mark> in body text."
  (when (org-export-derived-backend-p backend 'html)
    (let ((text (replace-regexp-in-string "\\(!\\)\\([^\n\r\t]+\\)\\(!\\)" "<mark>"  text nil nil 1 nil)))
      (replace-regexp-in-string "\\(<mark>\\)\\([^\n\r\t]+\\)\\(!\\)" "</mark>" text nil nil 2 nil))))

(add-to-list 'org-export-filter-plain-text-functions 'my-html-mark-tag)

I'm not sure why this is not working since the regexp part seems fairly straightforward: I replace occurrences of !text! with <mark> followed by </mark>. But this code only produces <mark></mark> instead of <mark>text</mark>.

How can I fix this?

1 Answer 1

1

Two things:

  1. [^\n\r\t] matches !
  2. You don't need two replacements.
(replace-regexp-in-string
  "!\\([^!\n\r\t]+\\)!" "<mark>\\1</mark>"
  "Hello !world!, how !are! you?")

=> "Hello <mark>world</mark>, how <mark>are</mark> you?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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