1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
use std::{
fs::{self, File},
io::{self, Write},
};
use convert_case::{Case, Casing};
use crate::data::Data;
use super::{get_project_root, CHAPTER};
pub fn generate_new_chapter(name: String) -> io::Result<()> {
let project_root = get_project_root().unwrap();
let raw_data_file = fs::read_to_string(project_root.join("lpm.toml")).unwrap();
let mut data_file: Data = toml::from_str(&raw_data_file).unwrap();
let mut main_file = fs::read_to_string(project_root.join("main.tex")).unwrap();
fs::create_dir(project_root.join(format!("content/{}", name.to_case(Case::Snake),))).unwrap();
let mut new_chapter = File::create(project_root.join(format!(
"content/{}/chapter_{:02}.tex",
name.to_case(Case::Snake),
data_file.last_chapter.number + 1
)))
.unwrap();
new_chapter
.write_all(CHAPTER.replace("REPLACEMENT_CHAPTER", &name).as_bytes())
.unwrap();
fs::create_dir(project_root.join(format!("content/{}/sections", name.to_case(Case::Snake),)))
.unwrap();
main_file = main_file.replace(
&format!(
"\\includeonly{{content/{}/{}}}",
&data_file.last_chapter.user_name,
&format!("chapter_{:02}", data_file.last_chapter.number)
),
&format!(
"\\includeonly{{content/{}/{}}}",
name.to_case(Case::Snake),
&format!("chapter_{:02}", data_file.last_chapter.number + 1)
),
);
let find_index = main_file.find("% NEXT_CHAPTER").unwrap();
main_file.insert_str(
find_index,
&format!(
"\\include{{content/{}/{}}}\n ",
name.to_case(Case::Snake),
&format!("chapter_{:02}", data_file.last_chapter.number + 1)
),
);
data_file.last_chapter.user_name = name.to_case(Case::Snake);
data_file.last_chapter.number += 1;
fs::write(
project_root.join("lpm.toml"),
toml::to_string(&data_file).expect("We changed it ourselfes, the conversion should work"),
)
.unwrap();
fs::write(project_root.join("main.tex"), main_file).unwrap();
Ok(())
}
|