gemini:// to Gemini Portal

siiky

2022/08/21

2022/08/21

en

I just modified my gmi->md script to rewrite gemini:// links to the Gemini Portal. With only 8 new (readable) lines the HTTPS/HTML version of the capsule became that much user friendlier. A handful of links are now broken in the process (gemini://localhost links from the gemini-ipfs-gateway) but no problem, they weren't valid anyway.

Pretty happy with the results and with how easy it was. Good thing Gemtext is so simple.

Here's the diff:

--- a/gmi2md.scm
+++ b/gmi2md.scm
@@ -4,15 +4,30 @@
   (chicken io)
   (chicken pathname)
   (chicken process-context)
+  (srfi 13)
   (srfi 197)
   gmi)

 (define-constant source-extensions '("gmi" "md" "org"))
 (define-constant image-extensions '("svg" "png" "jpg" "jpeg" "webp"))
+(define-constant gemini:// "gemini://")

 (define ((? p? f g) x) ((if (p? x) f g) x))
 (define phi (cute ? <> <> identity))

+(define (gemini-link? l)
+  (and (gmi:link? l)
+       (string-prefix? gemini:// (gmi:link:uri l))))
+
+
+(define (gemini->portal l)
+  (gmi:link (chain (gmi:link:uri l)
+                   (substring/shared _ (string-length gemini://))
+                   (string-append "https://portal.mozz.us/gemini/" _))
+            ((phi (o not string-null?)
+                  (cute string-append "(Gemini Portal) " <>))
+             (gmi:link:text l))))
+

 (define ((convert? gemini-root) l)
   (and (gmi:link? l)
@@ -98,11 +113,14 @@
        ""))
     ))

+(define (rewrite-links gemini-root)
+  (o (phi gemini-link? gemini->portal)
+     (phi (convert? gemini-root) extension/gmi->html)))

 (define (main args)
   (let ((gemini-root (make-absolute-pathname (current-directory) (car args))))
     (chain (gmi:read)
-           (map (phi (convert? gemini-root) extension/gmi->html) _)
+           (map (rewrite-links gemini-root) _)
            (group-links _)
            (map grouped-gmi-element->md-element _)
            (concatenate _)