Skip to content

Commit

Permalink
feat: add full text search document.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Jul 15, 2024
1 parent b9a93b0 commit 5001a9d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
44 changes: 36 additions & 8 deletions src/book/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use tera::{Context, Tera};

use crate::{
book::search::Document,
cli,
html::{self, Hypertext, Markdown, Template},
utils::{self, Logger},
Expand All @@ -14,6 +15,7 @@ pub struct Builder {
pub engine: Tera,
pub root: Root,
pub settings: Settings,
pub search_data: Vec<Document>,
}

pub fn new_builder() -> Result<Builder, Box<dyn std::error::Error>> {
Expand All @@ -33,11 +35,12 @@ pub fn new_builder() -> Result<Builder, Box<dyn std::error::Error>> {
engine,
root,
settings,
search_data: Vec::new(),
})
}

impl Builder {
pub fn generate_books(&self) -> io::Result<()> {
pub fn generate_books(&mut self) -> io::Result<()> {
cli::ouput_banner();
self.create_workspace();
self.copy_theme_assets();
Expand Down Expand Up @@ -87,7 +90,7 @@ impl Builder {
));
}

fn render_index_html(&self) {
fn render_index_html(&mut self) {
let mut log = Logger::console_log(); // initialize console logger.
let template = Template::new(&self.settings, &self.root.get_chapters());

Expand All @@ -99,7 +102,7 @@ impl Builder {
Ok(markdown_content) => {
// create Hypertext instance
let hypertext = Hypertext::new(
"index.html",
&template.name,
&index_file.display().to_string(),
Markdown::new(markdown_content),
);
Expand All @@ -124,6 +127,14 @@ impl Builder {

let index_chapter_file = out_base_path.join("index.html");

// push full text search data
let _ = &mut self.search_data.push(Document::new(
"".to_string(),
format!("{}.html", &index_chapter_file.display().to_string()),
template.name,
hypertext.to_html(),
));

// write to file /chapter2/chapter_1.1.1.html
match fs::write(index_chapter_file.display().to_string(), rendered) {
Ok(_) => log.info(format_args!(
Expand All @@ -137,7 +148,7 @@ impl Builder {
}
}

fn render_sub_chapter_html(&self) {
fn render_sub_chapter_html(&mut self) {
let mut log = Logger::console_log();
let template = Template::new(&self.settings, &self.root.get_chapters());

Expand Down Expand Up @@ -175,6 +186,14 @@ impl Builder {
.to_lowercase(),
);

// push full text search data
let _ = &mut self.search_data.push(Document::new(
"".to_string(),
format!("{}.html", &sub_chapter_file.display().to_string()),
html_content.title.to_string(),
html_content.to_html(),
));

// write to file /chapter2/chapter_1.1.1.html
match fs::write(
format!("{}.html", &sub_chapter_file.display().to_string()),
Expand All @@ -190,7 +209,7 @@ impl Builder {
}
}

fn render_chapter_html(&self) {
fn render_chapter_html(&mut self) {
let mut log = Logger::console_log();
let template = Template::new(&self.settings, &self.root.get_chapters());
let root = self.settings.get_output_path();
Expand Down Expand Up @@ -221,6 +240,14 @@ impl Builder {
context.insert("description", &template.description);
context.insert("content", &html_content.to_html());

// push full text search data
let _ = &mut self.search_data.push(Document::new(
"".to_string(),
format!("{}.html", &chapter_file.display().to_string()),
html_content.title.to_string(),
html_content.to_html(),
));

// render template
let rendered = self.engine.render("index.html", &context).unwrap();

Expand Down Expand Up @@ -277,12 +304,12 @@ impl Builder {

// HTML content for sub-chapters and return as HashMap<String, Vec<html::Hypertext>>
fn sub_chapters_html(&self) -> HashMap<String, Vec<html::Hypertext>> {
let mut result: HashMap<String, Vec<html::Hypertext>> = HashMap::new(); // Initialize result HashMap.
let mut log = Logger::console_log(); // Initialize console logger.
let mut result: HashMap<String, Vec<html::Hypertext>> = HashMap::new();
let mut log = Logger::console_log();

// iterate over chapters
for chapter in self.root.get_chapters() {
let mut chapter_hypertexts: Vec<html::Hypertext> = Vec::new(); // Initialize Vec for sub-chapters
let mut chapter_hypertexts: Vec<html::Hypertext> = Vec::new();

for sub_chapter in &chapter.sub_chapters {
let sub_chapter_path =
Expand All @@ -297,6 +324,7 @@ impl Builder {
&sub_chapter.path,
Markdown::new(&markdown_content),
);

chapter_hypertexts.push(hypertext);

log.info(format_args!(
Expand Down
2 changes: 2 additions & 0 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod builder;
pub mod root;
pub mod search;
pub mod settings;

pub use builder::*;
pub use root::*;
pub use search::*;
pub use settings::*;
21 changes: 21 additions & 0 deletions src/book/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Document {
pub id: String,
pub url: String,
pub title: String,
pub content: String,
}

impl Document {
// 构造函数,使用所有字段来创建一个新的 Document 实例
pub fn new(id: String, url: String, title: String, content: String) -> Self {
Self {
id,
url,
title,
content,
}
}
}
2 changes: 1 addition & 1 deletion src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static HELP_INFO: Lazy<Mutex<HashMap<Command, colored::ColoredString>>> = Lazy::
pub fn handle_build_command(_args: &[String]) {
let mut log = Logger::console_log();
match book::new_builder() {
Ok(builder) => match builder.generate_books() {
Ok(mut builder) => match builder.generate_books() {
Ok(_) => log.info(format_args!(
"Rendering of static resource files complete 🎉"
)),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use typikon::cli::Command;
fn main() {
// deconstructing command line input to parameter information
let (command, args) = typikon::cli::parse_args();
// Match processing of the corresponding command
// match processing of the corresponding command
match command {
Command::Init => typikon::cli::handle_init_command(&args),
Command::Help => typikon::cli::handle_help_command(&args),
Expand Down

0 comments on commit 5001a9d

Please sign in to comment.