2017-05-15 10:53:39 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2018-12-27 12:14:54 +00:00
|
|
|
use config::Config;
|
|
|
|
use errors::Result;
|
|
|
|
|
2017-05-15 10:53:39 +00:00
|
|
|
/// Takes a full path to a file and returns only the components after the first `content` directory
|
|
|
|
/// Will not return the filename as last component
|
|
|
|
pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
let mut is_in_content = false;
|
|
|
|
let mut components = vec![];
|
|
|
|
|
|
|
|
for section in path.parent().unwrap().components() {
|
2018-01-24 12:21:47 +00:00
|
|
|
let component = section.as_os_str().to_string_lossy();
|
2017-05-15 10:53:39 +00:00
|
|
|
|
|
|
|
if is_in_content {
|
|
|
|
components.push(component.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if component == "content" {
|
|
|
|
is_in_content = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
components
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Struct that contains all the information about the actual file
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct FileInfo {
|
|
|
|
/// The full path to the .md file
|
|
|
|
pub path: PathBuf,
|
2018-12-28 11:15:17 +00:00
|
|
|
/// The on-disk filename, will differ from the `name` when there is a language code in it
|
|
|
|
pub filename: String,
|
2017-05-15 10:53:39 +00:00
|
|
|
/// The name of the .md file without the extension, always `_index` for sections
|
2018-12-27 12:14:54 +00:00
|
|
|
/// Doesn't contain the language if there was one in the filename
|
2017-05-15 10:53:39 +00:00
|
|
|
pub name: String,
|
|
|
|
/// The .md path, starting from the content directory, with `/` slashes
|
|
|
|
pub relative: String,
|
|
|
|
/// Path of the directory containing the .md file
|
|
|
|
pub parent: PathBuf,
|
|
|
|
/// Path of the grand parent directory for that file. Only used in sections to find subsections.
|
|
|
|
pub grand_parent: Option<PathBuf>,
|
|
|
|
/// The folder names to this section file, starting from the `content` directory
|
|
|
|
/// For example a file at content/kb/solutions/blabla.md will have 2 components:
|
|
|
|
/// `kb` and `solutions`
|
|
|
|
pub components: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileInfo {
|
|
|
|
pub fn new_page(path: &Path) -> FileInfo {
|
|
|
|
let file_path = path.to_path_buf();
|
|
|
|
let mut parent = file_path.parent().unwrap().to_path_buf();
|
|
|
|
let name = path.file_stem().unwrap().to_string_lossy().to_string();
|
|
|
|
let mut components = find_content_components(&file_path);
|
2018-03-29 12:55:25 +00:00
|
|
|
let relative = if !components.is_empty() {
|
|
|
|
format!("{}/{}.md", components.join("/"), name)
|
|
|
|
} else {
|
|
|
|
format!("{}.md", name)
|
|
|
|
};
|
2017-05-15 10:53:39 +00:00
|
|
|
|
|
|
|
// If we have a folder with an asset, don't consider it as a component
|
2018-12-27 12:14:54 +00:00
|
|
|
// Splitting on `.` as we might have a language so it isn't *only* index but also index.fr
|
|
|
|
// etc
|
|
|
|
if !components.is_empty() && name.split('.').collect::<Vec<_>>()[0] == "index" {
|
2017-05-15 10:53:39 +00:00
|
|
|
components.pop();
|
|
|
|
// also set parent_path to grandparent instead
|
|
|
|
parent = parent.parent().unwrap().to_path_buf();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo {
|
2018-12-28 11:15:17 +00:00
|
|
|
filename: file_path.file_name().unwrap().to_string_lossy().to_string(),
|
2017-05-15 10:53:39 +00:00
|
|
|
path: file_path,
|
|
|
|
// We don't care about grand parent for pages
|
|
|
|
grand_parent: None,
|
|
|
|
parent,
|
|
|
|
name,
|
|
|
|
components,
|
|
|
|
relative,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_section(path: &Path) -> FileInfo {
|
2018-12-28 11:15:17 +00:00
|
|
|
let file_path = path.to_path_buf();
|
2017-05-15 10:53:39 +00:00
|
|
|
let parent = path.parent().unwrap().to_path_buf();
|
2018-12-27 12:14:54 +00:00
|
|
|
let name = path.file_stem().unwrap().to_string_lossy().to_string();
|
2017-05-15 10:53:39 +00:00
|
|
|
let components = find_content_components(path);
|
2018-12-27 12:14:54 +00:00
|
|
|
let relative = if !components.is_empty() {
|
|
|
|
format!("{}/{}.md", components.join("/"), name)
|
2017-05-15 10:53:39 +00:00
|
|
|
} else {
|
2018-12-27 12:14:54 +00:00
|
|
|
format!("{}.md", name)
|
2017-05-15 10:53:39 +00:00
|
|
|
};
|
|
|
|
let grand_parent = parent.parent().map(|p| p.to_path_buf());
|
|
|
|
|
|
|
|
FileInfo {
|
2018-12-28 11:15:17 +00:00
|
|
|
filename: file_path.file_name().unwrap().to_string_lossy().to_string(),
|
|
|
|
path: file_path,
|
2017-05-15 10:53:39 +00:00
|
|
|
parent,
|
|
|
|
grand_parent,
|
2018-12-27 12:14:54 +00:00
|
|
|
name,
|
2017-05-15 10:53:39 +00:00
|
|
|
components,
|
|
|
|
relative,
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 12:14:54 +00:00
|
|
|
|
|
|
|
/// Look for a language in the filename.
|
|
|
|
/// If a language has been found, update the name of the file in this struct to
|
|
|
|
/// remove it and return the language code
|
|
|
|
pub fn find_language(&mut self, config: &Config) -> Result<Option<String>> {
|
|
|
|
// No languages? Nothing to do
|
|
|
|
if !config.uses_i18n() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.name.contains('.') {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go with the assumption that no one is using `.` in filenames when using i18n
|
|
|
|
// We can document that
|
2018-12-29 10:17:43 +00:00
|
|
|
let mut parts: Vec<String> = self.name.splitn(2, '.').map(|s| s.to_string()).collect();
|
2018-12-27 12:14:54 +00:00
|
|
|
|
|
|
|
// The language code is not present in the config: typo or the user forgot to add it to the
|
|
|
|
// config
|
|
|
|
if !config.languages_codes().contains(&parts[1].as_ref()) {
|
|
|
|
bail!("File {:?} has a language code of {} which isn't present in the config.toml `languages`", self.path, parts[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.name = parts.swap_remove(0);
|
|
|
|
let lang = parts.swap_remove(0);
|
|
|
|
|
|
|
|
Ok(Some(lang))
|
|
|
|
}
|
2017-05-15 10:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl Default for FileInfo {
|
|
|
|
fn default() -> FileInfo {
|
|
|
|
FileInfo {
|
|
|
|
path: PathBuf::new(),
|
|
|
|
parent: PathBuf::new(),
|
|
|
|
grand_parent: None,
|
2018-12-28 11:15:17 +00:00
|
|
|
filename: String::new(),
|
2017-05-15 10:53:39 +00:00
|
|
|
name: String::new(),
|
|
|
|
components: vec![],
|
|
|
|
relative: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-12-27 12:14:54 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use config::{Config, Language};
|
|
|
|
|
2018-12-29 10:17:43 +00:00
|
|
|
use super::{find_content_components, FileInfo};
|
2017-05-15 10:53:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_content_components() {
|
2018-10-31 07:18:57 +00:00
|
|
|
let res =
|
|
|
|
find_content_components("/home/vincent/code/site/content/posts/tutorials/python.md");
|
2017-05-15 10:53:39 +00:00
|
|
|
assert_eq!(res, ["posts".to_string(), "tutorials".to_string()]);
|
|
|
|
}
|
2018-12-27 12:14:54 +00:00
|
|
|
#[test]
|
|
|
|
fn can_find_components_in_page_with_assets() {
|
2018-12-29 10:17:43 +00:00
|
|
|
let file = FileInfo::new_page(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/python/index.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
assert_eq!(file.components, ["posts".to_string(), "tutorials".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_valid_language_in_page() {
|
|
|
|
let mut config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false });
|
|
|
|
let mut file = FileInfo::new_page(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/python.fr.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
let res = file.find_language(&config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Some(String::from("fr")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_valid_language_in_page_with_assets() {
|
|
|
|
let mut config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false });
|
|
|
|
let mut file = FileInfo::new_page(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/python/index.fr.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
assert_eq!(file.components, ["posts".to_string(), "tutorials".to_string()]);
|
|
|
|
let res = file.find_language(&config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Some(String::from("fr")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn do_nothing_on_unknown_language_in_page_with_i18n_off() {
|
|
|
|
let config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
let mut file = FileInfo::new_page(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/python.fr.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
let res = file.find_language(&config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert!(res.unwrap().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_on_unknown_language_in_page_with_i18n_on() {
|
|
|
|
let mut config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
config.languages.push(Language { code: String::from("it"), rss: false });
|
|
|
|
let mut file = FileInfo::new_page(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/python.fr.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
let res = file.find_language(&config);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_valid_language_in_section() {
|
|
|
|
let mut config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false });
|
|
|
|
let mut file = FileInfo::new_section(&Path::new(
|
|
|
|
"/home/vincent/code/site/content/posts/tutorials/_index.fr.md",
|
|
|
|
));
|
2018-12-27 12:14:54 +00:00
|
|
|
let res = file.find_language(&config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Some(String::from("fr")));
|
|
|
|
}
|
2017-05-15 10:53:39 +00:00
|
|
|
}
|