#![deny(unsafe_code)]
pub mod external;
pub mod history;
pub mod index;
pub mod link;
#[cfg(test)]
mod corpus_tests;
#[cfg(test)]
mod open_tests;
#[cfg(test)]
mod tests;
use std::path::{Path, PathBuf};
use crate::md::{self, Document};
use crate::plat::{path as ppath, Platform};
use crate::source::csv::CsvSource;
use crate::source::detect::{self, Format};
use crate::source::dir::DirSource;
use crate::source::json::JsonSource;
use crate::source::jsonl::JsonlSource;
use crate::source::markdown::MarkdownSource;
use crate::source::text::TextSource;
use crate::source::Source;
use history::{History, Snapshot};
use index::Entry;
use link::{Fs, RealFs, Target};
const MAX_ASCENT: usize = 16;
pub struct Navigator {
root: PathBuf,
index_path: Option<PathBuf>,
current: PathBuf,
entries: Vec<Entry>,
history: History,
fs: Box<dyn Fs>,
}
impl Navigator {
pub fn new(file: &Path, explicit_index: Option<&Path>, cwd: &Path) -> Navigator {
Navigator::with_fs(Box::new(RealFs), file, explicit_index, cwd)
}
pub fn with_fs(
fs: Box<dyn Fs>,
file: &Path,
explicit_index: Option<&Path>,
cwd: &Path,
) -> Navigator {
let current = absolutize(file, cwd);
let explicit = explicit_index.map(|p| absolutize(p, cwd));
let (root, index_path) = discover(fs.as_ref(), ¤t, explicit.as_deref());
let mut nav = Navigator {
root,
index_path,
current,
entries: Vec::new(),
history: History::new(),
fs,
};
nav.reload_index();
nav
}
fn reload_index(&mut self) {
let path = match &self.index_path {
Some(p) => p.clone(),
None => return,
};
let dir = parent_of(&path);
if let Ok(text) = self.fs.read(&path) {
let doc = md::parse(&text);
self.entries = index::parse(&doc, &dir, &self.root, self.fs.as_ref());
}
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn current(&self) -> &Path {
&self.current
}
pub fn index_path(&self) -> Option<&Path> {
self.index_path.as_deref()
}
pub fn entries(&self) -> &[Entry] {
&self.entries
}
pub fn position_of(&self, path: &Path) -> Option<usize> {
self.entries.iter().position(|e| same_path(&e.path, path))
}
pub fn is_index(&self, path: &Path) -> bool {
self.index_path().is_some_and(|i| same_path(i, path))
}
pub fn is_current(&self, path: &Path) -> bool {
same_path(&self.current, path)
}
pub fn depth(&self) -> usize {
self.history.depth()
}
pub fn label(&self, path: &Path) -> String {
link::rel_to(path, &self.root)
}
pub fn resolve(&self, raw: &str) -> Target {
let dir = match self.fs.is_dir(&self.current) {
true => self.current.clone(),
false => parent_of(&self.current),
};
link::resolve(raw, &dir, &self.root, self.fs.as_ref())
}
pub fn load(&self, path: &Path) -> Result<Document, String> {
self.fs.read(path).map(|t| md::parse(&t))
}
pub fn load_source(&self, path: &Path) -> Result<Box<dyn Source>, String> {
let opened = |r: std::io::Result<Box<dyn Source>>| {
r.map_err(|e| format!("{}: {e}", path.display()))
};
if self.fs.is_dir(path) {
return Ok(Box::new(DirSource::open(path)) as Box<dyn Source>);
}
match detect::decide(None, Some(path), &[]) {
Format::Markdown => self
.load(path)
.map(|doc| Box::new(MarkdownSource::new(doc)) as Box<dyn Source>),
Format::Csv => opened(CsvSource::open(path, None).map(|s| Box::new(s) as _)),
Format::Json => opened(JsonSource::open(path).map(|s| Box::new(s) as _)),
Format::Jsonl => opened(JsonlSource::open(path).map(|s| Box::new(s) as _)),
Format::Code => opened(
crate::source::code::CodeSource::open(path).map(|s| Box::new(s) as _),
),
Format::Text => opened(TextSource::open(path).map(|s| Box::new(s) as _)),
}
}
pub fn set_current(&mut self, path: PathBuf) {
self.current = path;
}
pub fn push(&mut self, current: Snapshot) {
self.history.push(current);
}
pub fn back(&mut self, current: Snapshot) -> Option<Snapshot> {
self.history.back(current)
}
pub fn forward(&mut self, current: Snapshot) -> Option<Snapshot> {
self.history.forward(current)
}
pub fn sibling(&self, delta: isize) -> Option<PathBuf> {
if self.entries.is_empty() {
return None;
}
let at = match self.position_of(&self.current) {
Some(i) => i as isize,
None if self.is_index(&self.current) => -1,
None => return None,
};
let next = at + delta;
if next < 0 {
return match at {
0 => self.index_path.clone(),
_ => None,
};
}
self.entries.get(next as usize).map(|e| e.path.clone())
}
}
pub fn absolutize(path: &Path, cwd: &Path) -> PathBuf {
PathBuf::from(ppath::absolutize(
Platform::HOST,
&cwd.to_string_lossy(),
&path.to_string_lossy(),
))
}
pub fn same_path(a: &Path, b: &Path) -> bool {
link::same_path(a, b)
}
fn parent_of(path: &Path) -> PathBuf {
if let Some(p) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
return p.to_path_buf();
}
match ppath::is_absolute(Platform::HOST, &path.to_string_lossy()) {
true => path.to_path_buf(),
false => PathBuf::from("."),
}
}
pub fn discover(fs: &dyn Fs, file: &Path, explicit: Option<&Path>) -> (PathBuf, Option<PathBuf>) {
if let Some(p) = explicit {
let index = match fs.is_dir(p) {
true => p.join("README.md"),
false => p.to_path_buf(),
};
return (parent_of(&index), Some(index));
}
let start = parent_of(file);
let mut dir = start.clone();
for _ in 0..MAX_ASCENT {
let readme = dir.join("README.md");
if !same_path(&readme, file) && fs.is_file(&readme) && links_to(fs, &readme, file, &dir) {
return (dir.clone(), Some(readme));
}
match dir.parent() {
Some(p) if p != dir => dir = p.to_path_buf(),
_ => break,
}
}
let own = start.join("README.md");
let index = match fs.is_file(&own) {
true => Some(own),
false => None,
};
(start, index)
}
fn links_to(fs: &dyn Fs, readme: &Path, file: &Path, dir: &Path) -> bool {
let text = match fs.read(readme) {
Ok(t) => t,
Err(_) => return false,
};
let doc = md::parse(&text);
index::raw_links(&doc).iter().any(|raw| {
matches!(
link::resolve(raw, dir, dir, fs),
Target::Doc { ref path, .. } if same_path(path, file)
)
})
}