refactor: simplify links mod
This commit is contained in:
35
src/links.rs
35
src/links.rs
@@ -1,30 +1,10 @@
|
||||
use std::cell::RefCell;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use comrak::arena_tree::Node;
|
||||
use comrak::nodes::{Ast, NodeLink, NodeValue};
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub struct ParseOptions {
|
||||
pub alias_prop: Option<String>,
|
||||
pub path_root: PathBuf,
|
||||
pub to_complete_paths: bool,
|
||||
pub remove_unalised: bool,
|
||||
pub remove_unfound: bool,
|
||||
}
|
||||
impl Default for ParseOptions {
|
||||
fn default() -> Self {
|
||||
ParseOptions {
|
||||
alias_prop: None,
|
||||
path_root: PathBuf::new(),
|
||||
to_complete_paths: true,
|
||||
remove_unalised: true,
|
||||
remove_unfound: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iterate_links<'a, F>(ast: &'a Node<'a, RefCell<Ast>>, iterator: F)
|
||||
where
|
||||
F: Fn(&mut NodeLink),
|
||||
@@ -37,14 +17,17 @@ where
|
||||
});
|
||||
}
|
||||
|
||||
pub fn replace_links<'a>(ast: &'a Node<'a, RefCell<Ast>>, from: &'a str, to: &'a str) {
|
||||
iterate_links(ast, |l| {
|
||||
if l.url == from {
|
||||
l.url = String::from(to)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_links<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Vec<String> {
|
||||
let links: RefCell<Vec<String>> = RefCell::new(vec![]);
|
||||
let _ = utils::iter_nodes(ast, &|node| {
|
||||
if let NodeValue::Link(l) = &node.data.borrow().value {
|
||||
links.borrow_mut().push(l.url.clone());
|
||||
}
|
||||
Ok::<(), ()>(())
|
||||
});
|
||||
iterate_links(ast, |l| links.borrow_mut().push(l.url.clone()));
|
||||
let r = links.borrow().to_vec();
|
||||
r
|
||||
}
|
||||
|
||||
23
src/main.rs
23
src/main.rs
@@ -1,4 +1,3 @@
|
||||
use core::panic;
|
||||
use std::io::Write;
|
||||
|
||||
use clap::{ArgAction, Parser, Subcommand};
|
||||
@@ -35,9 +34,6 @@ enum Commands {
|
||||
|
||||
#[arg(short, long, num_args = 2, value_names = ["FROM", "TO"])]
|
||||
replace_url: Vec<String>,
|
||||
|
||||
#[arg(long, default_value = ".")]
|
||||
root: clio::ClioPath,
|
||||
},
|
||||
Frontmatter {
|
||||
#[command(subcommand)]
|
||||
@@ -97,28 +93,19 @@ fn main() {
|
||||
let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options());
|
||||
|
||||
let result = match cli.command {
|
||||
Commands::Links {
|
||||
list,
|
||||
root,
|
||||
replace_url,
|
||||
} => {
|
||||
Commands::Links { list, replace_url } => {
|
||||
let list = if replace_url.len() == 0 && !list {
|
||||
true
|
||||
} else {
|
||||
list
|
||||
};
|
||||
|
||||
replace_url.chunks(2).for_each(|p| {
|
||||
links::iterate_links(ast, |l| {
|
||||
if l.url == p[0] {
|
||||
l.url = (*p[1]).to_string()
|
||||
}
|
||||
})
|
||||
});
|
||||
replace_url
|
||||
.chunks(2)
|
||||
.for_each(|p| links::replace_links(ast, &p[0], &p[1]));
|
||||
|
||||
if list {
|
||||
let links = links::get_links(ast);
|
||||
cli::ResultType::List(links)
|
||||
cli::ResultType::List(links::get_links(ast))
|
||||
} else {
|
||||
let mut str = vec![];
|
||||
match comrak::format_commonmark(ast, &utils::default_options(), &mut str) {
|
||||
|
||||
Reference in New Issue
Block a user