From dd91aa054d60f1e966b0014afacd7996929aaac3 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 12 Feb 2025 19:53:21 +0200 Subject: [PATCH] generate index files with given filename, disable by default --- README.md | 6 +++--- src/argument.rs | 6 +++--- src/main.rs | 40 +++++++++++++++++++++------------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d63680e..4648399 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ cargo install pulsarss ## Launch ``` bash -pulsarss --source https://path/to/feed.rss +pulsarss --source https://path/to/feed.rss --index index.gmi ``` ### Options @@ -28,7 +28,7 @@ pulsarss --source https://path/to/feed.rss * `source`, `s` - RSS feed source (required) * `target`, `t` - Destination directory (`public` by default) * `update`, `u` - Update timeout in seconds (`60` by default) -* `index`, `i` - Generate index files (`index.gmi` by default, empty to disable) +* `index`, `i` - Generate index files with given filename (disabled by default) * `limit`, `l` - Limit channel items (unlimited by default) * `output`, `o` - Print output (`dw` by default): * `d` - debug @@ -49,7 +49,7 @@ After=network.target Type=simple User=pulsarss Group=pulsarss -ExecStart=pulsarss -s https://path/to/feed.rss +ExecStart=pulsarss -s https://path/to/feed.rss -i index.gmi [Install] WantedBy=multi-user.target diff --git a/src/argument.rs b/src/argument.rs index e108270..2d3b7a7 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -3,9 +3,9 @@ use clap::Parser; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub struct Argument { - /// Generate index files (`index.gmi` by default, empty to disable) - #[arg(short, long, default_value_t = String::from("index.gmi"))] - pub index: String, + /// Generate index files with given filename (disabled by default) + #[arg(short, long)] + pub index: Option, /// Limit channel items (unlimited by default) #[arg(short, long, default_value_t = 0)] diff --git a/src/main.rs b/src/main.rs index fea627a..1e6722c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,36 +94,38 @@ fn crawl(argument: &Argument, output: &Output) -> Result<(), Box> { File::create(destination.item())?.write_all(data.join("\n\n").as_bytes())?; - if !argument.index.is_empty() { - index.insert(destination.path); // request index file update + if !argument.index.is_none() { + index.insert(destination.path); // request index file update in this location } } // renew pending index files on items crawl completed - for path in index { - let subject = format!("{path}{}", argument.index); + if let Some(ref index_argument) = argument.index { + for path in index { + let subject = format!("{path}{index_argument}"); - let mut index = File::create(&subject)?; - let mut data = Vec::with_capacity(argument.limit); + let mut index = File::create(&subject)?; + let mut data = Vec::with_capacity(argument.limit); - let mut total = 0; - for file in read_dir(&path)? { - let name = file?.file_name().into_string().unwrap(); + let mut total = 0; + for file in read_dir(&path)? { + let name = file?.file_name().into_string().unwrap(); - if name == argument.index { - continue; + if &name == index_argument { + continue; + } + + let mut buffer = String::from("##"); // correct heading levels + File::open(format!("{path}{name}"))?.read_to_string(&mut buffer)?; + data.push(buffer); + + total += 1; } - let mut buffer = String::from("##"); // correct heading levels - File::open(format!("{path}{name}"))?.read_to_string(&mut buffer)?; - data.push(buffer); + index.write_all(data.join("\n\n").as_bytes())?; - total += 1; + output.debug(&format!("renew `{subject}` (total: {total})")); } - - index.write_all(data.join("\n\n").as_bytes())?; - - output.debug(&format!("renew `{subject}` (total: {total})")); } // print totals