pub struct HtmlRenderer;
Expand description
High level API to render a note providing its content
and some context
.
Implementations§
Source§impl HtmlRenderer
impl HtmlRenderer
Sourcepub fn viewer_page<T: Content>(
context: Context<HasSettings>,
content: T,
viewer_doc_js: &str,
) -> Result<String, NoteError>
pub fn viewer_page<T: Content>( context: Context<HasSettings>, content: T, viewer_doc_js: &str, ) -> Result<String, NoteError>
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.
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:
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"))
Sourcepub fn exporter_page<T: Content>(
context: Context<HasSettings>,
content: T,
) -> Result<String, NoteError>
pub fn exporter_page<T: Content>( context: Context<HasSettings>, content: T, ) -> Result<String, NoteError>
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.
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"))
Sourcepub fn error_page<T: Content>(
context: Context<HasSettings>,
note_erroneous_content: T,
error_message: &str,
viewer_doc_js: &str,
) -> Result<String, NoteError>
pub fn error_page<T: Content>( context: Context<HasSettings>, note_erroneous_content: T, error_message: &str, viewer_doc_js: &str, ) -> Result<String, NoteError>
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.
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"))
Sourcepub fn save_exporter_page<T: Content>(
doc_path: &Path,
content: T,
export_dir: &Path,
local_link_kind: LocalLinkKind,
) -> Result<(), NoteError>
pub fn save_exporter_page<T: Content>( doc_path: &Path, content: T, export_dir: &Path, local_link_kind: LocalLinkKind, ) -> Result<(), NoteError>
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 the standard output. The filename of the HTML
rendition is the same as in doc_path
but with .html
appended.
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"))
Auto Trait Implementations§
impl Freeze for HtmlRenderer
impl RefUnwindSafe for HtmlRenderer
impl Send for HtmlRenderer
impl Sync for HtmlRenderer
impl Unpin for HtmlRenderer
impl UnwindSafe for HtmlRenderer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more