Skip to content

Commit 1f98b01

Browse files
committed
feature: removed the trailing '<br>' of the last element
Signed-off-by: skoowu <skoowu.fancy@gmail.com>
1 parent cbd0960 commit 1f98b01

File tree

1 file changed

+64
-57
lines changed

1 file changed

+64
-57
lines changed

src/html.rs

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::Serialize;
1111
use tinytemplate::TinyTemplate;
1212

1313
pub(crate) struct Generator<'generator> {
14-
tt: TinyTemplate<'generator>,
14+
template: TinyTemplate<'generator>,
1515
cfg: &'generator Config,
1616
ref_link_tags: &'generator HashMap<String, (String, String)>,
1717
}
@@ -22,7 +22,7 @@ impl<'generator> Generator<'generator> {
2222
ref_link_tags: &'generator HashMap<String, (String, String)>,
2323
) -> Result<Self, Box<dyn Error>> {
2424
let mut g = Generator {
25-
tt: TinyTemplate::new(),
25+
template: TinyTemplate::new(),
2626
cfg,
2727
ref_link_tags,
2828
};
@@ -33,21 +33,21 @@ impl<'generator> Generator<'generator> {
3333

3434
fn init(&mut self) -> Result<(), Box<dyn Error>> {
3535
let templates = vec![
36-
(LINK_TEMPLATE_NAME, LINK_TEMPLATE),
37-
(ORDERED_LIST_TEMPLATE_NAME, ORDERED_LIST_TEMPLATE),
38-
(UNORDERED_LIST_TEMPLATE_NAME, UNORDERED_LIST_TEMPLATE),
39-
(TITLE_TEMPLATE_NAME, TITLE_TEMPLATE),
40-
(QUOTE_TEMPLATE_NAME, QUOTE_TEMPLATE),
41-
(IMG_TEMPLATE_NAME, IMG_TEMPLATE),
42-
(CODE_TEMPLATE_NAME, CODE_TEMPLATE),
43-
(PLAIN_TEXT_TEMPLATE_NAME, PLAIN_TEXT_TEMPLATE),
44-
(HEAD_TEMPLATE_NAME, HEAD_TEMPLATE),
45-
(BODY_BEGIN_TEMPLATE_NAME, BODY_BEGIN_TEMPLATE),
36+
(TP_ORDERED_LIST_NAME, TP_ORDERED_LIST),
37+
(TP_UNORDERED_LIST_NAME, TP_UNORDERED_LIST),
38+
(TP_TITLE_NAME, TP_TITLE),
39+
(TP_QUOTE_NAME, TP_QUOTE),
40+
(TP_IMG_NAME, TP_IMG),
41+
(TP_LINK_NAME, TP_LINK),
42+
(TP_CODE_NAME, TP_CODE),
43+
(TP_PLAIN_TEXT_NAME, TP_PLAIN_TEXT),
44+
(TP_HEAD_NAME, TP_HEAD),
45+
(TP_BODY_BEGIN_NAME, TP_BODY_BEGIN),
4646
];
47-
for (name, tpl) in templates {
48-
self.tt.add_template(name, tpl)?;
47+
for (name, tp) in templates {
48+
self.template.add_template(name, tp)?;
4949
}
50-
self.tt
50+
self.template
5151
.set_default_formatter(&tinytemplate::format_unescaped);
5252
Ok(())
5353
}
@@ -112,8 +112,8 @@ impl<'generator> Generator<'generator> {
112112
}
113113
}
114114
TokenKind::Link | TokenKind::QuickLink | TokenKind::Image => {
115-
let gl = t.as_generic_link();
116-
let (name, location) = (gl.name(), gl.location());
115+
let link = t.as_generic_link();
116+
let (name, location) = (link.name(), link.location());
117117

118118
if !name.is_empty() && !location.is_empty() {
119119
let s = if t.kind() == TokenKind::Image {
@@ -125,8 +125,8 @@ impl<'generator> Generator<'generator> {
125125
}
126126
}
127127
TokenKind::RefLink => {
128-
let gl = t.as_generic_link();
129-
let (name, tag) = (gl.name(), gl.tag());
128+
let link = t.as_generic_link();
129+
let (name, tag) = (link.name(), link.tag());
130130

131131
let default = (String::from(""), String::from(""));
132132
let (location, _title) = self.ref_link_tags.get(tag).unwrap_or(&default);
@@ -152,9 +152,9 @@ impl<'generator> Generator<'generator> {
152152
}
153153

154154
fn render_link(&self, show_name: &str, location: &str) -> String {
155-
self.tt
155+
self.template
156156
.render(
157-
LINK_TEMPLATE_NAME,
157+
TP_LINK_NAME,
158158
&LinkContext {
159159
show_name,
160160
location,
@@ -164,8 +164,8 @@ impl<'generator> Generator<'generator> {
164164
}
165165

166166
fn render_image(&self, alt: &str, location: &str) -> String {
167-
self.tt
168-
.render(IMG_TEMPLATE_NAME, &ImageContext { alt, location })
167+
self.template
168+
.render(TP_IMG_NAME, &ImageContext { alt, location })
169169
.unwrap()
170170
}
171171
}
@@ -178,14 +178,14 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
178178
let ctx = HeadContext {
179179
css_href: &self.cfg.css_href,
180180
};
181-
self.tt.render(HEAD_TEMPLATE_NAME, &ctx).unwrap()
181+
self.template.render(TP_HEAD_NAME, &ctx).unwrap()
182182
}
183183

184184
fn body_begin(&self) -> String {
185185
let ctx = BodyBeginContext {
186186
article_class: &self.cfg.add_class_on_article,
187187
};
188-
self.tt.render(BODY_BEGIN_TEMPLATE_NAME, &ctx).unwrap()
188+
self.template.render(TP_BODY_BEGIN_NAME, &ctx).unwrap()
189189
}
190190

191191
fn body_end(&self) -> String {
@@ -207,15 +207,15 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
207207
text: value,
208208
};
209209

210-
self.tt.render(TITLE_TEMPLATE_NAME, &ctx).unwrap()
210+
self.template.render(TP_TITLE_NAME, &ctx).unwrap()
211211
}
212212

213213
fn body_dividing(&self, _l: &SharedLine) -> String {
214214
String::from("<hr>")
215215
}
216216

217217
fn body_plain_text(&self, ls: &[SharedLine]) -> String {
218-
let lines: Vec<String> = ls
218+
let mut lines: Vec<String> = ls
219219
.iter()
220220
.map(|l| {
221221
let mut s = self.render_inline(l.borrow().all());
@@ -225,8 +225,15 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
225225
s
226226
})
227227
.collect();
228-
self.tt
229-
.render(PLAIN_TEXT_TEMPLATE_NAME, &PlainTextContext { lines })
228+
229+
// remove the trailing '<br>' of the last element
230+
// TODO: optimize
231+
if let Some(last) = lines.pop() {
232+
lines.push(last.trim_end_matches("<br>").to_string());
233+
}
234+
235+
self.template
236+
.render(TP_PLAIN_TEXT_NAME, &PlainTextContext { lines })
230237
.unwrap()
231238
}
232239

@@ -249,8 +256,8 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
249256
}
250257
})
251258
.collect();
252-
self.tt
253-
.render(ORDERED_LIST_TEMPLATE_NAME, &OrderedListContext { list })
259+
self.template
260+
.render(TP_ORDERED_LIST_NAME, &OrderedListContext { list })
254261
.unwrap()
255262
}
256263

@@ -269,14 +276,14 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
269276
}
270277
})
271278
.collect();
272-
self.tt
273-
.render(UNORDERED_LIST_TEMPLATE_NAME, &UnorderedListContext { list })
279+
self.template
280+
.render(TP_UNORDERED_LIST_NAME, &UnorderedListContext { list })
274281
.unwrap()
275282
}
276283

277284
fn body_quote(&self, s: &str) -> String {
278-
self.tt
279-
.render(QUOTE_TEMPLATE_NAME, &QuoteContext { text: s })
285+
self.template
286+
.render(TP_QUOTE_NAME, &QuoteContext { text: s })
280287
.unwrap()
281288
}
282289

@@ -289,9 +296,9 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
289296
.map(|l| l.borrow().html_escaped_text())
290297
.collect();
291298

292-
self.tt
299+
self.template
293300
.render(
294-
CODE_TEMPLATE_NAME,
301+
TP_CODE_NAME,
295302
&CodeBlockContext {
296303
name: first.borrow().get(1).map(|t| t.value()).unwrap_or(""),
297304
text: &text,
@@ -302,8 +309,8 @@ impl<'generator> HtmlGenerate for Generator<'generator> {
302309
}
303310

304311
// header
305-
const HEAD_TEMPLATE_NAME: &str = "header";
306-
const HEAD_TEMPLATE: &str = r#"
312+
const TP_HEAD_NAME: &str = "header";
313+
const TP_HEAD: &str = r#"
307314
<head>
308315
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
309316
{{ if css_href }} <link rel="stylesheet" type="text/css" href="{ css_href }"> {{ endif }}
@@ -331,8 +338,8 @@ struct HeadContext<'head_context> {
331338
css_href: &'head_context str,
332339
}
333340

334-
const BODY_BEGIN_TEMPLATE_NAME: &str = "body_begin";
335-
const BODY_BEGIN_TEMPLATE: &str = r#"
341+
const TP_BODY_BEGIN_NAME: &str = "body_begin";
342+
const TP_BODY_BEGIN: &str = r#"
336343
<body>
337344
<article{{ if article_class }} class={article_class}{{ endif }}>
338345
"#;
@@ -343,8 +350,8 @@ struct BodyBeginContext<'body_begin_context> {
343350
}
344351

345352
// title
346-
const TITLE_TEMPLATE_NAME: &str = "title";
347-
const TITLE_TEMPLATE: &str = "\
353+
const TP_TITLE_NAME: &str = "title";
354+
const TP_TITLE: &str = "\
348355
{{ if is_l1 }}<h1>{text}</h1>{{ endif }}\
349356
{{ if is_l2 }}<h2>{text}</h2>{{ endif }}\
350357
{{ if is_l3 }}<h3>{text}</h3>{{ endif }}\
@@ -364,8 +371,8 @@ struct TitleContext {
364371
}
365372

366373
// ordered list
367-
const ORDERED_LIST_TEMPLATE_NAME: &str = "ordered_list";
368-
const ORDERED_LIST_TEMPLATE: &str = "\
374+
const TP_ORDERED_LIST_NAME: &str = "ordered_list";
375+
const TP_ORDERED_LIST: &str = "\
369376
<ol>\
370377
{{ for item in list }}
371378
<li>{item}</li>\
@@ -378,8 +385,8 @@ struct OrderedListContext {
378385
}
379386

380387
// unordered list
381-
const UNORDERED_LIST_TEMPLATE_NAME: &str = "unordered_list";
382-
const UNORDERED_LIST_TEMPLATE: &str = "\
388+
const TP_UNORDERED_LIST_NAME: &str = "unordered_list";
389+
const TP_UNORDERED_LIST: &str = "\
383390
<ul>\
384391
{{ for item in list }}
385392
<li>{item}</li>\
@@ -392,8 +399,8 @@ struct UnorderedListContext {
392399
}
393400

394401
// link
395-
const LINK_TEMPLATE_NAME: &str = "link";
396-
const LINK_TEMPLATE: &str = r#"<a href="{location}">{show_name}</a>"#;
402+
const TP_LINK_NAME: &str = "link";
403+
const TP_LINK: &str = r#"<a href="{location}">{show_name}</a>"#;
397404

398405
#[derive(Serialize)]
399406
struct LinkContext<'link_context> {
@@ -402,8 +409,8 @@ struct LinkContext<'link_context> {
402409
}
403410

404411
// image
405-
const IMG_TEMPLATE_NAME: &str = "img";
406-
const IMG_TEMPLATE: &str = r#"<img src="{location}" alt="{alt}">"#;
412+
const TP_IMG_NAME: &str = "img";
413+
const TP_IMG: &str = r#"<img src="{location}" alt="{alt}">"#;
407414

408415
#[derive(Serialize)]
409416
struct ImageContext<'image_context> {
@@ -412,8 +419,8 @@ struct ImageContext<'image_context> {
412419
}
413420

414421
// code block
415-
const CODE_TEMPLATE_NAME: &str = "code_block";
416-
const CODE_TEMPLATE: &str = "<pre><code>\
422+
const TP_CODE_NAME: &str = "code_block";
423+
const TP_CODE: &str = "<pre><code>\
417424
{text}\
418425
</code></pre>";
419426

@@ -424,8 +431,8 @@ struct CodeBlockContext<'code_block_context> {
424431
}
425432

426433
// plain text
427-
const PLAIN_TEXT_TEMPLATE_NAME: &str = "plain_text";
428-
const PLAIN_TEXT_TEMPLATE: &str = "\
434+
const TP_PLAIN_TEXT_NAME: &str = "plain_text";
435+
const TP_PLAIN_TEXT: &str = "\
429436
<p>\
430437
{{ for text in lines}}\
431438
{text}\
@@ -438,8 +445,8 @@ struct PlainTextContext {
438445
}
439446

440447
// quote block
441-
const QUOTE_TEMPLATE_NAME: &str = "quote";
442-
const QUOTE_TEMPLATE: &str = "\
448+
const TP_QUOTE_NAME: &str = "quote";
449+
const TP_QUOTE: &str = "\
443450
<blockquote><p>
444451
{text}
445452
</p></blockquote>";

0 commit comments

Comments
 (0)