Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle home directory expansion #205

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 87 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ doc = false
[dependencies]
eyre = "0.6.9"
gumdrop = "0.8.1"
home = "0.5.9"
ignore = "0.4.21"
lazy_static = "1.4.0"
matter = "0.1.0-alpha4"
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rayon::prelude::*;
use references::*;
use slug::slugify;
use snafu::{ResultExt, Snafu};

use std::ffi::OsString;
use std::fmt;
use std::fs::{self, File};
Expand Down Expand Up @@ -266,6 +267,8 @@ impl<'a> Exporter<'a> {
/// Create a new exporter which reads notes from `root` and exports these to
/// `destination`.
pub fn new(root: PathBuf, destination: PathBuf) -> Exporter<'a> {
let root = expand_home_dir(root);
let destination = expand_home_dir(destination);
Exporter {
start_at: root.clone(),
root,
Expand Down Expand Up @@ -889,6 +892,21 @@ fn codeblock_kind_to_owned<'a>(codeblock_kind: CodeBlockKind) -> CodeBlockKind<'
}
}

/// Handles ~
fn expand_home_dir<P: Into<PathBuf>>(path: P) -> PathBuf {
let path = path.into();

if !path.starts_with("~") {
return path;
}

lazy_static! {
static ref HOME_DIR: PathBuf = home::home_dir().expect("Unable to find home directory");
}

HOME_DIR.join(path.strip_prefix("~").unwrap())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading