From d683589353d34cf80c79dd58a82fb52454cb3d90 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 22 May 2023 05:27:27 +0000 Subject: refactor(src): rename files --- src/gemini_to_html.rs | 153 -------------------------------------------------- src/html.rs | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/response.rs | 3 +- 4 files changed, 155 insertions(+), 156 deletions(-) delete mode 100644 src/gemini_to_html.rs create mode 100644 src/html.rs (limited to 'src') diff --git a/src/gemini_to_html.rs b/src/gemini_to_html.rs deleted file mode 100644 index 64ad5d8..0000000 --- a/src/gemini_to_html.rs +++ /dev/null @@ -1,153 +0,0 @@ -// This file is part of September . -// -// This program 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, version 3. -// -// This program 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. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// -// Copyright (C) 2022-2023 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use std::env::var; - -use germ::ast::Node; -use gmi::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)] -pub fn gemini_to_html( - response: &gmi::protocol::Response, - url: &Url, - is_proxy: bool, -) -> (String, String) { - let ast_tree = - germ::ast::Ast::from_string(String::from_utf8_lossy(&response.data)); - let ast = ast_tree.inner(); - let mut html = String::new(); - let mut title = String::new(); - - for node in ast { - match node { - Node::Text(text) => html.push_str(&format!("

{text}

")), - Node::Link { to, text } => { - let mut href = to.clone(); - let mut surface = false; - - if href.contains("://") && !href.starts_with("gemini://") { - surface = true; - } else if !href.starts_with("gemini://") && !href.starts_with('/') { - href = format!("./{href}"); - } else if href.starts_with('/') || !href.contains("://") { - href = link_from_host_href(url, &href); - } - - if var("PROXY_BY_DEFAULT") - .unwrap_or_else(|_| "true".to_string()) - .to_lowercase() - == "true" - && href.contains("gemini://") - && !surface - { - if is_proxy - || href - .trim_start_matches("gemini://") - .trim_end_matches('/') - .split('/') - .collect::>() - .first() - .unwrap() - != &url.authority.host.as_str() - { - href = format!("/proxy/{}", href.trim_start_matches("gemini://")); - } else { - href = href - .trim_start_matches("gemini://") - .replace(&url.authority.host, ""); - } - } - - if let Ok(keeps) = var("KEEP_GEMINI_EXACT") { - let mut keeps = keeps.split(','); - - if (href.starts_with('/') || !href.contains("://")) && !surface { - let temporary_href = link_from_host_href(url, &href); - - if keeps.any(|k| k == &*temporary_href) { - href = temporary_href; - } - } - } - - if let Ok(keeps) = var("KEEP_GEMINI_DOMAIN") { - if (href.starts_with('/') - || !href.contains("://") - && keeps.split(',').any(|k| k == &*url.authority.host)) - && !surface - { - href = link_from_host_href(url, &href); - } - } - - html.push_str(&format!( - "

{}

\n", - href, - text.clone().unwrap_or_default(), - )); - } - Node::Heading { level, text } => { - if title.is_empty() && *level == 1 { - title = text.clone(); - } - - html.push_str(&format!( - "<{}>{}", - match level { - 1 => "h1", - 2 => "h2", - 3 => "h3", - _ => "p", - }, - text, - )); - } - Node::List(items) => html.push_str(&format!( - "
    {}
", - items - .iter() - .map(|i| format!("
  • {i}
  • ")) - .collect::>() - .join("\n") - )), - Node::Blockquote(text) => { - html.push_str(&format!("
    {text}
    ")); - } - Node::PreformattedText { text, .. } => { - html.push_str(&format!("
    {text}
    ")); - } - Node::Whitespace => {} - } - } - - (title, html) -} diff --git a/src/html.rs b/src/html.rs new file mode 100644 index 0000000..2026272 --- /dev/null +++ b/src/html.rs @@ -0,0 +1,153 @@ +// This file is part of September . +// +// This program 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, version 3. +// +// This program 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. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// Copyright (C) 2022-2023 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::env::var; + +use germ::ast::Node; +use gmi::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)] +pub fn from_gemini( + response: &gmi::protocol::Response, + url: &Url, + is_proxy: bool, +) -> (String, String) { + let ast_tree = + germ::ast::Ast::from_string(String::from_utf8_lossy(&response.data)); + let ast = ast_tree.inner(); + let mut html = String::new(); + let mut title = String::new(); + + for node in ast { + match node { + Node::Text(text) => html.push_str(&format!("

    {text}

    ")), + Node::Link { to, text } => { + let mut href = to.clone(); + let mut surface = false; + + if href.contains("://") && !href.starts_with("gemini://") { + surface = true; + } else if !href.starts_with("gemini://") && !href.starts_with('/') { + href = format!("./{href}"); + } else if href.starts_with('/') || !href.contains("://") { + href = link_from_host_href(url, &href); + } + + if var("PROXY_BY_DEFAULT") + .unwrap_or_else(|_| "true".to_string()) + .to_lowercase() + == "true" + && href.contains("gemini://") + && !surface + { + if is_proxy + || href + .trim_start_matches("gemini://") + .trim_end_matches('/') + .split('/') + .collect::>() + .first() + .unwrap() + != &url.authority.host.as_str() + { + href = format!("/proxy/{}", href.trim_start_matches("gemini://")); + } else { + href = href + .trim_start_matches("gemini://") + .replace(&url.authority.host, ""); + } + } + + if let Ok(keeps) = var("KEEP_GEMINI_EXACT") { + let mut keeps = keeps.split(','); + + if (href.starts_with('/') || !href.contains("://")) && !surface { + let temporary_href = link_from_host_href(url, &href); + + if keeps.any(|k| k == &*temporary_href) { + href = temporary_href; + } + } + } + + if let Ok(keeps) = var("KEEP_GEMINI_DOMAIN") { + if (href.starts_with('/') + || !href.contains("://") + && keeps.split(',').any(|k| k == &*url.authority.host)) + && !surface + { + href = link_from_host_href(url, &href); + } + } + + html.push_str(&format!( + "

    {}

    \n", + href, + text.clone().unwrap_or_default(), + )); + } + Node::Heading { level, text } => { + if title.is_empty() && *level == 1 { + title = text.clone(); + } + + html.push_str(&format!( + "<{}>{}", + match level { + 1 => "h1", + 2 => "h2", + 3 => "h3", + _ => "p", + }, + text, + )); + } + Node::List(items) => html.push_str(&format!( + "
      {}
    ", + items + .iter() + .map(|i| format!("
  • {i}
  • ")) + .collect::>() + .join("\n") + )), + Node::Blockquote(text) => { + html.push_str(&format!("
    {text}
    ")); + } + Node::PreformattedText { text, .. } => { + html.push_str(&format!("
    {text}
    ")); + } + Node::Whitespace => {} + } + } + + (title, html) +} diff --git a/src/main.rs b/src/main.rs index 85eb228..a1ffb08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ #![recursion_limit = "128"] #![allow(clippy::cast_precision_loss)] -mod gemini_to_html; +mod html; mod response; mod url; diff --git a/src/response.rs b/src/response.rs index 8e335a0..83bdc1e 100644 --- a/src/response.rs +++ b/src/response.rs @@ -126,8 +126,7 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".< } ) }; - let gemini_html = - crate::gemini_to_html::gemini_to_html(&response, &url, is_proxy); + let gemini_html = crate::html::from_gemini(&response, &url, is_proxy); let gemini_title = gemini_html.0; let convert_time_taken = timer.elapsed(); -- cgit v1.2.3