Add option to filter links on reexport
This commit is contained in:
parent
76abf5ea6e
commit
159f155e4a
2 changed files with 42 additions and 4 deletions
|
|
@ -2,16 +2,44 @@ use std::fs::File;
|
||||||
use std::io::{self, BufReader, BufWriter};
|
use std::io::{self, BufReader, BufWriter};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use crate::data::adjacency_list::AdjacencyList;
|
||||||
use crate::data::store;
|
use crate::data::store;
|
||||||
|
|
||||||
pub fn reexport(from: &Path, to: &Path) -> io::Result<()> {
|
pub fn reexport(
|
||||||
|
from: &Path,
|
||||||
|
to: &Path,
|
||||||
|
in_parens: Option<bool>,
|
||||||
|
in_structure: Option<bool>,
|
||||||
|
) -> io::Result<()> {
|
||||||
eprintln!(">> Import");
|
eprintln!(">> Import");
|
||||||
let mut from = BufReader::new(File::open(from)?);
|
let mut from = BufReader::new(File::open(from)?);
|
||||||
let data = store::read_adjacency_list(&mut from)?;
|
let mut data = store::read_adjacency_list(&mut from)?;
|
||||||
|
|
||||||
eprintln!(">> Consistency check");
|
eprintln!(">> Consistency check");
|
||||||
data.check_consistency();
|
data.check_consistency();
|
||||||
|
|
||||||
|
if in_parens.is_some() || in_structure.is_some() {
|
||||||
|
eprintln!(">> Filtering");
|
||||||
|
|
||||||
|
let mut data2 = AdjacencyList::default();
|
||||||
|
for (page_idx, page) in data.pages() {
|
||||||
|
data2.push_page(page.data.clone());
|
||||||
|
for (_, link) in data.links(page_idx) {
|
||||||
|
if in_parens.is_some_and(|v| v != link.data.in_parens()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if in_structure.is_some_and(|v| v != link.data.in_structure()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
data2.push_link(link.to, link.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data = data2;
|
||||||
|
}
|
||||||
|
|
||||||
eprintln!(">> Export");
|
eprintln!(">> Export");
|
||||||
let mut to = BufWriter::new(File::create(to)?);
|
let mut to = BufWriter::new(File::create(to)?);
|
||||||
store::write_adjacency_list(&data, &mut to)?;
|
store::write_adjacency_list(&data, &mut to)?;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,13 @@ enum Command {
|
||||||
/// Read sift data on stdin and output brood data.
|
/// Read sift data on stdin and output brood data.
|
||||||
Ingest,
|
Ingest,
|
||||||
/// Read and reexport brood data.
|
/// Read and reexport brood data.
|
||||||
Reexport { to: PathBuf },
|
Reexport {
|
||||||
|
to: PathBuf,
|
||||||
|
#[arg(long, short = 'P')]
|
||||||
|
in_parens: Option<bool>,
|
||||||
|
#[arg(long, short = 'S')]
|
||||||
|
in_structure: Option<bool>,
|
||||||
|
},
|
||||||
/// Find a path from one article to another.
|
/// Find a path from one article to another.
|
||||||
Path {
|
Path {
|
||||||
from: String,
|
from: String,
|
||||||
|
|
@ -38,7 +44,11 @@ fn main() -> io::Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
match args.command {
|
match args.command {
|
||||||
Command::Ingest => commands::ingest::ingest(&args.datafile),
|
Command::Ingest => commands::ingest::ingest(&args.datafile),
|
||||||
Command::Reexport { to } => commands::reexport::reexport(&args.datafile, &to),
|
Command::Reexport {
|
||||||
|
to,
|
||||||
|
in_parens,
|
||||||
|
in_structure,
|
||||||
|
} => commands::reexport::reexport(&args.datafile, &to, in_parens, in_structure),
|
||||||
Command::Path { from, to, flip } => {
|
Command::Path { from, to, flip } => {
|
||||||
if flip {
|
if flip {
|
||||||
commands::path::path(&args.datafile, &to, &from)
|
commands::path::path(&args.datafile, &to, &from)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue