Browse Source

Move find_content_components to content mod

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
4f27dc2f18
4 changed files with 35 additions and 39 deletions
  1. +2
    -2
      src/content/page.rs
  2. +2
    -1
      src/content/section.rs
  3. +30
    -1
      src/content/utils.rs
  4. +1
    -35
      src/utils.rs

+ 2
- 2
src/content/page.rs View File

@@ -12,8 +12,8 @@ use errors::{Result, ResultExt};
use config::Config; use config::Config;
use front_matter::{PageFrontMatter, split_page_content}; use front_matter::{PageFrontMatter, split_page_content};
use markdown::markdown_to_html; 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};








+ 2
- 1
src/content/section.rs View File

@@ -8,9 +8,10 @@ use serde::ser::{SerializeStruct, self};
use config::Config; use config::Config;
use front_matter::{SectionFrontMatter, split_section_content}; use front_matter::{SectionFrontMatter, split_section_content};
use errors::{Result, ResultExt}; use errors::{Result, ResultExt};
use utils::{read_file, find_content_components};
use utils::{read_file};
use markdown::markdown_to_html; use markdown::markdown_to_html;
use content::Page; use content::Page;
use content::utils::find_content_components;




#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]


+ 30
- 1
src/content/utils.rs View File

@@ -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)] #[cfg(test)]
mod tests { mod tests {
use std::fs::File; use std::fs::File;


use tempdir::TempDir; use tempdir::TempDir;


use super::{find_related_assets, get_reading_analytics};
use super::{find_related_assets, find_content_components, get_reading_analytics};


#[test] #[test]
fn can_find_related_assets() { fn can_find_related_assets() {
@@ -74,4 +97,10 @@ mod tests {
assert_eq!(word_count, 2000); assert_eq!(word_count, 2000);
assert_eq!(reading_time, 10); 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()]);
}
} }

+ 1
- 35
src/utils.rs View File

@@ -4,6 +4,7 @@ use std::path::Path;


use errors::{Result, ResultExt}; use errors::{Result, ResultExt};


/// Create a file with the content given
pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> { pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
let mut file = File::create(&path)?; let mut file = File::create(&path)?;
file.write_all(content.as_bytes())?; file.write_all(content.as_bytes())?;
@@ -32,38 +33,3 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {


Ok(content) 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…
Cancel
Save