Skip to content

Commit 8de3fa3

Browse files
committed
refactor: renamed HtmlGenerate to Generate
Signed-off-by: skoowu <skoowu.fancy@gmail.com>
1 parent e857d57 commit 8de3fa3

File tree

11 files changed

+44
-147
lines changed

11 files changed

+44
-147
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
# Welcome to Medup !
2+
# Welcome to Medup!
33

44
* [markdown-guide](./markdown-guide.md)

examples/simple.md

Lines changed: 0 additions & 103 deletions
This file was deleted.

src/html_generate.rs renamed to src/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::SharedLine;
22

3-
pub trait HtmlGenerate {
3+
pub trait Generate {
44
fn head(&self) -> String;
55
fn body_begin(&self) -> String;
66
fn body_end(&self) -> String;

src/html.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::error::Error;
33

44
use super::SharedLine;
55
use crate::config::Config;
6-
use crate::html_generate::HtmlGenerate;
6+
use crate::generate::Generate;
77
use crate::lexer::{Token, TokenKind};
8-
use crate::stack;
8+
use crate::utils::stack;
99

1010
use serde::Serialize;
1111
use tinytemplate::TinyTemplate;
@@ -170,7 +170,7 @@ impl<'generator> Generator<'generator> {
170170
}
171171
}
172172

173-
impl<'generator> HtmlGenerate for Generator<'generator> {
173+
impl<'generator> Generate for Generator<'generator> {
174174
fn head(&self) -> String {
175175
if !self.cfg.custom_html_head.is_empty() {
176176
return self.cfg.custom_html_head.clone();

src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
collections::{HashMap, VecDeque},
44
};
55

6-
use crate::{cursor, stack};
6+
use crate::utils::{cursor, stack};
77

88
use itertools::Itertools;
99
use v_htmlescape as htmlescape;

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use regex::Regex;
77
use url::Url;
88

99
pub mod config;
10-
mod cursor;
10+
pub mod generate;
1111
mod html;
12-
pub mod html_generate;
1312
mod lexer;
1413
pub mod markdown;
1514
mod parser;
16-
mod stack;
15+
mod utils;
1716

1817
pub type SharedLine = Rc<RefCell<parser::Line>>;
1918

src/markdown.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,12 @@ impl<'markdown> Markdown<'markdown> {
6868
}
6969
}
7070

71-
// Render markdown ast into html
71+
// Convert markdown ast into html
7272
pub fn to_html(ast: &Ast, cfg: &Config) -> Result<String, Box<dyn Error>> {
73-
let gen = html::Generator::new(cfg, ast.ref_link_tags())?;
74-
Ok(ast.render_html(&gen))
73+
Ok(ast.generate(&html::Generator::new(cfg, ast.ref_link_tags())?))
7574
}
7675

77-
// Render markdown ast into raw html body
78-
pub fn to_raw(ast: &Ast, cfg: &Config) -> Result<String, Box<dyn Error>> {
79-
let gen = html::Generator::new(cfg, ast.ref_link_tags())?;
80-
Ok(ast.render_html_body(&gen))
76+
// Convert markdown ast into body part of the html
77+
pub fn to_html_body(ast: &Ast, cfg: &Config) -> Result<String, Box<dyn Error>> {
78+
Ok(ast.generate_body(&html::Generator::new(cfg, ast.ref_link_tags())?))
8179
}

src/parser.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::rc::Rc;
77
use std::{fmt, io};
88

99
use super::SharedLine;
10-
use crate::html_generate::HtmlGenerate;
10+
use crate::generate::Generate;
1111
use crate::lexer::{Lexer, Token, TokenKind};
1212

1313
use itertools::Itertools;
@@ -153,42 +153,43 @@ impl Ast {
153153
}
154154
}
155155

156-
// Iterate through each block of the Ast and process the block into a 'html' string
157-
pub(crate) fn render_html(&self, html: &impl HtmlGenerate) -> String {
156+
// TODO: optimize
157+
pub(crate) fn generate(&self, gen: &impl Generate) -> String {
158158
let mut buff: Vec<String> = vec![];
159-
let body = self.render_html_body(html);
159+
let body = self.generate_body(gen);
160160

161161
buff.push(String::from("<!doctype html><html>"));
162-
buff.push(html.head());
163-
buff.push(html.body_begin());
162+
buff.push(gen.head());
163+
buff.push(gen.body_begin());
164164
buff.push(body);
165-
buff.push(html.body_end());
165+
buff.push(gen.body_end());
166166
buff.push(String::from("</html>"));
167167

168168
buff.join("\n")
169169
}
170170

171-
pub(crate) fn render_html_body(&self, html: &impl HtmlGenerate) -> String {
171+
// Iterate through each block of the Ast and process the block into a 'html' string
172+
pub(crate) fn generate_body(&self, gen: &impl Generate) -> String {
172173
self.blocks()
173174
.iter()
174175
.filter(|b| b.kind() != Kind::Meta__ && b.kind() != Kind::ListNesting__)
175176
.map(|b| match b.kind() {
176-
Kind::Title => html.body_title(b.first()),
177-
Kind::PlainText => html.body_plain_text(b.contains()),
178-
Kind::Dividing => html.body_dividing(b.first()),
179-
Kind::CodeBlock => html.body_code(b.contains()),
180-
Kind::UnorderedList => html.body_unordered_list(b.contains()),
181-
Kind::Blank => html.body_blank(b.contains()),
177+
Kind::Title => gen.body_title(b.first()),
178+
Kind::PlainText => gen.body_plain_text(b.contains()),
179+
Kind::Dividing => gen.body_dividing(b.first()),
180+
Kind::CodeBlock => gen.body_code(b.contains()),
181+
Kind::UnorderedList => gen.body_unordered_list(b.contains()),
182+
Kind::Blank => gen.body_blank(b.contains()),
182183
Kind::Quote => {
183184
let s = b
184185
.quote_ast
185186
.as_ref()
186-
.map(|a| a.render_html_body(html))
187+
.map(|a| a.generate_body(gen))
187188
.unwrap_or_else(|| "".to_string());
188-
html.body_quote(&s)
189+
gen.body_quote(&s)
189190
}
190-
Kind::OrderedList => html.body_ordered_list(b.contains()),
191-
Kind::CodeBlockMark => html.body_plain_text(b.contains()), // treat code block mark as plain text
191+
Kind::OrderedList => gen.body_ordered_list(b.contains()),
192+
Kind::CodeBlockMark => gen.body_plain_text(b.contains()), // treat code block mark as plain text
192193
_ => unreachable!(),
193194
})
194195
.filter(|s| !s.is_empty())
@@ -536,7 +537,7 @@ impl Line {
536537
l
537538
}
538539

539-
pub(crate) fn enter_nested_blocks(&self, html: &impl HtmlGenerate) -> String {
540+
pub(crate) fn enter_nested_blocks(&self, gen: &impl Generate) -> String {
540541
self.nested_blocks
541542
.iter()
542543
.filter(|b| {
@@ -549,21 +550,21 @@ impl Line {
549550
&& b.kind() != Kind::CodeBlock
550551
})
551552
.map(|b| match b.kind() {
552-
Kind::Title => html.body_title(b.first()),
553-
Kind::PlainText => html.body_plain_text(b.contains()),
554-
Kind::Dividing => html.body_dividing(b.first()),
555-
Kind::CodeBlock => html.body_code(b.contains()),
556-
Kind::UnorderedList => html.body_unordered_list(b.contains()),
557-
Kind::Blank => html.body_blank(b.contains()),
553+
Kind::Title => gen.body_title(b.first()),
554+
Kind::PlainText => gen.body_plain_text(b.contains()),
555+
Kind::Dividing => gen.body_dividing(b.first()),
556+
Kind::CodeBlock => gen.body_code(b.contains()),
557+
Kind::UnorderedList => gen.body_unordered_list(b.contains()),
558+
Kind::Blank => gen.body_blank(b.contains()),
558559
Kind::Quote => {
559560
let s = b
560561
.quote_ast
561562
.as_ref()
562-
.map(|a| a.render_html_body(html))
563+
.map(|a| a.generate_body(gen))
563564
.unwrap_or_else(|| "".to_string());
564-
html.body_quote(&s)
565+
gen.body_quote(&s)
565566
}
566-
Kind::OrderedList => html.body_ordered_list(b.contains()),
567+
Kind::OrderedList => gen.body_ordered_list(b.contains()),
567568
_ => "".to_string(),
568569
})
569570
.join("\n")
File renamed without changes.

src/utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub(crate) mod cursor;
2+
pub(crate) mod stack;

0 commit comments

Comments
 (0)