From f67a9ebc3bfdc44d596397fb381e392eee8e6e06 Mon Sep 17 00:00:00 2001 From: Chaos Date: Wed, 18 Jan 2023 10:38:20 +0000 Subject: [PATCH] cursed broken code for links on genhtml --- Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ src/args.rs | 2 ++ src/commands/genhtml.rs | 38 ++++++++++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f75c3a..337d6e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -616,6 +616,7 @@ dependencies = [ "taglib", "tempfile", "thiserror", + "urlencoding", "walkdir", ] @@ -959,6 +960,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" +[[package]] +name = "urlencoding" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" + [[package]] name = "utf8-width" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index e1f0a5d..317c6bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,8 @@ taglib = { path = "./modules/taglib", optional = true } # for genhtml command html-escape = "0.2.11" +urlencoding = "2.1.2" + # error handling thiserror = "1.0" diff --git a/src/args.rs b/src/args.rs index 0812430..17bd51e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -38,6 +38,8 @@ pub struct GenHTMLCommandArgs { pub title: String, #[clap(long, default_value = "generated by musicutil")] pub description: String, + #[clap(long)] + pub link_base: Option, } #[derive(Debug, Args)] diff --git a/src/commands/genhtml.rs b/src/commands/genhtml.rs index 0cb3a0e..7e096f6 100644 --- a/src/commands/genhtml.rs +++ b/src/commands/genhtml.rs @@ -4,23 +4,31 @@ use crate::types::File; use crate::utils::formats::get_format_handler; use crate::utils::scan_for_music; use std::cmp::Ordering; +use std::ffi::OsStr; use std::io::Write; use html_escape::encode_text; +use urlencoding::encode as url_encode; -fn table_for_files(files: Vec, includes_path: bool) -> String { +fn table_for_files(files: Vec, includes_path: bool, link_base: Option) -> String { let mut html_content = String::new(); let mut path_head = String::new(); if includes_path { path_head.push_str("Path") } + let mut link_head = String::new(); + if link_base.is_some() { + link_head.push_str("Link") + } + html_content.push_str( format!( " + {} {} @@ -29,7 +37,7 @@ fn table_for_files(files: Vec, includes_path: bool) -> String { ", - path_head + link_head, path_head ) .as_str(), ); @@ -57,17 +65,39 @@ fn table_for_files(files: Vec, includes_path: bool) -> String { path_data.push_str(format!("", encode_text(&file_directory)).as_str()); } + + let mut url_data = String::new(); + if link_base.is_some() { + let mut url = String::new(); + let link_base_str = link_base.as_ref().unwrap().clone(); + url.push_str(link_base_str.as_str()); + url.push('/'); + + let file_path = file.join_path_from_source(); + let file_path: Vec<&OsStr> = file_path.iter().collect(); + for i in 0..(file_path.len()) { + url.push_str(url_encode(file_path.get(i).unwrap().to_str().unwrap()).to_string().as_str()); + if i != file_path.len()-1 { + url.push('/'); + } + } + + + url_data.push_str(format!("", url).as_str()); + } + html_content.push_str( format!( " {} + {} ", - td_class, path_data, data_title, data_artist, data_format + td_class, url_data, path_data, data_title, data_artist, data_format ) .as_str(), ); @@ -138,7 +168,7 @@ pub fn genhtml_command( .as_str(), ); - html_content.push_str(&table_for_files(files, true)); + html_content.push_str(&table_for_files(files, true, genhtml_args.link_base.clone())); html_content.push_str(""); let file_path = std::path::PathBuf::from(genhtml_args.dest.as_str()).join("index.html");
Title Artist
{}🔗
{} {} {}