2016-12-16 00:22:56 +01:00
|
|
|
// Copyright (C) 2016 Élisabeth HENRY.
|
|
|
|
//
|
|
|
|
// This file is part of Crowbook.
|
|
|
|
//
|
|
|
|
// Crowbook is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 2.1 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Crowbook is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Misc utility functions used across crowbook
|
|
|
|
|
2016-12-30 15:31:22 +01:00
|
|
|
use token::Token;
|
|
|
|
|
2016-12-16 00:22:56 +01:00
|
|
|
use std;
|
2016-12-16 01:32:49 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-12-16 01:06:49 +01:00
|
|
|
use std::io::Result;
|
2016-12-16 00:22:56 +01:00
|
|
|
|
|
|
|
/// Try to canonicalize a path using std::fs::canonicalize, and returns the
|
|
|
|
/// unmodified path if it fails (e.g. if the path doesn't exist (yet))
|
2016-12-16 01:34:37 +01:00
|
|
|
pub fn normalize<P: AsRef<Path>>(path: P) -> String {
|
|
|
|
try_normalize(path.as_ref())
|
2017-01-09 17:45:59 +01:00
|
|
|
.unwrap_or_else(|_| format!("{}", path.as_ref().display()))
|
2016-12-16 01:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-16 01:34:37 +01:00
|
|
|
fn try_normalize<P: AsRef<Path>>(path: P) -> Result<String> {
|
2016-12-16 01:32:49 +01:00
|
|
|
let full_path = std::fs::canonicalize(path.as_ref())?;
|
|
|
|
let mut cwd = std::env::current_dir()?;
|
|
|
|
let mut ups = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Ok(path) = full_path.strip_prefix(&cwd.clone()) {
|
|
|
|
let mut new_path = PathBuf::new();
|
|
|
|
for _ in 0..ups {
|
|
|
|
new_path.push("../");
|
|
|
|
}
|
|
|
|
new_path.push(path);
|
|
|
|
return Ok(format!("{}", new_path.display()));
|
|
|
|
} else {
|
|
|
|
if !cwd.pop() {
|
|
|
|
return Ok(format!("{}", full_path.display()));
|
|
|
|
} else {
|
|
|
|
ups += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 00:22:56 +01:00
|
|
|
}
|
2016-12-30 15:31:22 +01:00
|
|
|
|
|
|
|
/// Insert a title (if there is none) to a vec of tokens
|
|
|
|
pub fn insert_title(tokens: &mut Vec<Token>) {
|
|
|
|
for token in tokens.iter() {
|
2017-01-09 17:45:59 +01:00
|
|
|
if let &Token::Header(1, _) = token {
|
|
|
|
return;
|
2016-12-30 15:31:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tokens.insert(0, Token::Header(1, vec!()));
|
|
|
|
}
|