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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Tp-Note's high level HTML rendering API.
//!
//! A set of functions that take a `Context` type and a `Content` type (or raw
//! text) and return the HTML rendition of the content. The API is completely
//! stateless. All functions read the `LIB_CFG` global variable to read the
//! configuration stored in `LibCfg.tmpl_html`.
use crate::config::LIB_CFG;
use crate::config::LocalLinkKind;
use crate::content::Content;
use crate::context::Context;
use crate::context::HasSettings;
use crate::error::NoteError;
#[cfg(feature = "viewer")]
use crate::filter::TERA;
use crate::html::HTML_EXT;
use crate::html::rewrite_links;
use crate::note::Note;
#[cfg(feature = "viewer")]
use crate::note::ONE_OFF_TEMPLATE_NAME;
#[cfg(feature = "viewer")]
use crate::note_error_tera_template;
use crate::template::TemplateKind;
use parking_lot::RwLock;
use std::collections::HashSet;
use std::fs::OpenOptions;
use std::io;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
#[cfg(feature = "viewer")]
use tera::Tera;
/// High level API to render a note providing its `content` and some `context`.
pub struct HtmlRenderer;
impl HtmlRenderer {
/// Returns the HTML rendition of a `ContentString`.
///
/// The markup to HTML rendition engine is determined by the file extension
/// of the variable `context.path`. The resulting HTML and other HTML
/// template variables originating from `context` are inserted into the
/// `TMPL_HTML_VIEWER` template before being returned.
/// The string `viewer_doc_js` contains JavaScript live update code that
/// will be injected into the HTML page via the
/// `TMPL_HTML_VAR_DOC_VIEWER_JS` template variable.
/// This function is stateless.
///
/// ```rust
/// use tpnote_lib::content::Content;
/// use tpnote_lib::content::ContentString;
/// use tpnote_lib::context::Context;
/// use tpnote_lib::html_renderer::HtmlRenderer;
/// use std::env::temp_dir;
/// use std::fs;
/// use std::path::Path;
///
/// // Prepare test: create existing note file.
/// let content = ContentString::from_string(String::from(r#"---
/// title: My day
/// subtitle: Note
/// ---
/// Body text
/// "#), "doc".to_string());
///
/// // Start test
/// let mut context = Context::from(Path::new("/path/to/note.md")).unwrap();
/// // We do not inject any JavaScript.
/// // Render.
/// let html = HtmlRenderer::viewer_page::<ContentString>(context, content, "")
/// .unwrap();
/// // Check the HTML rendition.
/// assert!(html.starts_with("<!DOCTYPE html>\n<html"))
/// ```
///
/// A more elaborated example that reads from disk:
///
/// ```rust
/// use tpnote_lib::config::LIB_CFG;
/// use tpnote_lib::content::Content;
/// use tpnote_lib::content::ContentString;
/// use tpnote_lib::context::Context;
/// use tpnote_lib::html_renderer::HtmlRenderer;
/// use std::env::temp_dir;
/// use std::fs;
///
/// // Prepare test: create existing note file.
/// let raw = r#"---
/// title: My day2
/// subtitle: Note
/// ---
/// Body text
/// "#;
/// let notefile = temp_dir().join("20221030-My day2--Note.md");
/// fs::write(¬efile, raw.as_bytes()).unwrap();
///
/// // Start test
/// let mut context = Context::from(¬efile).unwrap();
/// // We do not inject any JavaScript.
/// // Render.
/// let content = ContentString::open(context.get_path()).unwrap();
/// // You can plug in your own type (must impl. `Content`).
/// let html = HtmlRenderer::viewer_page(context, content, "").unwrap();
/// // Check the HTML rendition.
/// assert!(html.starts_with("<!DOCTYPE html>\n<html"))
/// ```
pub fn viewer_page<T: Content>(
context: Context<HasSettings>,
content: T,
// Java Script live updater inject code. Will be inserted into
// `tmpl_html.viewer`.
viewer_doc_js: &str,
) -> Result<String, NoteError> {
let tmpl_html = &LIB_CFG.read_recursive().tmpl_html.viewer;
HtmlRenderer::render(context, content, viewer_doc_js, tmpl_html)
}
/// Returns the HTML rendition of a `ContentString`.
/// The markup to HTML rendition engine is determined by the file extension
/// of the variable `context.path`. The resulting HTML and other HTML
/// template variables originating from `context` are inserted into the
/// `TMPL_HTML_EXPORTER` template before being returned.
/// `context` is expected to have at least all `HasSettings` keys
/// and the additional key `TMPL_HTML_VAR_VIEWER_DOC_JS` set and valid.
/// All other keys are ignored.
/// This function is stateless.
///
/// ```rust
/// use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
/// use tpnote_lib::content::Content;
/// use tpnote_lib::content::ContentString;
/// use tpnote_lib::context::Context;
/// use tpnote_lib::html_renderer::HtmlRenderer;
/// use std::env::temp_dir;
/// use std::fs;
/// use std::path::Path;
///
/// // Prepare test: create existing note file.
/// let content= ContentString::from_string(String::from(r#"---
/// title: "My day"
/// subtitle: "Note"
/// ---
/// Body text
/// "#), "doc".to_string());
///
/// // Start test
/// let mut context = Context::from(Path::new("/path/to/note.md")).unwrap();
/// // Render.
/// let html = HtmlRenderer::exporter_page::<ContentString>(context, content)
/// .unwrap();
/// // Check the HTML rendition.
/// assert!(html.starts_with("<!DOCTYPE html>\n<html"))
/// ```
pub fn exporter_page<T: Content>(
context: Context<HasSettings>,
content: T,
) -> Result<String, NoteError> {
let tmpl_html = &LIB_CFG.read_recursive().tmpl_html.exporter;
HtmlRenderer::render(context, content, "", tmpl_html)
}
/// Helper function.
fn render<T: Content>(
context: Context<HasSettings>,
content: T,
viewer_doc_js: &str,
tmpl_html: &str,
) -> Result<String, NoteError> {
let note = Note::from_existing_content(context, content, TemplateKind::None)?;
note.render_content_to_html(tmpl_html, viewer_doc_js)
}
/// When the header cannot be deserialized, the file located in
/// `context.path` is rendered as "Error HTML page".
///
/// The erroneous content is rendered to html with
/// `parse_hyperlinks::renderer::text_rawlinks2html` and inserted in
/// the `TMPL_HTML_VIEWER_ERROR` template (which can be configured at
/// runtime).
/// The string `viewer_doc_js` contains JavaScript live update code that
/// will be injected into the HTML page via the
/// `TMPL_HTML_VAR_DOC_VIEWER_JS` template variable.
/// This function is stateless.
///
/// ```rust
/// use tpnote_lib::config::LIB_CFG;
/// use tpnote_lib::config::TMPL_HTML_VAR_DOC_ERROR;
/// use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
/// use tpnote_lib::content::Content;
/// use tpnote_lib::content::ContentString;
/// use tpnote_lib::context::Context;
/// use tpnote_lib::error::NoteError;
/// use tpnote_lib::html_renderer::HtmlRenderer;
/// use std::env::temp_dir;
/// use std::fs;
///
/// // Prepare test: create existing erroneous note file.
/// let raw_error = r#"---
/// title: "My day3"
/// subtitle: "Note"
/// --
/// Body text
/// "#;
/// let notefile = temp_dir().join("20221030-My day3--Note.md");
/// fs::write(¬efile, raw_error.as_bytes()).unwrap();
/// let mut context = Context::from(¬efile);
/// let e = NoteError::FrontMatterFieldMissing { field_name: "title".to_string() };
///
/// // Start test
/// let mut context = Context::from(¬efile).unwrap();
/// // We do not inject any JavaScript.
/// // Render.
/// // Read from file.
/// // You can plug in your own type (must impl. `Content`).
/// let content = ContentString::open(context.get_path()).unwrap();
/// let html = HtmlRenderer::error_page(
/// context, content, &e.to_string(), "").unwrap();
/// // Check the HTML rendition.
/// assert!(html.starts_with("<!DOCTYPE html>\n<html"))
/// ```
#[cfg(feature = "viewer")]
pub fn error_page<T: Content>(
context: Context<HasSettings>,
note_erroneous_content: T,
error_message: &str,
// Java Script live updater inject code. Will be inserted into
// `tmpl_html.viewer`.
viewer_doc_js: &str,
) -> Result<String, NoteError> {
//
let context =
context.insert_error_content(¬e_erroneous_content, error_message, viewer_doc_js);
let tmpl_html = &LIB_CFG.read_recursive().tmpl_html.viewer_error;
// Apply template.
let mut tera = Tera::default();
// Switch `autoescape_on()` only for HTML templates.
tera.autoescape_on(vec![ONE_OFF_TEMPLATE_NAME]);
tera.extend(&TERA)?;
let html = tera
.render_str(tmpl_html, &context)
.map_err(|e| note_error_tera_template!(e, "[html_tmpl] viewer_error".to_string()))?;
Ok(html)
}
/// Renders `doc_path` with `content` into HTML and saves the result in
/// `export_dir` in case `export_dir` is an absolute directory. Otherwise
/// the parent directory of `doc_path` is concatenated with `export_dir`
/// and the result is stored there.
/// `-` dumps the rendition to STDOUT. The filename of the HTML rendition is
/// the same as in `doc_path` but with `.html` appended.
///
/// ```rust
/// use tpnote_lib::config::LIB_CFG;
/// use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
/// use tpnote_lib::config::LocalLinkKind;
/// use tpnote_lib::content::Content;
/// use tpnote_lib::content::ContentString;
/// use tpnote_lib::context::Context;
/// use tpnote_lib::html_renderer::HtmlRenderer;
/// use std::env::temp_dir;
/// use std::fs;
/// use std::path::Path;
///
/// // Prepare test: create existing note file.
/// let raw = r#"---
/// title: "My day3"
/// subtitle: "Note"
/// ---
/// Body text
/// "#;
/// let notefile = temp_dir().join("20221030-My day3--Note.md");
/// fs::write(¬efile, raw.as_bytes()).unwrap();
///
/// // Start test
/// let content = ContentString::open(¬efile).unwrap();
/// // You can plug in your own type (must impl. `Content`).
/// HtmlRenderer::save_exporter_page(
/// ¬efile, content, Path::new("."), LocalLinkKind::Long).unwrap();
/// // Check the HTML rendition.
/// let expected_file = temp_dir().join("20221030-My day3--Note.md.html");
/// let html = fs::read_to_string(expected_file).unwrap();
/// assert!(html.starts_with("<!DOCTYPE html>\n<html"))
/// ```
pub fn save_exporter_page<T: Content>(
doc_path: &Path,
content: T,
export_dir: &Path,
local_link_kind: LocalLinkKind,
) -> Result<(), NoteError> {
let context = Context::from(doc_path)?;
let doc_path = context.get_path();
let doc_dir = context.get_dir_path().to_owned();
// Determine filename of html-file.
let html_path = match export_dir {
p if p == Path::new("-") => PathBuf::new(),
p => {
let mut html_filename = doc_path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
html_filename.push_str(HTML_EXT);
let mut q = doc_path.parent().unwrap_or(Path::new("")).to_path_buf();
q.push(p);
q.push(PathBuf::from(html_filename));
q
}
};
if html_path == Path::new("") {
log::debug!("Rendering HTML to STDOUT (`{:?}`)", export_dir);
} else {
log::debug!("Rendering HTML into: {:?}", html_path);
};
// These must live longer than `writeable`, and thus are declared first:
let (mut stdout_write, mut file_write);
// We need to ascribe the type to get dynamic dispatch.
let writeable: &mut dyn Write = if html_path == Path::new("") {
stdout_write = io::stdout();
&mut stdout_write
} else {
file_write = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&html_path)?;
&mut file_write
};
// Render HTML.
let root_path = context.get_root_path().to_owned();
let html = Self::exporter_page(context, content)?;
let html = rewrite_links(
html,
&root_path,
&doc_dir,
local_link_kind,
// Do append `.html` to `.md` in links.
true,
Arc::new(RwLock::new(HashSet::new())),
);
// Write HTML rendition.
writeable.write_all(html.as_bytes())?;
Ok(())
}
}