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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
use std::{fmt::Display, fs, path::Path};
use anyhow::{Context, Result};
use crate::{
config_file::Config,
file_tree::{FileTree, GeneratedFile},
};
use super::{replacement::untemplatize_chapter, MangledName};
pub struct ChapterName {
name: MangledName,
number: u32,
}
impl Display for ChapterName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:02}_{}", self.number, self.name))
}
}
impl ChapterName {
pub fn from_str(name: &str, last_chapter_number: u32) -> Self {
let name = MangledName::new(name);
Self {
name,
number: last_chapter_number + 1,
}
}
pub fn new(name: MangledName, last_chapter_number: u32) -> Self {
Self {
name,
number: last_chapter_number + 1,
}
}
pub fn to_components(self) -> (MangledName, u32) {
(self.name, self.number)
}
fn from_components(name: MangledName, number: u32) -> Self {
Self { name, number }
}
}
pub fn generate_new_chapter(
config: Config,
project_root: &Path,
name: String,
) -> anyhow::Result<FileTree> {
let mut file_tree = FileTree::new();
let (last_chapter_name, last_chapter_number) = get_last_chapter_name(project_root)
.context("Failed to get information on last chapter")?
.unwrap_or(ChapterName {
name: MangledName::from_str_unsafe("static"),
number: 0,
})
.to_components();
file_tree.add_file(new_main_file(
project_root,
&config,
&name,
last_chapter_number,
last_chapter_name,
)?);
file_tree.add_file(new_chapter_file(
&config,
&name,
project_root,
last_chapter_number,
));
Ok(file_tree)
}
fn get_last_chapter_name(project_root: &Path) -> anyhow::Result<Option<ChapterName>> {
let chapter_dirs = project_root.join("content");
let mut chapter_names = fs::read_dir(chapter_dirs)?
.filter_map(|path| -> Option<Result<String>> {
let path = match path.context("Failed to read a path") {
Ok(ok) => ok,
Err(err) => return Some(Err(err)),
};
let os_file_name = path.file_name();
let file_name = os_file_name
.to_str()
.expect("All chapter should be converted to ascii");
if file_name == "static" {
None
} else {
Some(Ok(file_name.to_owned()))
}
})
.collect::<Result<Vec<String>>>()?;
// There are no chapters, besides the default `static` one, which was sorted out
if chapter_names.is_empty() {
return Ok(None);
}
// The names are prefixed with a number
chapter_names.sort();
let raw_components = chapter_names[chapter_names.len() - 1]
.split_once('_')
.expect("Exits");
let number: u32 = raw_components.0.parse().expect("Will be a number");
// The name is already mangled
assert!(MangledName::check_mangled(raw_components.1));
let name: MangledName = MangledName::from_str_unsafe(raw_components.1);
Ok(Some(ChapterName::from_components(name, number)))
}
fn new_chapter_file(
config: &Config,
name: &str,
project_root: &Path,
last_chapter_number: u32,
) -> GeneratedFile {
let chapter_text = untemplatize_chapter(&config.templates.chapter, &name);
GeneratedFile::new(
project_root
.join("content")
.join(ChapterName::from_str(name, last_chapter_number).to_string())
.join("chapter.tex"),
chapter_text,
)
}
fn new_main_file(
project_root: &Path,
config: &Config,
name: &str,
last_chapter_number: u32,
last_chapter_name: MangledName,
) -> anyhow::Result<GeneratedFile> {
let main_path = project_root.join(&config.main_file);
let mut main_text = fs::read_to_string(&main_path)?;
let chapter_includeonly: String = format!(
"\\includeonly{{content/{}/{}}}",
ChapterName::from_str(name, last_chapter_number).to_string(),
"chapter.tex",
);
if &last_chapter_name.as_str() == &"static" && last_chapter_number == 0 {
// This is the first added chapter; The `\includeonly` will be empty.
main_text = main_text.replace("\\includeonly{}", &chapter_includeonly)
} else {
main_text = main_text.replace(
&format!(
"\\includeonly{{content/{}/{}}}",
ChapterName::new(last_chapter_name, last_chapter_number - 1).to_string(),
"chapter.tex",
),
&chapter_includeonly,
)
};
let find_index = main_text
.find("% NEXT_CHAPTER")
.expect("The % NEXT_CHAPTER maker must exist");
main_text.insert_str(
find_index,
&format!(
"\\include{{content/{}/{}}}\n ",
ChapterName::from_str(name, last_chapter_number).to_string(),
"chapter.tex",
),
);
Ok(GeneratedFile::new(main_path, main_text))
}
|