generate index files with given filename, disable by default

This commit is contained in:
yggverse 2025-02-12 19:53:21 +02:00
parent dbd9fa3960
commit dd91aa054d
3 changed files with 27 additions and 25 deletions

View file

@ -20,7 +20,7 @@ cargo install pulsarss
## Launch ## Launch
``` bash ``` bash
pulsarss --source https://path/to/feed.rss pulsarss --source https://path/to/feed.rss --index index.gmi
``` ```
### Options ### Options
@ -28,7 +28,7 @@ pulsarss --source https://path/to/feed.rss
* `source`, `s` - RSS feed source (required) * `source`, `s` - RSS feed source (required)
* `target`, `t` - Destination directory (`public` by default) * `target`, `t` - Destination directory (`public` by default)
* `update`, `u` - Update timeout in seconds (`60` 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) * `limit`, `l` - Limit channel items (unlimited by default)
* `output`, `o` - Print output (`dw` by default): * `output`, `o` - Print output (`dw` by default):
* `d` - debug * `d` - debug
@ -49,7 +49,7 @@ After=network.target
Type=simple Type=simple
User=pulsarss User=pulsarss
Group=pulsarss Group=pulsarss
ExecStart=pulsarss -s https://path/to/feed.rss ExecStart=pulsarss -s https://path/to/feed.rss -i index.gmi
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View file

@ -3,9 +3,9 @@ use clap::Parser;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct Argument { pub struct Argument {
/// Generate index files (`index.gmi` by default, empty to disable) /// Generate index files with given filename (disabled by default)
#[arg(short, long, default_value_t = String::from("index.gmi"))] #[arg(short, long)]
pub index: String, pub index: Option<String>,
/// Limit channel items (unlimited by default) /// Limit channel items (unlimited by default)
#[arg(short, long, default_value_t = 0)] #[arg(short, long, default_value_t = 0)]

View file

@ -94,36 +94,38 @@ fn crawl(argument: &Argument, output: &Output) -> Result<(), Box<dyn Error>> {
File::create(destination.item())?.write_all(data.join("\n\n").as_bytes())?; File::create(destination.item())?.write_all(data.join("\n\n").as_bytes())?;
if !argument.index.is_empty() { if !argument.index.is_none() {
index.insert(destination.path); // request index file update index.insert(destination.path); // request index file update in this location
} }
} }
// renew pending index files on items crawl completed // renew pending index files on items crawl completed
for path in index { if let Some(ref index_argument) = argument.index {
let subject = format!("{path}{}", argument.index); for path in index {
let subject = format!("{path}{index_argument}");
let mut index = File::create(&subject)?; let mut index = File::create(&subject)?;
let mut data = Vec::with_capacity(argument.limit); let mut data = Vec::with_capacity(argument.limit);
let mut total = 0; let mut total = 0;
for file in read_dir(&path)? { for file in read_dir(&path)? {
let name = file?.file_name().into_string().unwrap(); let name = file?.file_name().into_string().unwrap();
if name == argument.index { if &name == index_argument {
continue; 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 index.write_all(data.join("\n\n").as_bytes())?;
File::open(format!("{path}{name}"))?.read_to_string(&mut buffer)?;
data.push(buffer);
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 // print totals