Skip to content

Commit

Permalink
add: book builder chapters output.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Jul 2, 2024
1 parent 4cc7106 commit 2d32a64
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
Empty file added book/chapter1.md
Empty file.
Empty file added book/chapter2.md
Empty file.
12 changes: 6 additions & 6 deletions root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ root:
index: "index.md"
chapters:
- title: "Chapter 1"
index: "/chapter1/chapter1.md"
index: "chapter1.md"
sub_chapters:
- title: "Section 1.1"
path: "book/chapter_1.1.0.md"
path: "chapter_1.1.0.md"
- title: "Section 1.2"
path: "book/chapter_1.1.1.md"
path: "chapter_1.1.1.md"
- title: "Chapter 2"
index: "/chapter2/chapter2.md"
index: "chapter2.md"
sub_chapters:
- title: "Section 2.1"
path: "book/chapter_1.1.0.md"
path: "chapter_1.1.0.md"
- title: "Section 2.2"
path: "book/chapter_1.1.1.md"
path: "chapter_1.1.1.md"
74 changes: 57 additions & 17 deletions src/book/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tera::Tera;

use crate::{
book,
html::{self, template::Template, Hypertext, Markdown},
html::{self, Hypertext, Markdown},
utils::Logger,
};

Expand All @@ -15,19 +15,54 @@ pub struct Builder {
root: Root,
pub engine: Tera,
settings: Settings,
pub templates: Vec<Template>,
}

impl Builder {
// 渲染整本书
pub fn render(&mut self) -> io::Result<()> {
self.create_directory()?;
self.get_chapters_hypertext();
self.sub_chapters_html();
self.copy_theme_assets()?;
Ok(())
}

pub fn get_chapters_hypertext(&mut self) -> HashMap<String, Vec<html::Hypertext>> {
pub fn chapters_html(&mut self) -> HashMap<String, html::Hypertext> {
let mut result: HashMap<String, html::Hypertext> = HashMap::new();
let mut log = Logger::console_log();

if let Some(chapters) = self.get_chapter() {
// 开始遍历所有文章,找出同一子篇章的
for chapter in chapters {
let chapter_index_path = Path::new(&chapter.index);

let chapter_path = format!(
"{}/{}",
self.settings.settings.directory.source,
&chapter_index_path.display().to_string()
);

// 开始读取 markdown 文件的内容
match fs::read_to_string(&chapter_path) {
Ok(markdown_content) => {
let hypertext =
Hypertext::new(&chapter_path, Markdown::new(&markdown_content));

result.insert(chapter.title.replace(" ", "_").to_lowercase(), hypertext);

log.info(format_args!(
"Loading markdown file {:?} successful",
chapter_path
));
}
Err(err) => log.error(format_args!("Loading markdown file fail : {:?}", err)),
}
}
}

result
}

pub fn sub_chapters_html(&mut self) -> HashMap<String, Vec<html::Hypertext>> {
let mut result: HashMap<String, Vec<html::Hypertext>> = HashMap::new();
let mut log = Logger::console_log();

Expand All @@ -39,13 +74,18 @@ impl Builder {

// 遍历子文章目录,并且将所有的子文章放到同父类中
for sub_chapter in &chapter.sub_chapters {
let sub_chapter_path = &sub_chapter.path;
let sub_chapter_path = format!(
"{}/{}",
self.settings.settings.directory.source, &sub_chapter.path
);

// 开始读取 markdown 文件的内容
match fs::read_to_string(sub_chapter_path) {
match fs::read_to_string(&sub_chapter_path) {
Ok(markdown_content) => {
let hypertext =
Hypertext::new(sub_chapter_path, Markdown::new(&markdown_content));
let hypertext = Hypertext::new(
&sub_chapter_path.as_str(),
Markdown::new(&markdown_content),
);
chapter_hypertexts.push(hypertext);

log.info(format_args!(
Expand All @@ -59,13 +99,15 @@ impl Builder {
}
}

// 转换小写名字
let chapter_key = base_chapter
.file_name()
.and_then(|os_str| os_str.to_str())
.map_or_else(|| chapter.title.clone(), |s| s.to_string());

result.insert(chapter_key.to_lowercase(), chapter_hypertexts);
// 转换小写名字和下划线
result.insert(
base_chapter
.display()
.to_string()
.replace(" ", "_")
.to_lowercase(),
chapter_hypertexts,
);
}
}

Expand Down Expand Up @@ -126,7 +168,6 @@ impl Builder {

pub fn new_builder() -> Result<Builder, Box<dyn std::error::Error>> {
let root = book::get_root()?;
let templates = html::get_templates().unwrap();
let settings = book::get_settings()?;
let engine = Tera::new(
format!(
Expand All @@ -138,7 +179,6 @@ pub fn new_builder() -> Result<Builder, Box<dyn std::error::Error>> {
// 返回 Builder 实例
Ok(Builder {
root,
templates,
engine,
settings,
})
Expand Down
16 changes: 2 additions & 14 deletions src/html/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use super::{Hypertext, Markdown};
pub struct Template {
// 模版名称用于区分多个文件的模版
pub name: String,
// 模版路径,模版存储多个文件层次结构中
pub path: String,
// 模版文件的内容
pub content: String,
// 要插入渲染好的 HTML 数据,这个是通过 MD 转换好的 HTML 数据段
Expand All @@ -16,23 +14,13 @@ pub struct Template {
}

impl Template {
pub fn new(_tmpl: String) -> Template {
pub fn new(name: &str, hypertext: Hypertext) -> Template {
unimplemented!()
}

pub fn empty() -> Template {
Template {
name: "index.html".to_string(),
path: "index.html".to_string(),
content: "".to_string(),
hypertext: Hypertext::new("index.html", Markdown::new("")),
data_ctx: tera::Context::new(),
}
}
}

// 渲染模版引擎函数
pub fn render_to_html(_tmpl: Vec<Template>) {
pub fn render_to_html(temp: Vec<Template>) {
unimplemented!()
}

Expand Down
13 changes: 12 additions & 1 deletion tests/book/builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ fn test_new_builder() {
fn test_new_builder_get_hypertext() {
match book::new_builder() {
Ok(mut builder) => {
println!("{:?}", &mut builder.get_chapters_hypertext());
println!("{:?}", &mut builder.sub_chapters_html());
assert!(true);
}
Err(_) => assert!(false),
}
}

#[test]
fn test_new_builder_get_chapters() {
match book::new_builder() {
Ok(mut builder) => {
println!("{:?}", &mut builder.chapters_html());
assert!(true);
}
Err(_) => assert!(false),
Expand Down

0 comments on commit 2d32a64

Please sign in to comment.