Converting Org Links to Gemini Links in Any Buffer (publ. 2024-12-13)

I like writing my gemlog posts directly in gemtext format, rather than writing them in Org format, and then converting to Gemini, like some people do. Gemtext has a pleasant, simple markup that is natural to write in, and that looks good in Gemini mode.

Sometimes though, especially when writing up my "News and Links Digests", I copy and paste a lot of Org Links from my references collection, which is an Org document.

One trick would be to convert the Org document over to gemini, using emacs-ox-gemini, and then copy the links out of there, or some slight variation of that. But I didn't like the extra steps involved in that approach.

Another approach I tried was writing a function that used replace-regexp. This generally worked, but it relied on regular expressions. The problem with that is that the Org link format uses (potentially) escaped characters, since the ?\[, ?\], and ?\\ characters ("[", "]", and "\") might potentially need to be included in the link string or link description string. Regular expressions do not handle escaped characters very well. See this thread:

Regular expressions and user-escaped characters

I just wasn't happy with the knowledge that potentially my conversion function could choke on some valid Org link line. So over a few lunchbreaks, I came up with an approach that parses like a state machine. It has been a while since my computer science classes, so I'm not quite sure if technically it is a state machine, but I think it works like one. Here is the core function that does the actually parsing:

There is definitely room for optimization there. But it works fast enough for human use. I imagine there is a better approach that just using "concat" and "string" to append a character to the end of a string, but I haven't looked into it yet. And I think there are a few places where logic could be simplified.

Here is a more convenient wrapper function for that:

Here is the function that applies the parsing code to each line in the buffer:

So, how I use this is to just copy and paste org links into my gemlog post, whereever I want them. And then right before publishing, I run M-x org-to-gemini-links-in-buffer, in the same buffer as my gemlog post.

This article © 2024 by Christopher Howard is licensed under Attribution-ShareAlike 4.0 International.

CC BY-SA 4.0 Deed

The elisp code in this article is © 2024 by Christopher Howard, and is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The elisp code in this article is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Licenses

Proxied content from gemini://gem.librehacker.com/gemlog/starlog/20241213-1.gmi

Gemini request details:

Original URL
gemini://gem.librehacker.com/gemlog/starlog/20241213-1.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.