From 4ae6bdb1e31a23778d7547a27686a64d40900cf0 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 1 Apr 2022 12:19:56 +0000 Subject: fix(gmi_to_html): support project gemini's interesting style --- src/main.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index e4877db..7925b57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,21 @@ use std::{env::var, time::Instant}; use actix_web::{web, Error, HttpResponse}; use gmi::{protocol::Response, url::Url}; +fn link_from_host_href(url: &Url, href: &str) -> String { + format!( + "gemini://{}{}{}", + url.authority.host, + { + if href.starts_with('/') { + "" + } else { + "/" + } + }, + href + ) +} + #[allow(clippy::too_many_lines)] fn gemini_to_html( response: &Response, @@ -52,12 +67,12 @@ fn gemini_to_html( // Convert links "=" => { let line = line.replace("=>", "").trim_start().to_owned(); - let mut split = line.split(' ').collect::>(); + let mut split = line.split_whitespace().collect::>(); let mut href = split.remove(0).to_string(); let text = split.join(" "); - if href.starts_with('/') { - href = format!("gemini://{}{}", url.authority.host, href); + if href.starts_with('/') || !href.contains("://") { + href = link_from_host_href(url, &href); } if var("PROXY_BY_DEFAULT").unwrap_or_else(|_| "true".to_string()) @@ -85,9 +100,8 @@ fn gemini_to_html( if let Ok(keeps) = var("KEEP_GEMINI_EXACT") { let mut keeps = keeps.split(','); - if href.starts_with('/') { - let temporary_href = - format!("gemini://{}{}", url.authority.host, href); + if href.starts_with('/') || !href.contains("://") { + let temporary_href = link_from_host_href(url, &href); if keeps.any(|k| k == &*temporary_href) { href = temporary_href; @@ -97,9 +111,10 @@ fn gemini_to_html( if let Ok(keeps) = var("KEEP_GEMINI_DOMAIN") { if href.starts_with('/') - && keeps.split(',').any(|k| k == &*url.authority.host) + || !href.contains("://") + && keeps.split(',').any(|k| k == &*url.authority.host) { - href = format!("gemini://{}{}", url.authority.host, href); + href = link_from_host_href(url, &href); } } -- cgit v1.2.3