cursed broken code for links on genhtml

This commit is contained in:
Chaos 2023-01-18 10:38:20 +00:00
parent aadb338d75
commit f67a9ebc3b
No known key found for this signature in database
4 changed files with 45 additions and 4 deletions

7
Cargo.lock generated
View file

@ -616,6 +616,7 @@ dependencies = [
"taglib", "taglib",
"tempfile", "tempfile",
"thiserror", "thiserror",
"urlencoding",
"walkdir", "walkdir",
] ]
@ -959,6 +960,12 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]]
name = "urlencoding"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9"
[[package]] [[package]]
name = "utf8-width" name = "utf8-width"
version = "0.1.6" version = "0.1.6"

View file

@ -38,6 +38,8 @@ taglib = { path = "./modules/taglib", optional = true }
# for genhtml command # for genhtml command
html-escape = "0.2.11" html-escape = "0.2.11"
urlencoding = "2.1.2"
# error handling # error handling
thiserror = "1.0" thiserror = "1.0"

View file

@ -38,6 +38,8 @@ pub struct GenHTMLCommandArgs {
pub title: String, pub title: String,
#[clap(long, default_value = "generated by musicutil")] #[clap(long, default_value = "generated by musicutil")]
pub description: String, pub description: String,
#[clap(long)]
pub link_base: Option<String>,
} }
#[derive(Debug, Args)] #[derive(Debug, Args)]

View file

@ -4,23 +4,31 @@ use crate::types::File;
use crate::utils::formats::get_format_handler; use crate::utils::formats::get_format_handler;
use crate::utils::scan_for_music; use crate::utils::scan_for_music;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ffi::OsStr;
use std::io::Write; use std::io::Write;
use html_escape::encode_text; use html_escape::encode_text;
use urlencoding::encode as url_encode;
fn table_for_files(files: Vec<File>, includes_path: bool) -> String { fn table_for_files(files: Vec<File>, includes_path: bool, link_base: Option<String>) -> String {
let mut html_content = String::new(); let mut html_content = String::new();
let mut path_head = String::new(); let mut path_head = String::new();
if includes_path { if includes_path {
path_head.push_str("<th>Path</th>") path_head.push_str("<th>Path</th>")
} }
let mut link_head = String::new();
if link_base.is_some() {
link_head.push_str("<th>Link</th>")
}
html_content.push_str( html_content.push_str(
format!( format!(
" "
<table class=\"pure-table pure-table-horizontal\"> <table class=\"pure-table pure-table-horizontal\">
<thead> <thead>
<tr> <tr>
{}
{} {}
<th>Title</th> <th>Title</th>
<th>Artist</th> <th>Artist</th>
@ -29,7 +37,7 @@ fn table_for_files(files: Vec<File>, includes_path: bool) -> String {
</thead> </thead>
<tbody> <tbody>
", ",
path_head link_head, path_head
) )
.as_str(), .as_str(),
); );
@ -57,17 +65,39 @@ fn table_for_files(files: Vec<File>, includes_path: bool) -> String {
path_data.push_str(format!("<td>{}</td>", encode_text(&file_directory)).as_str()); path_data.push_str(format!("<td>{}</td>", 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!("<td><a href=\"{}\">🔗</a></td>", url).as_str());
}
html_content.push_str( html_content.push_str(
format!( format!(
" "
<tr class=\"{}\"> <tr class=\"{}\">
{} {}
{}
<td>{}</td> <td>{}</td>
<td>{}</td> <td>{}</td>
<td>{}</td> <td>{}</td>
</tr> </tr>
", ",
td_class, path_data, data_title, data_artist, data_format td_class, url_data, path_data, data_title, data_artist, data_format
) )
.as_str(), .as_str(),
); );
@ -138,7 +168,7 @@ pub fn genhtml_command(
.as_str(), .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("</body></html>"); html_content.push_str("</body></html>");
let file_path = std::path::PathBuf::from(genhtml_args.dest.as_str()).join("index.html"); let file_path = std::path::PathBuf::from(genhtml_args.dest.as_str()).join("index.html");