0

I'm trying to export a bunch of org entries with numerous internal links to an external website. These links get broken during the export, but I can mostly recreate the correct URLs with some simple Regex. I say "mostly", because I would like the text between the <a></a> HTML tags to be downcased unless the second word is capitalized, but I am unable to express this in my function. Here is what I have:

(defun my/function ()
  (interactive)
  (org-narrow-to-subtree)
  (org-html-export-as-html)
  ;; repair broken links
  (while (re-search-forward "\\(\\[BROKEN LINK: \\*\\)\\(.*?\\)\\(]\\)" nil t)
    (replace-match (concat "<A href=\"https://mywebsite.org/"
                   (downcase (replace-regexp-in-string "[ ,]" "-" (match-string 2))) ; replace spaces, commas with hyphens
                   "\">"
                   ;; RELEVANT CODE BELOW
                   (if (let ((case-fold-search nil))
                     (string-match ".* [A-Z]" (match-string 2)))
                       (match-string 2)
                     (downcase (match-string 2)))
                   ;; RELEVANT CODE ABOVE
                   "</A>")))
  (browse-url-of-buffer))

When I run this function, things work as intended if no links in the org heading(s) being exported satisfy the condition. However, if the condition is satisfied, I get the error args out of range:0, 6.

4
  • Why are there broken links?
    – NickD
    Jun 24, 2021 at 15:34
  • Because they are internal links to other headings, of the form [[id:5437B089-FEE2-46A7-9520-BE8E44C1FED4][link]].
    – Pablo
    Jun 24, 2021 at 15:59
  • The function turns these links into something like <a href="https://website.org/link">link</a>, and this works fine, except that there are some special cases where the text between the tags should appear capitalized, and I would like to know why the if statement in the function isn't working for these cases.
    – Pablo
    Jun 24, 2021 at 16:02
  • For debugging, do M-x toggle-debug-on-error and rerun. The resulting backtrace should tell you which function exactly gets the error, so you can look up its doc string and try to figure out what happened. The repeated use of (match-string 2) looks suspect to me, but I have not really looked at the code carefully.
    – NickD
    Jun 24, 2021 at 19:09

0

Your Answer

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

Browse other questions tagged or ask your own question.