// This file is part of Locus . // // 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 { crate::{ response::success, route::track_mount, url::{ROOT_GEMINI_URL, ROOT_HTTPS_URL}, }, windmark::response::Response, }; fn error(message: &str, context: &windmark::context::RouteContext) -> Response { success( &format!( "# World Wide Web to Gemini Gateway\n\n{message}\n\n=> /web Go back" ), context, ) } pub fn module(router: &mut windmark::router::Router) { track_mount(router, "/web", "World Wide Web to Gemini Gateway", |context| { success( &format!( r"# World Wide Web to Gemini Gateway To use this gateway, simply append the address of your target resource to the end of the current route, minus the protocol. To visit the web version of this exact page, <{ROOT_HTTPS_URL}/web>, you would visit <{ROOT_GEMINI_URL}/web/fuwn.me/web>. => /web/fuwn.me/web Try it!" ), &context, ) }); track_mount( router, "/web/*url", "World Wide Web to Gemini Gateway Visitor", async move |context| { Response::success({ let Ok(url) = url::Url::parse(&format!( "https://{}", if let Some(url_choice) = context.parameters.get("url") { url_choice } else { return error("Looks like you didn't provide a URL!", &context); } )) else { return error("The URL you provided is invalid.", &context); }; let mut contents = vec![]; let website = if let Ok(website) = reqwest::get(url.clone()).await { if let Ok(text) = website.text().await { text } else { return error( "The website you provided could not be reached.", &context, ); } } else { return error( "The website you provided could not be reached.", &context, ); }; let Ok(dom) = tl::parse(&website, tl::ParserOptions::default()) else { return error( "The website you provided could not be properly parsed.", &context, ); }; let parser = dom.parser(); let mut nodes = dom.nodes().iter().peekable(); while let Some(element) = nodes.next() { let mut text = String::new(); let () = element.as_tag().map_or((), |tag| { match tag.name().as_utf8_str().to_string().as_str() { "p" => { if tag.inner_html(parser).contains(">() .first() .unwrap_or(&"A parse error occurred in this location.") )); } "a" => { contents.pop(); contents.push(format!( "=> {} {}\n{}", tag.attributes().get("href").flatten().map_or( "A parse error occurred in this location.".to_string(), |href| href.as_utf8_str().to_string() ), tag.inner_text(parser), nodes.peek().map_or("", |next_node| { next_node.as_tag().map_or("", |next_tag| { if next_tag.name().as_utf8_str().to_string().as_str() == "a" { "" } else { "\n" } }) }) )); } "h1" => { contents.push(format!( "{}# {}\n\n", if contents.is_empty() { "\n" } else { "" }, tag.inner_text(parser) )); } "h2" => { contents.push(format!("\n## {}\n\n", tag.inner_text(parser))); } "h3" => { contents.push(format!("\n### {}\n\n", tag.inner_text(parser))); } "pre" => { contents.push(format!("```\n{}```\n", tag.inner_text(parser))); } "blockquote" => { contents.push(format!("> {}\n\n", tag.inner_text(parser))); } "li" => { contents.push(format!("* {}\n", tag.inner_text(parser))); } "img" => { contents.push(format!( "=> {} {}\n\n", tag.attributes().get("src").flatten().map_or( "A parse error occurred in this location.".to_string(), |src| src.as_utf8_str().to_string() ), tag.attributes().get("alt").flatten().map_or( "A parse error occurred in this location.".to_string(), |alt| alt.as_utf8_str().to_string() ), )); } "html" | "head" | "script" | "link" | "title" | "body" | "ul" | "style" => {} _ => { text = tag.inner_text(parser).to_string(); if !(text.contains("Proxy Information") || text.contains("Proxied content from")) { contents.push(format!("{text}\n\n")); } } } }); if text.contains("Proxy Information") || text.contains("Proxied content from") { break; } } contents.join("") }) }, ); }