rename -D to -d, allow multiple occurrences

This commit is contained in:
Sunshine 2022-08-29 20:13:28 -04:00
parent 3fa3a0222a
commit 145550e637
No known key found for this signature in database
GPG key ID: B80CA68703CD8AB1
3 changed files with 30 additions and 23 deletions

View file

@ -113,7 +113,7 @@ cat index.html | monolith -aIiFfcMv -b https://original.site/ - > result.html
- `-b`: Use custom `base URL`
- `-c`: Exclude CSS
- `-C`: Save document using custom `charset`
- `-D`: Allow retrieving assets only from specified `domain(s)`
- `-d`: Allow retrieving assets only from specified `domain(s)`
- `-e`: Ignore network errors
- `-E`: Exclude all assets located within domains specified in whitelist
- `-f`: Omit frames

View file

@ -1,4 +1,4 @@
use clap::{App, Arg};
use clap::{App, Arg, ArgAction};
use std::env;
#[derive(Default)]
@ -52,9 +52,17 @@ impl Options {
.args_from_usage("-b, --base-url=[http://localhost/] 'Sets custom base URL'")
.args_from_usage("-c, --no-css 'Removes CSS'")
.args_from_usage("-C, --charset=[UTF-8] 'Enforces custom encoding'")
.args_from_usage("-D, --domains=[bad.org,ads.site] 'Whitelist of domains'")
.arg(
Arg::with_name("domains")
.short('d')
.long("domains")
.takes_value(true)
.value_name("DOMAINS")
.action(ArgAction::Append)
.help("Whitelist of domains"),
)
.args_from_usage("-e, --ignore-errors 'Ignore network errors'")
.args_from_usage("-E, --exclude-domains 'Treat list of specified domains as blacklist'")
.args_from_usage("-E, --exclude-domains 'Treat specified domains as blacklist'")
.args_from_usage("-f, --no-frames 'Removes frames and iframes'")
.args_from_usage("-F, --no-fonts 'Removes fonts'")
.args_from_usage("-i, --no-images 'Removes images'")
@ -95,8 +103,16 @@ impl Options {
if let Some(charset) = app.value_of("charset") {
options.charset = Some(charset.to_string());
}
if let Some(domains) = app.value_of("domains") {
options.domains = Some(domains.split(",").map(|s| s.to_string()).collect());
if let Some(domains) = app.get_many::<String>("domains") {
let mut final_list_of_domains: Vec<String> = Vec::new();
let provided_arguments: Vec<&str> = domains.map(|v| v.as_str()).collect::<Vec<_>>();
for provided_argument in provided_arguments {
let comma_separated_domains: Vec<&str> = provided_argument.split(",").collect();
for comma_separated_domain in comma_separated_domains {
final_list_of_domains.push(comma_separated_domain.trim().to_string());
}
}
options.domains = Some(final_list_of_domains);
}
options.ignore_errors = app.is_present("ignore-errors");
options.exclude_domains = app.is_present("exclude-domains");

View file

@ -266,25 +266,16 @@ pub fn retrieve_asset(
"".to_string(),
))
} else {
if options.exclude_domains {
if let Some(domains) = &options.domains {
if domains
let domain_matches = domains
.iter()
.any(|d| domain_is_within_domain(url.host_str().unwrap(), &d.trim()))
.any(|d| domain_is_within_domain(url.host_str().unwrap(), &d.trim()));
if (options.exclude_domains && domain_matches)
|| (!options.exclude_domains && !domain_matches)
{
return Err(client.get("").send().unwrap_err());
}
}
} else {
if let Some(domains) = &options.domains {
if domains
.iter()
.any(|d| !domain_is_within_domain(url.host_str().unwrap(), &d.trim()))
{
return Err(client.get("").send().unwrap_err());
}
}
}
// URL not in cache, we retrieve the file
match client.get(url.as_str()).send() {