Move find_content_components to content mod
This commit is contained in:
parent
54fc1055ad
commit
4f27dc2f18
|
@ -12,8 +12,8 @@ use errors::{Result, ResultExt};
|
|||
use config::Config;
|
||||
use front_matter::{PageFrontMatter, split_page_content};
|
||||
use markdown::markdown_to_html;
|
||||
use utils::{read_file, find_content_components};
|
||||
use content::utils::{find_related_assets, get_reading_analytics};
|
||||
use utils::{read_file};
|
||||
use content::utils::{find_related_assets, find_content_components, get_reading_analytics};
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -8,9 +8,10 @@ use serde::ser::{SerializeStruct, self};
|
|||
use config::Config;
|
||||
use front_matter::{SectionFrontMatter, split_section_content};
|
||||
use errors::{Result, ResultExt};
|
||||
use utils::{read_file, find_content_components};
|
||||
use utils::{read_file};
|
||||
use markdown::markdown_to_html;
|
||||
use content::Page;
|
||||
use content::utils::find_content_components;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
|
|
@ -33,13 +33,36 @@ pub fn get_reading_analytics(content: &str) -> (usize, usize) {
|
|||
}
|
||||
|
||||
|
||||
/// Takes a full path to a .md 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() {
|
||||
let component = section.as_ref().to_string_lossy();
|
||||
|
||||
if is_in_content {
|
||||
components.push(component.to_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
if component == "content" {
|
||||
is_in_content = true;
|
||||
}
|
||||
}
|
||||
|
||||
components
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs::File;
|
||||
|
||||
use tempdir::TempDir;
|
||||
|
||||
use super::{find_related_assets, get_reading_analytics};
|
||||
use super::{find_related_assets, find_content_components, get_reading_analytics};
|
||||
|
||||
#[test]
|
||||
fn can_find_related_assets() {
|
||||
|
@ -74,4 +97,10 @@ mod tests {
|
|||
assert_eq!(word_count, 2000);
|
||||
assert_eq!(reading_time, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_find_content_components() {
|
||||
let res = find_content_components("/home/vincent/code/site/content/posts/tutorials/python.md");
|
||||
assert_eq!(res, ["posts".to_string(), "tutorials".to_string()]);
|
||||
}
|
||||
}
|
||||
|
|
36
src/utils.rs
36
src/utils.rs
|
@ -4,6 +4,7 @@ use std::path::Path;
|
|||
|
||||
use errors::{Result, ResultExt};
|
||||
|
||||
/// Create a file with the content given
|
||||
pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
|
||||
let mut file = File::create(&path)?;
|
||||
file.write_all(content.as_bytes())?;
|
||||
|
@ -32,38 +33,3 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
|
|||
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
|
||||
/// Takes a full path to a .md 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() {
|
||||
let component = section.as_ref().to_string_lossy();
|
||||
|
||||
if is_in_content {
|
||||
components.push(component.to_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
if component == "content" {
|
||||
is_in_content = true;
|
||||
}
|
||||
}
|
||||
|
||||
components
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{find_content_components};
|
||||
|
||||
#[test]
|
||||
fn test_find_content_components() {
|
||||
let res = find_content_components("/home/vincent/code/site/content/posts/tutorials/python.md");
|
||||
assert_eq!(res, ["posts".to_string(), "tutorials".to_string()]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue