3

I am exporting from org mode format to LaTeX and HTML formats. To tweak a little plain text in those formats, I am using filter functions.

If I try to use such a form of a filter function:

(defun filter-function-html (text backend info)
  "Docstring."
  (when (org-export-derived-backend-p 'latex)
  (regexp-function-1)
  (regexp-function-2)
  (regexp-function-3)
  etc ... 
  ))

the same for HTML format, then simply:

(add-to-list 'org-export-plain-text-functions
  'my-latex-filter-function
  'my-html-filter-function)

However, this is not working. Only way I could make it work is to define each regexp as separate function and added it separately each time with add-to-list.

How can I improve readablity of these functions?

Specific example (lengthy):

(defun my-latex-filter-nobreaks (text backend info)
    "Ensure \" \" are properly handled in LaTeX export."
    (when (org-export-derived-backend-p backend 'latex)
      (replace-regexp-in-string " \\([zuioaskvZUIOASKV]\\) " " \\1~" text)))

  (defun my-latex-filter-highlightNotes-1 (text backend info)
    "Highligh custom notes markup. Notes are surounded by \"%%\" or \"!!\" delimiters."
    (when (org-export-derived-backend-p backend 'latex)
      (replace-regexp-in-string "!!\\(.*\\)!!" "\\\\highLight[yellow]{\\1}" text)))

  (defun my-latex-filter-highlightNotes-2 (text backend info)
    "Highligh custom notes markup. Notes are surounded by \"%%\" or \"!!\" delimiters."
    (when (org-export-derived-backend-p backend 'latex)
      (replace-regexp-in-string "\\\\%\\\\%\\(.*\\)\\\\%\\\\%" "\\\\highLight[red]{\\1}" text)))

  (defun my-html-filter-nobreaks (text backend info)
    "Ensure \" \" are properly handled in LaTeX export."
    (when (org-export-derived-backend-p backend 'html)
      (replace-regexp-in-string " \\([zuioaskvZUIOASKV]\\) " " \\1&nbsp" text)))

  (defun my-html-filter-highlightNotes-1 (text backend info)
    "Highligh custom notes markup. Notes are surounded by \"%%\" or \"!!\" delimiters."
    (when (org-export-derived-backend-p backend 'html)
     (replace-regexp-in-string "!!\\(.*\\)!!" "<span style=\"background-color:yellow\">\\1</span>" text)))

  (defun my-html-filter-highlightNotes-2 (text backend info)
    "Highligh custom notes markup. Notes are surounded by \"%%\" or \"!!\" delimiters."
    (when (org-export-derived-backend-p backend 'html)
      (replace-regexp-in-string "%%\\(.*\\)%%" "<span style=\"background-color:red\">\\1</span>" text)))


  (add-to-list 'org-export-filter-plain-text-functions
               'my-latex-filter-nobreaks)

  (add-to-list 'org-export-filter-plain-text-functions
               'my-latex-filter-highlightNotes-1)

  (add-to-list 'org-export-filter-plain-text-functions
               'my-latex-filter-highlightNotes-2)

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

  (add-to-list 'org-export-filter-plain-text-functions
               'my-html-filter-highlightNotes-1)

  (add-to-list 'org-export-filter-plain-text-functions
               'my-html-filter-highlightNotes-2)

which I would like to shrink to:

(defun my-latex-filter-nobreaks (text backend info)
    "Ensure \" \" are properly handled in LaTeX export."
    (when (org-export-derived-backend-p backend 'latex)
      (replace-regexp-in-string " \\([zuioaskvZUIOASKV]\\) " " \\1~" text)
      (replace-regexp-in-string "!!\\(.*\\)!!" "\\\\highLight[yellow]{\\1}" text)
      (replace-regexp-in-string "\\\\%\\\\%\\(.*\\)\\\\%\\\\%" "\\\\highLight[red]{\\1}" text)))

  (defun my-html-filter-nobreaks (text backend info)
    "Ensure \" \" are properly handled in LaTeX export."
    (when (org-export-derived-backend-p backend 'html)
      (replace-regexp-in-string " \\([zuioaskvZUIOASKV]\\) " " \\1&nbsp" text)
      (replace-regexp-in-string "!!\\(.*\\)!!" "<span style=\"background-color:yellow\">\\1</span>" text)
      (replace-regexp-in-string "%%\\(.*\\)%%" "<span style=\"background-color:red\">\\1</span>" text)))

     
  (add-to-list 'org-export-filter-plain-text-functions
               'my-latex-filter-nobreaks
               'my-html-filter-nobreaks)

File for testing:

* Test for code readability improving

This is a test. A beginner here. !!Custom yellow markup.!! %%Custom red markup.%%
1

1 Answer 1

4

The problem with your code is that you are applying different replacement functions to the same original text. The result is, the value returned is just the result of the final replacement applied to the original text. You need to accumulate changes. One way to do this is:

  (defun my-latex-filter-highlightNotes (text backend info)
    "Highligh custom notes markup. Notes are surounded by \"%%\" or \"!!\" delimiters."
    (when (org-export-derived-backend-p backend 'latex)
      (setq text ;; update the value of text
        (replace-regexp-in-string "!!\\(.*\\)!!" "\\\\highLight[yellow]{\\1}" text))
      (setq text ;; update the value of text again!
        (replace-regexp-in-string "\\\\%\\\\%\\(.*\\)\\\\%\\\\%" "\\\\highLight[red]{\\1}" text))))
5
  • Thank you very much for the answer. I am a little confused about the fact that the first setq is needed. I understand that the second one updates text argument after first replacement, but why is it needed to update text parameter before first regexp? Mar 26, 2021 at 15:13
  • replace-regexp-in-string does not modify the string you give it. It returns a modified string, but the original remains unchanged. If you don't use the first setq, then when you call the second replace function, it is acting on the original, unchanged string. You want it to add it's changes on top of the changes made in the first replacement.
    – Tyler
    Mar 26, 2021 at 15:17
  • Think I have to read more about how is elisp code executed (evaluated), but I think I finally understand. Another thing that wasnt working for me was trying to put two when forms in one defun. Is that prohibited somehow or was I making some syntax mistake? Mar 26, 2021 at 15:28
  • There is no prohibition to having multiple when (or anything else for that matter) in one defun.
    – NickD
    Mar 26, 2021 at 16:37
  • 1
    @nickd yes, I got confused with sources in three languages
    – Tyler
    Mar 26, 2021 at 16:39

Your Answer

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

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