Handle editing language index colocated

This commit is contained in:
Vincent Prouillet 2018-12-28 18:18:12 +01:00
parent 7313b41f4d
commit 34708d6592
4 changed files with 50 additions and 16 deletions

View file

@ -297,9 +297,15 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
let is_md = path.extension().unwrap() == "md"; let is_md = path.extension().unwrap() == "md";
let index = path.parent().unwrap().join("index.md"); let index = path.parent().unwrap().join("index.md");
let mut potential_indices = vec![path.parent().unwrap().join("index.md")];
for language in &site.config.languages {
potential_indices.push(path.parent().unwrap().join(format!("index.{}.md", language.code)));
}
let colocated_index = potential_indices.contains(&path.to_path_buf());
// A few situations can happen: // A few situations can happen:
// 1. Change on .md files // 1. Change on .md files
// a. Is there an `index.md`? Return an error if it's something other than delete // a. Is there already an `index.md`? Return an error if it's something other than delete
// b. Deleted? remove the element // b. Deleted? remove the element
// c. Edited? // c. Edited?
// 1. filename is `_index.md`, this is a section // 1. filename is `_index.md`, this is a section
@ -315,9 +321,9 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
} }
// Added another .md in a assets directory // Added another .md in a assets directory
if index.exists() && path.exists() && path != index { if index.exists() && path.exists() && !colocated_index {
bail!( bail!(
"Change on {:?} detected but there is already an `index.md` in the same folder", "Change on {:?} detected but only files named `index.md` with an optional language code are allowed",
path.display() path.display()
); );
} else if index.exists() && !path.exists() { } else if index.exists() && !path.exists() {

View file

@ -16,15 +16,15 @@ use rebuild::{after_content_change, after_content_rename};
// Loads the test_site in a tempdir and build it there // Loads the test_site in a tempdir and build it there
// Returns (site_path_in_tempdir, site) // Returns (site_path_in_tempdir, site)
macro_rules! load_and_build_site { macro_rules! load_and_build_site {
($tmp_dir: expr) => {{ ($tmp_dir: expr, $site: expr) => {{
let mut path = let mut path =
env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site"); path.push($site);
let mut options = dir::CopyOptions::new(); let mut options = dir::CopyOptions::new();
options.copy_inside = true; options.copy_inside = true;
dir::copy(&path, &$tmp_dir, &options).unwrap(); dir::copy(&path, &$tmp_dir, &options).unwrap();
let site_path = $tmp_dir.path().join("test_site"); let site_path = $tmp_dir.path().join($site);
let mut site = Site::new(&site_path, "config.toml").unwrap(); let mut site = Site::new(&site_path, "config.toml").unwrap();
site.load().unwrap(); site.load().unwrap();
let public = &site_path.join("public"); let public = &site_path.join("public");
@ -81,7 +81,7 @@ macro_rules! rename {
#[test] #[test]
fn can_rebuild_after_simple_change_to_page_content() { fn can_rebuild_after_simple_change_to_page_content() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let file_path = edit_file!( let file_path = edit_file!(
site_path, site_path,
"content/rebuild/first.md", "content/rebuild/first.md",
@ -103,7 +103,7 @@ Some content"#
#[test] #[test]
fn can_rebuild_after_title_change_page_global_func_usage() { fn can_rebuild_after_title_change_page_global_func_usage() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let file_path = edit_file!( let file_path = edit_file!(
site_path, site_path,
"content/rebuild/first.md", "content/rebuild/first.md",
@ -125,7 +125,7 @@ date = 2017-01-01
#[test] #[test]
fn can_rebuild_after_sort_change_in_section() { fn can_rebuild_after_sort_change_in_section() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let file_path = edit_file!( let file_path = edit_file!(
site_path, site_path,
"content/rebuild/_index.md", "content/rebuild/_index.md",
@ -150,7 +150,7 @@ template = "rebuild.html"
#[test] #[test]
fn can_rebuild_after_transparent_change() { fn can_rebuild_after_transparent_change() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let file_path = edit_file!( let file_path = edit_file!(
site_path, site_path,
"content/posts/2018/_index.md", "content/posts/2018/_index.md",
@ -182,7 +182,7 @@ insert_anchor_links = "left"
#[test] #[test]
fn can_rebuild_after_renaming_page() { fn can_rebuild_after_renaming_page() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let (old_path, new_path) = rename!(site_path, "content/posts/simple.md", "hard.md"); let (old_path, new_path) = rename!(site_path, "content/posts/simple.md", "hard.md");
let res = after_content_rename(&mut site, &old_path, &new_path); let res = after_content_rename(&mut site, &old_path, &new_path);
@ -195,7 +195,7 @@ fn can_rebuild_after_renaming_page() {
#[test] #[test]
fn can_rebuild_after_renaming_colocated_asset_folder() { fn can_rebuild_after_renaming_colocated_asset_folder() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let (old_path, new_path) = let (old_path, new_path) =
rename!(site_path, "content/posts/with-assets", "with-assets-updated"); rename!(site_path, "content/posts/with-assets", "with-assets-updated");
assert!(file_contains!(site_path, "content/posts/with-assets-updated/index.md", "Hello")); assert!(file_contains!(site_path, "content/posts/with-assets-updated/index.md", "Hello"));
@ -214,7 +214,7 @@ fn can_rebuild_after_renaming_colocated_asset_folder() {
#[test] #[test]
fn can_rebuild_after_renaming_section_folder() { fn can_rebuild_after_renaming_section_folder() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let (old_path, new_path) = rename!(site_path, "content/posts", "new-posts"); let (old_path, new_path) = rename!(site_path, "content/posts", "new-posts");
assert!(file_contains!(site_path, "content/new-posts/simple.md", "simple")); assert!(file_contains!(site_path, "content/new-posts/simple.md", "simple"));
@ -227,7 +227,7 @@ fn can_rebuild_after_renaming_section_folder() {
#[test] #[test]
fn can_rebuild_after_renaming_non_md_asset_in_colocated_folder() { fn can_rebuild_after_renaming_non_md_asset_in_colocated_folder() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let (old_path, new_path) = let (old_path, new_path) =
rename!(site_path, "content/posts/with-assets/zola.png", "gutenberg.png"); rename!(site_path, "content/posts/with-assets/zola.png", "gutenberg.png");
@ -239,7 +239,7 @@ fn can_rebuild_after_renaming_non_md_asset_in_colocated_folder() {
#[test] #[test]
fn can_rebuild_after_deleting_file() { fn can_rebuild_after_deleting_file() {
let tmp_dir = tempdir().expect("create temp dir"); let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir); let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
let path = site_path.join("content").join("posts").join("fixed-slug.md"); let path = site_path.join("content").join("posts").join("fixed-slug.md");
fs::remove_file(&path).unwrap(); fs::remove_file(&path).unwrap();
@ -247,3 +247,29 @@ fn can_rebuild_after_deleting_file() {
println!("{:?}", res); println!("{:?}", res);
assert!(res.is_ok()); assert!(res.is_ok());
} }
#[test]
fn can_rebuild_after_editing_in_colocated_asset_folder_with_language() {
let tmp_dir = tempdir().expect("create temp dir");
let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site_i18n");
let file_path = edit_file!(
site_path,
"content/blog/with-assets/index.fr.md",
br#"
+++
date = 2018-11-11
+++
Edite
"#
);
let res = after_content_change(&mut site, &file_path);
println!("{:?}", res);
assert!(res.is_ok());
assert!(file_contains!(
site_path,
"public/fr/blog/with-assets/index.html",
"Edite"
));
}

View file

@ -1,5 +1,5 @@
+++ +++
title = "Un slug fix" title = "Un slug fixe"
slug = "something-else" slug = "something-else"
date = 2017-01-01 date = 2017-01-01
+++ +++

View file

@ -0,0 +1,2 @@
{{page.title}}
{{page.content | safe}}